Thread: vector push_back array

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55

    vector push_back array

    hi,

    I've recently changed some code in my program from a multidimensional array to a vector, and I'm wondering if/how it's possible to initialise a vector array.
    i.e.
    vector<int> MyVector;
    MyVector.push_back(int1,int2,int3,int4,int5);

    old code:
    Code:
    	//header
    	extern int LIST[][5];
    
    	//source
    	int LIST[][5] = 
    	{
    		{24,3,2,5,16};
    		{24,2,5,16,3};
    		{n1,n2,n3,n4,n5};
    	};

    //new code
    Code:
    	//header
    	extern vector<int> LIST[5];
    
    	//source
    	void Create_List()
    	{
    		FillList(24,3,2,5,16);
    		FillList(24,2,5,16,3);
    		FillList(n1,n2,n3,n4,n5);
    	}
    
    	bool FillList(int i1, int i2, int i3, int i4, int i5)
    	{
        		LIST[0].push_back(i1);
        		LIST[1].push_back(i2);
        		LIST[2].push_back(i3);
        		LIST[3].push_back(i4);
        		LIST[4].push_back(i5);
    
    		return true;
    	}


    At the moment, I use FillList() function to populate the vector. Just wondering if there's a better way to do this other than calling another function each time ?


    Thanks!

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    If you also have the array, you can pass that to the vector constructor so that the vector gets initialized with the values in the array.
    Code:
    vector<int> list(list_array, list_array + sizeof(list_array) / sizeof(int));

  3. #3
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    Thanks for the reply!
    Was a little stuck but managed to figure it out from your post.

    Code:
        vector<int> iVec(*iListArray,*iListArray+sizeof(iListArray) / sizeof(int));

    How would one access then a particular element within vector, like you do an array given the above initialisation?
    i.e.
    Code:
    int LIST[][5] = 
    {
        {1,2,3,4,5},
        {6,7,8,9,10},
    };
    
    cout << LIST[2][3];
    cout << iVec[i][j] doesn't work with the above particular setup?


    Thanks!

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Firstly, iVec would need to be either an array of vectors, or a vector of vectors (otherwise the two-dimensional syntax is invalid).

    Each element of iVec would then be a vector (which can each be initialised using an approach like that described by MemLoop). You won't be able to avoid either some loop construct and/or usage of some temporary array (or vector) of ints.

    When you start going to multiple dimensions with vectors, things become (literally) recursive.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM