Thread: push_back vector at the end of another vector

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    34

    push_back vector at the end of another vector

    i have a recursive function that return a vector
    and the problem is when the function recall itself it make some copy of variables and vector then return that vector
    so if i need all data i need to somehow push the return vector into main vector...
    for example i have sth like
    Code:
    std::vector<std::string> test(char *root)
    {
    std::vector<std::string>pointer;
    ...//some function and loops
    test(variable);
    return pointer
    }
    when i use
    pointer.push_back(test(variable));
    instead of
    test(variable);
    give me error that
    no matching function for call to 'std::vector<std::basic_string<char>
    >:/push_back(std::vector<std::basic_string<char> >)'|

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    std::vector<T>'s push_back works with elements (i.e. a T), not a std::vector<T>. Instead use this.
    Code:
        std::vector<std::string> temp = test(variable);
        pointer.insert(pointer.end(), temp.begin(), temp.end());
    You might also want to think about your variable names. A std::vector<std::string> named pointer is far from clear coding.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    Quote Originally Posted by grumpy View Post
    You might also want to think about your variable names. A std::vector<std::string> named pointer is far from clear coding.
    my real code is so much different from this i said its just an example...
    and tnx your solution works...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector push_back array
    By Tropod in forum C++ Programming
    Replies: 3
    Last Post: 10-15-2010, 04:46 PM
  2. vector<float*> push_back() problem
    By stella1016 in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2009, 08:57 AM
  3. Error from vector push_back()
    By The Wazaa in forum C++ Programming
    Replies: 7
    Last Post: 03-11-2006, 01:15 PM
  4. vector.push_back(anotherVector)
    By boojus in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2003, 05:07 PM
  5. vector<bool> push_back
    By ygfperson in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2003, 08:48 PM