Thread: Vector Of Vectors

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    55

    Vector Of Vectors

    Hi, I am Trying to create a vector of vectors

    I have managed to create the vector structure
    but now i need to add elements to it with a particular sequence

    for example if i call the function, with a variable number (which = 5)

    i want the vector to be able to go through and create a vector structure like
    vector vector 0 = 1, 0, 0, 0, 0, 0
    vector vector 1 = 2, 0, 0, 0, 0, 0
    vector vector 2 = 3, 0, 0, 0, 0, 0
    vector vector 3 = 4, 0, 0, 0, 0, 0
    vector vector 4 = 5, 0, 0, 0, 0, 0

    i have tried many ways but cannot seem to assign the correct values

    Code:
    vector<vector<int> > MYVEC; // create vector
    
    void Initialise(vector<vector<int> > &MYVEC, int num) // function to initialise vector
    {
            int v = 1;	// used to assign 1, 2, 3 ...
    	int k = 0;	// used always assigning 0 to other elements
    
    	for ( int i = 0; i < num; i++ ) 
    	{
    		MYVEC.push_back ( vector<int>() );
    		
                    MYVEC[i].push_back(i);
    
    		for ( int j = 0; j < numVertices; j++ )
    		{
    			MYVEC[i].push_back(i);
    			MYVEC[i][j] = 0;
    		}
    	}
    }
    This dosnt work and just assigns 0 to all elements,
    so is like:
    vector vector 0 = 0, 0, 0, 0, 0, 0
    vector vector 1 = 0, 0, 0, 0, 0, 0
    vector vector 2 = 0, 0, 0, 0, 0, 0
    vector vector 3 = 0, 0, 0, 0, 0, 0
    vector vector 4 = 0, 0, 0, 0, 0, 0

    i need the first element of each vector in the big vector to be go up from 1, 2 ... etc

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One way:
    Code:
    void Initialise(vector<vector<int> >& myvec, int num)
    {
        vector<int> temp(6);
        for (int i = 0; i < num; ++i)
        {
            temp[0] = i + 1;
            myvec.push_back(temp);
        }
    }
    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
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi, thanks i have done it using

    Code:
    	int k = 0;	// used to assign 0 to other elements
    
    	for ( int i = 0; i < num; i++ ) 
    	{
    		MYVEC.push_back ( vector<int>() );	
    
    		for ( int j = 0; j < num; j++ )
    		{
    			MYVEC[i].push_back(i);	
    
    			MYVEC[i][0] = (i+1);	// set first elem vector to 1...+
    			MYVEC[i][j] = (k);		// set second elem...+ of vector to 0
    		}
    }
    this seems to do what i need and gives me:
    vector vector 0 = 1, 0, 0, 0, 0, 0
    vector vector 1 = 2, 0, 0, 0, 0, 0
    vector vector 2 = 3, 0, 0, 0, 0, 0
    vector vector 3 = 4, 0, 0, 0, 0, 0
    vector vector 4 = 5, 0, 0, 0, 0, 0


    now if i need to search through the vector, for example to find where a certain number is, e.g. 1
    i want it to return 1 location = 0 (as first elem of vec vec 0 is 1)
    e.g. 2
    i want it to return 2 location = 1 (as first elem of vec vec 1 is 2)

    how would i do this?
    thanks

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi, ignore the last post I have done it.
    Thanks =)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please stop naming your vector MYVEC. Conventionally, names that are fully capitalised are used for macros, and maybe also constants.

    Quote Originally Posted by Khadafi
    this seems to do what i need and gives me:
    vector vector 0 = 1, 0, 0, 0, 0, 0
    vector vector 1 = 2, 0, 0, 0, 0, 0
    vector vector 2 = 3, 0, 0, 0, 0, 0
    vector vector 3 = 4, 0, 0, 0, 0, 0
    vector vector 4 = 5, 0, 0, 0, 0, 0
    It gives me:
    Code:
    1 0 0 0 0
    2 0 0 0 0
    3 0 0 0 0
    4 0 0 0 0
    5 0 0 0 0
    Spot the difference. Furthermore, it is inefficient as you are repeatedly assigning to MYVEC[i][0] and then you don't have to assign k to MYVEC[i][j] when you could have just pushed k. Do you understand my example?

    Quote Originally Posted by Khadafi
    now if i need to search through the vector, for example to find where a certain number is, e.g. 1
    i want it to return 1 location = 0 (as first elem of vec vec 0 is 1)
    e.g. 2
    i want it to return 2 location = 1 (as first elem of vec vec 1 is 2)

    how would i do this?
    What have you tried?
    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

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi, thanks i understand your example now and it works =)
    sorry about the MYVEC, it was just an example to show you what i mean.

    Actually would i be able to change the vector so i only have
    vector vector 0 = 1
    vector vector 1 = 2
    vector vector 2 = 3
    vector vector 3 = 4
    vector vector 4 = 5
    vector vector 5 = 6
    as the 0 are not really needed and i can pushback data to each vector vector later when needed?

    I've got the search this working so thats fine.
    Last edited by Khadafi; 12-30-2011 at 11:15 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Khadafi
    Actually would i be able to change the vector so i only have
    vector vector 0 = 1
    vector vector 1 = 2
    vector vector 2 = 3
    vector vector 3 = 4
    vector vector 4 = 5
    vector vector 5 = 6
    as the 0 are not really needed
    Yes, and that makes it even easier, e.g.,
    Code:
    void Initialise(vector<vector<int> >& myvec, int num)
    {
        for (int i = 0; i < num; ++i)
        {
            myvec.push_back(vector<int>(1, i + 1));
        }
    }
    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

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Thank that works =)

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Khadafi
    Hi, that dosnt seem to work
    it only assigns 1 and then throws an error?
    It seems you did something wrong

    (Hint: if you don't want a vague assessment, don't give vague feedback.)
    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

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    thanks sorry i fixed it.

    I now need to be able to merge 2 vectors inside the big vector

    e.g. i have
    vector vector 0 = 1
    vector vector 1 = 2
    vector vector 2 = 3
    vector vector 3 = 4
    vector vector 4 = 5
    vector vector 5 = 6

    -----------------------------------------

    i may need to merge vector vector 0 and 1
    to get vector vector 0 = 1, 2
    vector vector 1 = nothing

    then may need to merge vector vector 2 and 3
    to get vector vector 2 = 3, 4
    vector vector 3 = nothing

    then may need to merge vector vector 0 and 2
    to get vector vector 0 = 1,2,3,4
    vector vector 2 = nothing

    could you please explain how this could be done
    would i just have to push each vector the the vector

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi, I am trying this

    my vector is:
    vector vector 0 = 1
    vector vector 1 = 2
    vector vector 2 = 3
    vector vector 3 = 4
    vector vector 4 = 5
    vector vector 5 = 6

    for example if:
    int j = 1;
    int k = 2;

    i need to merge vector vector 0 and vector vector 1

    i am trying

    vector[j-1].resize(vector[j-1].size() + vector[k-1].size());
    merge(vector[j-1].begin(), vector[j-1].end(), vector[k-1].begin(), vector[k-1].end(), vector[j-1].begin());

    but it dosnt work

    it gives an error "sequence not ordered"
    Last edited by Khadafi; 12-30-2011 at 11:54 AM.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi, dont worry i figured it out.
    thanks for all your help =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector of Vectors
    By Zosden in forum C++ Programming
    Replies: 6
    Last Post: 10-05-2008, 01:30 AM
  2. vector of vectors
    By xddxogm3 in forum C++ Programming
    Replies: 11
    Last Post: 05-04-2007, 10:25 AM
  3. partition a vector of vectors
    By acosgaya in forum C++ Programming
    Replies: 1
    Last Post: 10-06-2006, 10:20 AM
  4. Vector of Vectors
    By swanley007 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 04:33 PM
  5. vector of vectors
    By Magos in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2004, 03:46 AM