user13549804
user13549804

Reputation:

Create 2D vector from 2 1D vectors

I am fairly new to vectors and I'm trying to populate a 2D vector from 2 1D vectors for coordinate points. I have 2 vectors like this where source_x and source_y contains values from a file:

std::vector<float,T<float>> pos_x(5); 
std::vector<float,T<float>> pos_y(5); 

for (int i = 0; i < 5 ; i++){
  pos_x[i] = i+1; //{1,2,3,4,5}
}
for (int i = 0; i < num ; i++){
   pos_y[i] = i+1 ; //{1,2,3,4,5}
} 

I created my 2D vector like this:

std::vector<std::vector<float, T<float>>> position; 
for (int i = 0; i < num ; i++){
    for (int j = 0; j < num ; i++){
         //Output expected: {{1,2,3,4,5},{1,2,3,4,5}}
         position[i][j] = //I'm confuse here 
    }
}

I am not certain how to populate pos_x to position[i] and pos_y to position[j].

Thank you

Upvotes: 0

Views: 1623

Answers (2)

Ripi2
Ripi2

Reputation: 7198

There's no such thing as a 2D vector. The truth is that you can create a vector that contains vectors. The first vector is used as an index in the collected vectors.

Note that this concept is similar to a 2D array: arr[3][4] means 3 indices, each one points to 4 data.

To create such 2D vector:

std::vector< std::vector <float>> positions.

Notice I didn't use the second parameter (as in std::vector<float, SomeAllocator> because we don't need this custom memory allocator.

Also notice that, contrary to arrays, I did't tell anything about the sizes of each vector, because the std::vector will take care of it.

Let's populate it.

The "main" vector contains vectors. So these secondary vectors may be created before stored in the "main" one.

std::vector<float> v1; //secondary
positions.push_back(v1); //add it to main vector

Put some values in the secondary:

v1.push_back(7.5);
v1.push_back(-3.1);

Another way is to access through the main vector. If we new this main vector contains v1 in its first index:

positions[0].push_back(8.); // same as v1.push_back(8.) if positions[0] refers to v1

or better using "at": positions.at(0).push_back(8.);

Change some value:

v1.at(1) = 66.88;

or

positions[0].at(1) = 66.88;

You can also do v1[1] = 66.88 but prefer the at() methof because it will check that index "1" is allowed by the size of the vector v1.

You can create and add another secondary vector:

std::vector<float> v2; //secondary
positions.push_back(v2); //add it to main vector

and work with it the same as with previous v1. Now, positions[1] refers to v2

I leave the rest of pulling from other vectors to you.

Upvotes: 0

john
john

Reputation: 87957

So my guess is this

std::vector<std::vector<float>> position(num, std::vector<float>(2)); 
for (int i = 0; i < num ; i++){
     position[i][0] = pos_x[i];
     position[i][1] = pos_y[i];
}

But I could easily be wrong.

UPDATE based on the example in the question I now think this is the correct code

std::vector<std::vector<float>> position(2, std::vector<float>(num)); 
for (int i = 0; i < num ; i++){
     position[0][i] = pos_x[i];
     position[1][i] = pos_y[i];
}

Upvotes: 1

Related Questions