Pushback a vector

This is a discussion on Pushback a vector within the C++ Programming forums, part of the General Programming Boards category; How would you add a vector to the end of the collection of vectors of a class? like.. trees is ...

  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
    21,310
    Code:
    a.insert(a.end(), b.begin(), b.end());
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,317
    (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,317
    >> 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,262
    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, 12:26 AM

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