GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Feb-2004, 05:55
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough

2D vector


anyone know how to create a 2D vector,
can give an example how to create it,
and how to inside the data inside the 2D vector
like:

1 2 3
4 5 6
7 8 9 3 column and 3 row.

thx :-)
__________________
challenges are make life interesting,
overcome them is make life meaningful.
  #2  
Old 15-Feb-2004, 07:23
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Tay,

Take a look at this thread. Garth gives a great explanation on 2d arrays.
  #3  
Old 15-Feb-2004, 07:36
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
2D array i know

but i want know how to create 2D vector and also want know how to insert data inside the 2D vector.

we need include <vector>, for example
CPP / C++ / C Code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
  vector<int> v;
  v.push_back(4);
  v.push_back(2);
  v.push_back(1);
  v.push_back(6);
  v.push_back(8);
  v.push_back(3);

  for (int i=0;i<6;i++)
     cout << v[i] << " "; 
}
but i no idea make it to 2D vector
can u help me dsmith
Last edited by dsmith : 15-Feb-2004 at 08:09. Reason: Added c syntax highlighting.
  #4  
Old 15-Feb-2004, 08:08
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by tay
can u help me dsmith

Tay, you have found my major weakness! I hope that someone else can help you, because I am illiterate in most of the C++ stuff.

If no one else posts, I will look into it, but I really know nothing about this...
  #5  
Old 16-Feb-2004, 08:56
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
i thought c also got vector
__________________
challenges are make life interesting,
overcome them is make life meaningful.
  #6  
Old 16-Feb-2004, 12:48
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
You can create types that contain 2 ints, like this:

CPP / C++ / C Code:
typedef struct{
int x;
int y;
} coord;

This is declared as:
CPP / C++ / C Code:
coord point;
point.x = 5;
point.y = 7;

This is a new type, which you can tell the vector class to use. See if you can figure out how you can use this via vector template.

This is C, in C++ I'd create a coord class with 2 public variables x & y (or even better, public functions which alter the private variables) which can also be used in the vector class.

GF
  #7  
Old 16-Feb-2004, 21:13
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
let say i want make 50 x 50 square
what to do it
mean column 50 , row also 50
__________________
challenges are make life interesting,
overcome them is make life meaningful.
  #8  
Old 17-Feb-2004, 11:18
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Oh, that's a matrix. There is no premade matrix class sadly (although there's plenty on the net). But they're not hard to code, if you're well up on pointers. www.gidforums.com.

This is purely C:
CPP / C++ / C Code:
double *ptr;
int N = 10; // => N*N matrix will be created.
ptr = calloc(N*N, sizeof(double) ); //memory allocated & zeroed
if(ptr == NULL){  //whoopsee
	printf("Memory allocation failure.");
	exit(1);
}

//To set a value (i,j) in the array to 18.4
*(ptr + (i*N) + j) = 18.4;

C++ has a far nicer way of playing with matrices. You can create a Matrix object, and tell it how big to be, and set it's values easily. But it requires writing all that yourself, but it's not too tough.
GF
Last edited by Garth Farley : 17-Feb-2004 at 11:56. Reason: Left brain at home
  #9  
Old 17-Feb-2004, 11:21
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
However, the code I've just posted doesn't do what vector can. Vector is a linked list, where you can add new links all the time. Above once you've called calloc, a chunk of memory is allocated. This won't expand dynamically without shedloads of nasty stuff, and isn't worth it. So you#ve to be sure how big you want the array before you call calloc.

Hmmm, I'm wondering if it's possible to make a vector of vectors?
GF
  #10  
Old 05-May-2004, 15:56
kzutter kzutter is offline
New Member
 
Join Date: May 2004
Posts: 3
kzutter will become famous soon enough

2D Vector Sample


I see this thread is a little old, and you have probably already solved your problem, but this may help someone.
CPP / C++ / C Code:
#include <iostream>
#include <vector>
using namespace std;

int main( )
{
  // create a vector of vectors
  vector< vector< int> > v2d;

  // just a temp to populate v2d
  vector< int> row;

  // populate v2d
  for(int i = 1; i<10; i++){
    row.push_back(i);
    if( i%3 == 0){
      v2d.push_back(row);
      row.clear();
    }
  }
  // output
  for(int i=0; i<3; i++){
    for(int j=0; j<3; j++){
      cout << v2d[i][j]; // note array style indexing, but be careful! - no bounds checking
    }
    cout << endl;
  }


}


Quote:
Originally Posted by tay
anyone know how to create a 2D vector,
can give an example how to create it,
and how to inside the data inside the 2D vector
like:

1 2 3
4 5 6
7 8 9 3 column and 3 row.

thx :-)
Last edited by kzutter : 05-May-2004 at 16:04. Reason: C++ tags
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why error? eye C++ Forum 0 18-Jan-2004 03:25
'stuct' instead of just 'int' mediaware2002 C++ Forum 1 08-Jan-2004 09:33
vector prob nattylife C++ Forum 1 15-Nov-2003 07:41
vector sorting gotnospoon C++ Forum 0 10-Nov-2003 15:26
C++, Matrix Max Length calculus87 C++ Forum 2 15-Sep-2003 07:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:53.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.