2D vectors array.......

This is a discussion on 2D vectors array....... within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I need to make a constructor that initialize a 2d vector array by zeros or nulls...... how can ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    6

    2D vectors array.......

    Hello everyone,
    I need to make a constructor that initialize a 2d vector array by zeros or nulls......
    how can i do that ._.
    tried nested loops like in 2d arrays but failed
    should i use push_back?or is there a way i can do this without it?

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,231
    What have you tried?

    Soma

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25
    Hello, as phantomotap says: tried anything? Show the code.

    std::vector can be initialized just when you are creating it, like this:
    Code:
    //Creates 2D vector of size 10 and the 0 in the line below defines the default size.
    std::vector<std::vector <int>> Storage(10, std::vector<int>(10, 0));
    If you define a vector like this (with no default value):
    std::vector<std::vector <int>> Storage(10, std::vector<int>(10));
    Then initialize it through a loop with another inner loop:
    Code:
    int initvalue = 0;
    	for(int i = 0; i < 10; i++)
    	{
    		for(int ii =0; ii < 10; ii++)
    		{
    			Storage[ii][i] = initvalue;	
    		}
    	}
    Is this all you are trying to do? You can use push.back if you want.
    Or are you trying to create a 2D vector by yourself, something along these lines:
    Code:
    class Vec2D
    {
        private:
          int v[2];
        public:
          Vec2D(int x, int y);
          //etc... 
    }
    Last edited by ManyTimes; 05-30-2011 at 01:08 PM.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    6
    thx alot on the replies
    this is the last thing i did and it doesn't show any errors anymore but i don't know is this even correct or not -_-" "new to programming"

    Code:
    protected:
    	char w;
    	int x,y;
    	int i,j;
    	vector<vector<char> > Vec(int i, vector<char>(int i,int j));
    public:
    	rand_m_tst()
    	{
    		w='$';
    		x=0;
    		y=0;
    		i=20;
    		j=0;
    		vector<vector<char>> Vec(i,vector<char>(i,j));
    		for(int f=0;f<20;f++)
    		{
    			for(int j=0;j<20;j++)
    				Vec[f][j]=NULL;
    		}
    	};
    the 2d vector in protected area is useless now right? -_-"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of vectors?
    By rodrigorules in forum C++ Programming
    Replies: 10
    Last Post: 02-11-2010, 04:05 AM
  2. Array of Vectors amd other STL questions
    By kolistivra in forum C++ Programming
    Replies: 16
    Last Post: 04-12-2007, 09:11 AM
  3. dynamic array of vectors
    By axr0284 in forum C++ Programming
    Replies: 8
    Last Post: 02-25-2006, 11:01 PM
  4. Array of vectors
    By earth_angel in forum C++ Programming
    Replies: 7
    Last Post: 07-12-2005, 09:46 AM
  5. array of vectors
    By bearcat19 in forum C++ Programming
    Replies: 1
    Last Post: 02-23-2003, 07:13 PM

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