1. ## Vector Of Vectors

Hi, I am Trying to create a vector of vectors

I have managed to create the vector structure
but now i need to add elements to it with a particular sequence

for example if i call the function, with a variable number (which = 5)

i want the vector to be able to go through and create a vector structure like
vector vector 0 = 1, 0, 0, 0, 0, 0
vector vector 1 = 2, 0, 0, 0, 0, 0
vector vector 2 = 3, 0, 0, 0, 0, 0
vector vector 3 = 4, 0, 0, 0, 0, 0
vector vector 4 = 5, 0, 0, 0, 0, 0

i have tried many ways but cannot seem to assign the correct values

Code:
```vector<vector<int> > MYVEC; // create vector

void Initialise(vector<vector<int> > &MYVEC, int num) // function to initialise vector
{
int v = 1;	// used to assign 1, 2, 3 ...
int k = 0;	// used always assigning 0 to other elements

for ( int i = 0; i < num; i++ )
{
MYVEC.push_back ( vector<int>() );

MYVEC[i].push_back(i);

for ( int j = 0; j < numVertices; j++ )
{
MYVEC[i].push_back(i);
MYVEC[i][j] = 0;
}
}
}```
This dosnt work and just assigns 0 to all elements,
so is like:
vector vector 0 = 0, 0, 0, 0, 0, 0
vector vector 1 = 0, 0, 0, 0, 0, 0
vector vector 2 = 0, 0, 0, 0, 0, 0
vector vector 3 = 0, 0, 0, 0, 0, 0
vector vector 4 = 0, 0, 0, 0, 0, 0

i need the first element of each vector in the big vector to be go up from 1, 2 ... etc

Thanks

2. One way:
Code:
```void Initialise(vector<vector<int> >& myvec, int num)
{
vector<int> temp(6);
for (int i = 0; i < num; ++i)
{
temp[0] = i + 1;
myvec.push_back(temp);
}
}```

3. Hi, thanks i have done it using

Code:
```	int k = 0;	// used to assign 0 to other elements

for ( int i = 0; i < num; i++ )
{
MYVEC.push_back ( vector<int>() );

for ( int j = 0; j < num; j++ )
{
MYVEC[i].push_back(i);

MYVEC[i][0] = (i+1);	// set first elem vector to 1...+
MYVEC[i][j] = (k);		// set second elem...+ of vector to 0
}
}```
this seems to do what i need and gives me:
vector vector 0 = 1, 0, 0, 0, 0, 0
vector vector 1 = 2, 0, 0, 0, 0, 0
vector vector 2 = 3, 0, 0, 0, 0, 0
vector vector 3 = 4, 0, 0, 0, 0, 0
vector vector 4 = 5, 0, 0, 0, 0, 0

now if i need to search through the vector, for example to find where a certain number is, e.g. 1
i want it to return 1 location = 0 (as first elem of vec vec 0 is 1)
e.g. 2
i want it to return 2 location = 1 (as first elem of vec vec 1 is 2)

how would i do this?
thanks

4. Hi, ignore the last post I have done it.
Thanks =)

5. Please stop naming your vector MYVEC. Conventionally, names that are fully capitalised are used for macros, and maybe also constants.

this seems to do what i need and gives me:
vector vector 0 = 1, 0, 0, 0, 0, 0
vector vector 1 = 2, 0, 0, 0, 0, 0
vector vector 2 = 3, 0, 0, 0, 0, 0
vector vector 3 = 4, 0, 0, 0, 0, 0
vector vector 4 = 5, 0, 0, 0, 0, 0
It gives me:
Code:
```1 0 0 0 0
2 0 0 0 0
3 0 0 0 0
4 0 0 0 0
5 0 0 0 0```
Spot the difference. Furthermore, it is inefficient as you are repeatedly assigning to MYVEC[i][0] and then you don't have to assign k to MYVEC[i][j] when you could have just pushed k. Do you understand my example?

now if i need to search through the vector, for example to find where a certain number is, e.g. 1
i want it to return 1 location = 0 (as first elem of vec vec 0 is 1)
e.g. 2
i want it to return 2 location = 1 (as first elem of vec vec 1 is 2)

how would i do this?
What have you tried?

6. Hi, thanks i understand your example now and it works =)
sorry about the MYVEC, it was just an example to show you what i mean.

Actually would i be able to change the vector so i only have
vector vector 0 = 1
vector vector 1 = 2
vector vector 2 = 3
vector vector 3 = 4
vector vector 4 = 5
vector vector 5 = 6
as the 0 are not really needed and i can pushback data to each vector vector later when needed?

I've got the search this working so thats fine.

Actually would i be able to change the vector so i only have
vector vector 0 = 1
vector vector 1 = 2
vector vector 2 = 3
vector vector 3 = 4
vector vector 4 = 5
vector vector 5 = 6
as the 0 are not really needed
Yes, and that makes it even easier, e.g.,
Code:
```void Initialise(vector<vector<int> >& myvec, int num)
{
for (int i = 0; i < num; ++i)
{
myvec.push_back(vector<int>(1, i + 1));
}
}```

8. Thank that works =)

Hi, that dosnt seem to work
it only assigns 1 and then throws an error?
It seems you did something wrong

(Hint: if you don't want a vague assessment, don't give vague feedback.)

10. thanks sorry i fixed it.

I now need to be able to merge 2 vectors inside the big vector

e.g. i have
vector vector 0 = 1
vector vector 1 = 2
vector vector 2 = 3
vector vector 3 = 4
vector vector 4 = 5
vector vector 5 = 6

-----------------------------------------

i may need to merge vector vector 0 and 1
to get vector vector 0 = 1, 2
vector vector 1 = nothing

then may need to merge vector vector 2 and 3
to get vector vector 2 = 3, 4
vector vector 3 = nothing

then may need to merge vector vector 0 and 2
to get vector vector 0 = 1,2,3,4
vector vector 2 = nothing

could you please explain how this could be done
would i just have to push each vector the the vector

11. Hi, I am trying this

my vector is:
vector vector 0 = 1
vector vector 1 = 2
vector vector 2 = 3
vector vector 3 = 4
vector vector 4 = 5
vector vector 5 = 6

for example if:
int j = 1;
int k = 2;

i need to merge vector vector 0 and vector vector 1

i am trying

vector[j-1].resize(vector[j-1].size() + vector[k-1].size());
merge(vector[j-1].begin(), vector[j-1].end(), vector[k-1].begin(), vector[k-1].end(), vector[j-1].begin());

but it dosnt work

it gives an error "sequence not ordered"

12. Hi, dont worry i figured it out.
thanks for all your help =)