Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 09-11-2005, 11:30 AM   #1 (permalink)
xytor
Registered User
 
Join Date: Sep 2005
Posts: 4
xytor is on a distinguished road
Creating and Using a 2 dimensional Vector (vector of vectors)

I am having trouble understanding how to create a 2 dimentional vector (a vector of vectors) and have it act like I want it to.

Code:
void test2dVector(){
 vector <vector <int> > v; //*two dimensions
 v.push_back(vector <int>()); //*create v[0]
 v.push_back(vector <int>()); //*create v[1]
 v[0].push_back(15); //*assign v[0][0]
 v[1].push_back(16); //*assign v[1][0]


	    for(size_t i=0;i<v.size();i++)
  {
   for (size_t j=0;j<v[i].size();j++)
   {
       cout<<v[i][j]<<" "; //I am not sure that this will work! Will it?
}
   cout<<"\n";
}



}
In this code, I try to create and use a 2-d vector.
I still do not understand how to make it act like a real 2-d array, meaning, creating an actual grid-like structure, as opposed to this, which only covers one column.

So once and for all, can somebody FULLY explain how to do this, completely?
xytor is offline   Reply With Quote
Old 09-11-2005, 07:49 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Nothing beats some code. and besides, you should check out:
C++ Multi Dimensional Array using Vectors.
Code:
#include <iostream>
#include <vector>
#include <cstdlib>

using std::size_t;
using std::vector;
int main()
{  
  vector<vector<int> > Matrix;
  
  //Create the 2x2 matrix.
  size_t rows = 2;
  size_t cols = 2;  
  // 1: set the number of rows.
  Matrix.resize(rows);
  // 2: set the number of columns per row.
  for(size_t i = 0; i < rows; ++i)
  {
    Matrix[i].resize(cols);
  }
  
  Matrix[0][0] = 1;
  Matrix[0][1] = 2;
  Matrix[1][0] = 3;
  Matrix[1][1] = 4;
  
  //Demo of row/column workings.
  for(size_t i = 0; i < rows; ++i)
  {
    for(size_t j = 0; j < cols; ++j)
    {
      std::cout <<"["<<i<<"]"<<"["<<j<<"] = " <<Matrix[i][j]<<std::endl;
    }
  }  
  
  return 0;
}
__________________
Valmont is offline   Reply With Quote
Old 09-12-2005, 03:35 AM   #3 (permalink)
xytor
Registered User
 
Join Date: Sep 2005
Posts: 4
xytor is on a distinguished road
OOh, so the resize function of the vector is needed.
Thats what I didn't know.
Thank you so much!!
xytor is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:57 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting