#### Archived

This topic is now archived and is closed to further replies.

# vector of an array?

This topic is 5564 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I have the following problem. I need to make a vector of an array (i think). And be able to call the Size(). For instance: struct node { int num1; int num2; char desc; int num3; }; // key, row, Description, Parent node treeNode[8] ={ { 1, 1, 'A', 0,}, { 2, 2, 'B', 1,}, { 3, 2, 'C', 1,}, { 4, 3, 'D', 3,}, { 5, 3, 'E', 3,}, { 6, 4, 'F', 4,}, { 7, 4, 'G', 4,}, { 8, 4, 'H', 4,}, }; int tmain(int argc, _TCHAR* argv[]) { // I need to be able to determine the length of the array //treeNode if it was created dynamically. i.e. treeNode.size(); // therefore, am I correct in saying I woudl have to use a //vector? like this. vector nodes; vector.push_back(treeNode[1]) vector.push_back(treeNode[2]) .. vector.push_back(treeNode[7]) cout << nodes.size() << endl; // Is there a better day then creating the array elements, // then a vector of each array. return 0; } Can someone suggest the best way to implement this. Many thanks in advance!! [edited by - csxpcm on September 23, 2003 8:45:13 AM]

##### Share on other sites
Basically I need a vector of an array, that I can call size on. Like so..

Levels[0] = new array(1,1,"E",1)
Levels[1] = new array(1,3,"B",1)
Levels[2] = new array(1,5,"F",2)
Levels[3] = new array(1,4,"H",2)

then I can call
Levels.Size()// whereby levels is a vector.

What would be the best way to implement this?

[edited by - csxpcm on September 23, 2003 8:48:25 AM]

##### Share on other sites
So do you only need the size? Or do you want to put the data into vector for other purposes also? When putting the elements one by one, you do it "size" times, so actually putting them is irrelevant. To get the amount of items in array treeNode, this should work: sizeof(treeNode) / sizeof(node).

##### Share on other sites
Hi, thanks for the suggestion.
What I (think) would be the best is something like this:

A vector (dynamicaly created) of arrays of type node.

struct node
{
int key;
int row;
char desc;
int parent;
};

// in main
----------------------------------
vector nodes;
nodes.push_back( node{7,4,''G'',4,} );
nodes.push_back( node{3,2,''H'',2,} );

nodes.size()

----------------------------------

However, this doesn''t work. Is it not possible to create an array without a contructor?

##### Share on other sites
Hello csxpcm,

node{7,4,''G'',4,} I think will not work what does n{7,4,''G'',4,} supose to do? but maybe somethinglike this would.

vector.push_back(node n={7,4,''G'',4,}) this creates a temp n on the stack and then is push unto the vector by the compiler generated copy constructor.
or maybe
vector.push_back({7,4,''G'',4,}) might but I dought it. This could maybe bypass the temp node on the stack and go stright to compiler generated constructor but I dought it.

best bet is just make a constructor for the struct.
when in dought create default, copy constructor and assignmet.

Lord Bart

##### Share on other sites
Thanks!
Ive added a constructor, but it still doesnt do something that I need. For instance the code below:

#include "stdafx.h"#include <vector>#include <iostream>#include <string>using namespace std;struct node{    int key, row, description, parent;    node( int k=0, int r=0, char d='a', int p=0 )     : key(k), row(r), description(d), parent(p)  {}};// need to pass in a particular nodeint countLeafNodes(node& parentNode) {  cout << parentNode.key;  return 1;}int _tmain(int argc, _TCHAR* argv[]){  vector<node> nodes;  nodes.push_back( node(1,4,'G',4) );  // 1  nodes.push_back( node(2,2,'H',2) );  // 2  nodes.push_back( node(3,6,'J',2) );  // 3  countLeafNodes(node[2]);  // this wouldnt work.  cout << nodes.size() << endl;   return 0;}

I then need to be able to pass a particular node to the function countLeafNodes.
In Java, the following could be specified. Levels could be dynamically increased. I could use Levels.Length() to get the size of the array, but also, I can specify an actual array element. I need this same sort of functionality.

var Levels = new Array()
Levels[0] = new makearray(1,4,'G',4)
Levels[1] = new makearray(2,2,'H',2)
Levels[2] = new makearray(3,6,'J',2)

so here i could use
countLeafNodes(node[2]); // and this would now work.

Does anyone have any recommendations please?

[edited by - csxpcm on September 23, 2003 10:11:29 AM]

##### Share on other sites
Is this a typo or is this what you actual have
countLeafNodes(node[2]);

should be

countLeafNodes(nodes[2]);

Lord Bart

[edited by - lord bart on September 23, 2003 10:23:26 AM]

##### Share on other sites
Thanks 'Lord Bart'!
but, assuming I add several nodes then delete some, passing
countLeafNodes(nodes[1]);
will at first refer to
node(2,2,'H',2) ); // 1
but might not after a few insertion/deletes.

How could I do something like:
nodes[0].push_back( node(1,4,'G',4) );
nodes[1].push_back( node(2,2,'H',2) );
nodes[2].push_back( node(3,6,'J',2) );

then when i pass
countLeafNodes(nodes[1]);
I know it refers to 'node(2,2,'H',2)'

[edited by - csxpcm on September 23, 2003 10:42:50 AM]

##### Share on other sites
Ok so what you want is a vector of vectors?

Were you have a main vector that has for each entry has a vector of nodes in it.

just do vector< vector > nodes;

But now you need to think is this what you want to do if your going to do alot of inserts, deletes and to which vector.

If the inner vector (node vector) does not change alot but the outer one might have a node vector remove, inserted, added then I would not use a vector here, use a list or a map or change it to a vector of pointer to a vector of nodes vector< vector < node >* > nodes.
Inserts into a vector are costly as is removing any were but the end of a vector.

Not sure what you realy want to do?

Lord Bart

[edited by - lord bart on September 23, 2003 11:17:01 AM]

##### Share on other sites
Thanks Bart
Ok, what would you recommend if neither were to change much?
For instance, here is an example in Java that I want to work in C++.

var Levels = new Array()

Levels[0] = new makearray(1,1,'b',1);
Levels[1] = new makearray(2,2,'g',1);
Levels[2] = new makearray(3,5,'h',1);
Levels[3] = new makearray(4,5,'w',1);
Levels[4] = new makearray(5,3,'p',1);

The inner array i.e.
new makearray(1, 1, 'b', 1); // Will not change!

the outer one will not change either.
but, I want to be able to call
levels.size().

- In Java, the above code appears to be an array, but you dont have to specify a size. Then depending on how many elements you have added, you can call levels.length(), to find out.

This is really easy in Java, its like an array of arrays, and I want the same in C++. I need to be able to call something like levels.length() on it. But this is where im stuck.

[edited by - csxpcm on September 23, 2003 11:43:38 AM]

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013652
×