0

so what i'm trying to get my head around it say i have a vector of vectors containing some unsigned chars and i want to copy a certain vector to another vector as follows.

std::vector< std::vector< unsigned char > > foo;
std::vector < unsigned char > bar;

for (int x = 0; x < foo.size(); x++)
{
    // do stuff
    if (x == 3)
    {
        bar.push_back(&foo[x]);
    }
}

How can i go about doing this?

Thanks in advance.

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by L7Sqr
1

so what i'm trying to get my head around it say i have a vector of vectors containing some unsigned chars and i want to copy a certain vector to another vector as follows.
...
How can i go about doing this?

Do you want to have a copy of the original vector or do you want the modifications to either version to affect the other? I'll assume the former.

std::vector< std::vector< int > > vvi;
std::vector< int > vi;

//...
std::copy (vvi[3].begin (), vvi[3].end (), std::back_inserter(vi));

std::copy will copy all elements between the first two iterators into the location starting at the third iterator. In this case, to append to whatever contents are in the destination already, you can use a std::back_inserter .

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.