Thread: Adding vectors ??

  1. #1
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186

    Adding vectors ??

    When i last time tried to add vectors i got nasty error (in dev-c++) that opens another file .cpp and cite the error, a line that has some iterator.

    this is the code

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
    	//Making vectors 
    	
    	vector<string> my;
    	my.push_back("Car");
    	my.push_back("Boat");
    	my.push_back("Plane");
    
    	vector<string> yours;
    	yours.push_back("Jet");
    	yours.push_back("Parachute");
    	yours.push_back("Wings");
    
    	vector<string> ours(my, yours);
    
    	return 0;
    }


    plz tell me how to add more than one vector (for example 5 vectors) in one vector ? do i have to specify the size of the vector that is having all the vectors ??

    "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 <_<

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Assuming that you want to combine the two vectors, it would be something like:
    Code:
    vector<string> ours(my);
    ours.insert(ours.end(), yours.begin(), yours.end());
    If you want to create a vector of vectors, then it would be:
    Code:
    vector<vector<string> > ours;
    ours.push_back(my);
    ours.push_back(yours);
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  2. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  3. Adding Vectors
    By Tonto in forum Game Programming
    Replies: 5
    Last Post: 11-20-2006, 01:06 AM
  4. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM
  5. adding vectors
    By sdchem in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2003, 11:34 AM