Thread: Pushback a vector

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    10

    Pushback a vector

    How would you add a vector to the end of the collection of vectors of a class?
    like..
    trees is a class

    Code:
    vector <trees> a;
    vector <trees> b;
    
    //now I want to add b to the vector a
    // Is it this?? it doesn't seem to work!
    
    a.push_back(b);

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Code:
    a.insert(a.end(), b.begin(), b.end());
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    simple :S


    Code:
    #include<iostream>
    #include<vector> //You have this line missing 
    
    using namespace std;
    int main()
    
    {
    
    vector <trees> a;
    a.push_back("Tree"); //You have the qoutation marks missing
    
    return 0;
    
    }
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    (Ignore this line... it is incorrect). Instead of insert, you can use append which might be optimized for the task of inserting at the end.

    Hussain Hani, it looks like the OP is trying to add all the values in one vector to the end of another.
    Last edited by Daved; 10-18-2006 at 03:30 PM.

  5. #5
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    wat is OP ??
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Daved
    Instead of insert, you can use append which might be optimized for the task of inserting at the end.
    Ok Daved, you stumped me, where is append located? I tried a simple example, but append isn't recognized.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> wat is OP ??
    Original Poster.

    >> where is append located?
    Apparently only in my head. I guess I was thinking of the string class.

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Hmmm, do you mean something like a vector of a vector of trees? (edit: whoops, my example uses ints. Ah well. It brings accross the idea. )

    Code:
    #include <iostream>
    #include <vector>
    
    int main( void )
    {
    	std::vector< int > vec_trees;
    	std::vector< std::vector<int> > vec_vec_trees;
    
    	// initialise vec_trees
    
    	vec_vec_trees.push_back( vec_trees );
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM