Vector Of Vectors

This is a discussion on Vector Of Vectors within the C++ Programming forums, part of the General Programming Boards category; Hi, I am Trying to create a vector of vectors I have managed to create the vector structure but now ...

  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
    22,274
    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);
        }
    }
    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
    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
    22,274
    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?
    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

  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
    22,274
    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));
        }
    }
    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

  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
    22,274
    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.)
    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

  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, 02:30 AM
  2. vector of vectors
    By xddxogm3 in forum C++ Programming
    Replies: 11
    Last Post: 05-04-2007, 11:25 AM
  3. partition a vector of vectors
    By acosgaya in forum C++ Programming
    Replies: 1
    Last Post: 10-06-2006, 11: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

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