Click here to Skip to main content
15,887,262 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all,

I want to initialize a vector of vectors in c++ vs2010
I tried to declare a table of doubles

double dTable[3][4] = { 1,2,3,4,
5,6,7,8,
9,0,-1,-2};

and then declaring a vector of vectors:
using namespace std;
vector < vector<double>> vOfvs (&dtable[0][0], &dTable[0][0]+63)

I get a warning c4244 conversion from double to unassigned __int64
and a linker error that says that dTable is already defined

What is the correct syntax for this in vs2010?

Thanks!

What I have tried:

Googling it
Run in different editor
Posted
Comments
Richard MacCutchan 20-Feb-24 3:50am    
Please show the actual code (correctly formatted between <pre> tag) and the complete error messages. As it stands whatr you have shown is not clear.
0x01AA 20-Feb-24 3:52am    
Richard MacCutchan 20-Feb-24 4:23am    
That's the correct answer, as I have just discovered.

There is I direct way without first declaring a helper vector:
vector<vector<double> > myVector 
    { 
        /* Element 0 */
        {1, 2, 3, 4},  
        
        /* Element 1 */
        {5, 6, 7, 8},  
        
         /* Element 2 */
        {9, 10, 11, 12}  
    }; 

More details you can find her: 2D Vector In C++ With User Defined Size - GeeksforGeeks[^]
 
Share this answer
 
v2
Comments
CPallini 20-Feb-24 4:46am    
5.
0x01AA 20-Feb-24 5:06am    
Thank you very much
dj4400 20-Feb-24 5:22am    
Hi
Thanks for your reply but this syntax doesn't work in vs2010
0x01AA 20-Feb-24 7:10am    
I see, sorry I completely ovelloked the vs2010.

Yes, the above works only from c++11.

For c++98 I see only the way, that you make a helper function where you pass the matix to initalize by reference and something like the raw data you show in the weustion and the row and column dimensions :(
The problem is probably that Visual Studio 2010 does not support C++11 initialization lists for vectors. Although I cannot test with VS2010, I could imagine that this task could be solved with a template function. Here, a two-dimensional vector is initialized with predefined values.
C++
template<typename T, size_t N, size_t M>
void initializeVector(std::vector<std::vector<T>>&targetVector, const T(&sourceArray)[N][M]) {
    for (size_t i = 0; i < N; ++i) {
        std::vector<T> row;
        for (size_t j = 0; j < M; ++j) {
            row.push_back(sourceArray[i][j]);
        }
        targetVector.push_back(row);
    }
}

This could then be used as follows:
C++
std::vector<std::vector<double>> myArray;
const double predefinedValues[3][3] = { 
    {1.0, 2.0, 3.0},
    {4.0, 5.0, 6.0},
    {7.0, 8.0, 9.0}
};

initializeVector(myArray, predefinedValues);
 
Share this answer
 
v3
Comments
0x01AA 21-Feb-24 7:06am    
Great, my 5!

Note: Some older compiler do have problems with
std::vector<std::vector<double>> and need that as std::vector<std::vector<double> >

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900