# Thread: How do I convert a vector...

1. ## How do I convert a vector...

How do I convert a vector of vectors to a vector of its datatype?

Example:

I have a "vector <char> nameOfVectorOfChars". I then have a "vector < vector <char> > nameOfVectorOfVectorOfChars".
I wish to get the content of the vector of chars contained in the vector of vector of chars, and push_back it to the vector of chars. How do I do this?

2. Wait. So, just so I understand what you mean -- you want to take a vector of vectors and flatten it to a one dimensional vector?

3. Originally Posted by whiteflags
Wait. So, just so I understand what you mean -- you want to take a vector of vectors and flatten it to a one dimensional vector?
Something like that.
I want to get the content (i.e. the chars) of a vector contained in the vector of vectors of chars, and add the content to a vector of chars. Make any sense?

Think of it this way...

I start off with a couple of vectors of chars and one vector of vectors of chars. I then add/push_back each vector of chars to that vector of vector of chars. Now, I want to retrieve the chars contained in each vector of chars contained in the vector of vectors of chars. If I try using the at() function of the vector of vectors of chars, it will only retrieve the vectors of chars contained within it, and not the chars contained within those vectors, which is what I actually want.

4. I see, well the content is already a vector of chars, so...

You would make a copy of the first element (the first vector in the vector) and then append the rest of the vectors to the copy with something like insert. Delete your original and there you have it.

If you're just going to flatten this data structure anyway, though, you really should find some way to use a one dimensional vector from the start, I think.

5. I think you're asking how to do this:
Code:
```vector<char> nameOfVectorOfChars;
nameOfVectorOfChars.push_back('a');
nameOfVectorOfChars.push_back('b');
nameOfVectorOfChars.push_back('c');

vector< vector<char> > nameOfVectorOfVectorOfChars;
nameOfVectorOfVectorOfChars.push_back(nameOfVectorOfChars);```
I hope you use better names in the real code of course.
If your nameOfVectorOfChars is large enough and you don't need it afterwards, there is a more efficient way, but you don't need to worry about that yet.

6. Originally Posted by iMalc
I think you're asking how to do this:
Code:
```vector<char> nameOfVectorOfChars;
nameOfVectorOfChars.push_back('a');
nameOfVectorOfChars.push_back('b');
nameOfVectorOfChars.push_back('c');

vector< vector<char> > nameOfVectorOfVectorOfChars;
nameOfVectorOfVectorOfChars.push_back(nameOfVectorOfChars);```
I hope you use better names in the real code of course.
If your nameOfVectorOfChars is large enough and you don't need it afterwards, there is a more efficient way, but you don't need to worry about that yet.
No, I already know how to do that part. I was just wondering how to retrieve the content of the vector(s) of chars contained inside the vector of vector of chars, after I've already done that.

But, anyway, forget it...

7. Well I took an interest in your problem... you could always treat it like a matrix.

Code:
```typedef std::vector<char> charArray;
typedef std::vector<charArray> charMatrix;

for (charMatrix::size_type i = 0; i < foo.size (); i++) {
std::cout << "foo[" << i <<"] is ( ";
for (charArray::size_type j = 0; j < foo[i].size (); j++) {
std::cout << foo[i][j] << ", ";
}
std::cout << ").\n";
}```
But I read that you wanted something different.

Flattening is still possible, like I said, make a copy of the first vector in the vector, and then append the rest of the vectors in the vector to that, using insert.

So keep it in mind.