How do I convert a vector...

This is a discussion on How do I convert a vector... within the C++ Programming forums, part of the General Programming Boards category; How do I convert a vector of vectors to a vector of its datatype? Example: I have a "vector <char> ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question 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. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    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. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    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.

    EDIT: Nevermind...I'll use strings instead.
    Last edited by Programmer_P; 05-27-2010 at 04:34 PM.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    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. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by iMalc View Post
    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...
    I'm using strings instead.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning a pointer to a vector
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 08-27-2009, 06:15 AM
  2. Problems when destroying a vector of strings
    By ryanrigdon in forum C++ Programming
    Replies: 15
    Last Post: 08-17-2009, 04:14 PM
  3. convert C++ vector into C
    By meerajpatel in forum C Programming
    Replies: 10
    Last Post: 07-09-2009, 05:09 AM
  4. C programing
    By flame82 in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 02:35 PM
  5. my vector class..easy dynamic arrays
    By nextus in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2003, 09:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21