473,320 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

vector/array advice please

csx
Hi all,
I have the following code, which at the moment is a vector of arrays. But
unfortunately, it doesnt do what I want.
Basically, 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);

Can use levels.length() // to return the size of the outer array - i.e. 5

This is really easy in Java, its like an array of arrays but with the
levels.length() feature of a vector, and I want the same in C++. Also, you
dont have to specify the size of the array beforehand.

I want to be able to specify an array by its corresponding array index. So,
if i wanted array (4, 5, 'w', 1) this would be accessed like
nodes temp = Levels[3].
But I also want to be easily able to call levels.length() so that I can get
the size of the array. In this case, 5.

I implemented mine as a vector of arrays, so that I could use
"nodes.size()", but now I cant refer to them via index. i.e. node[4].
If I had implemented it as an array of arrays, the index would work,
node[4]. but I wouldn't be able to call a method like nodes.size().

Can someone please provide some advice
Thanks in advance!!




#include "stdafx.h"
#include <vector>
#include <iostream> // we have to use STD::cout with this
#include <string>
using namespace std; // Now don't need to specify the namespace, i.e.
STD::COUT

struct node
{
int key, row, parent;
char description;
node( int k=0, int r=0, char d='temp', int p=0 )
: key(k), row(r), description(d), parent(p) {}
};

int _tmain(int argc, _TCHAR* argv[])
{
vector<node> nodes;
nodes.push_back( node(1,4,'G',4) ); // 0
nodes.push_back( node(2,2,'H',2) ); // 1
nodes.push_back( node(3,6,'J',2) ); // 2
return 0;
}


Jul 19 '05 #1
4 2463
csx escribió:
I implemented mine as a vector of arrays, so that I could use
"nodes.size()", but now I cant refer to them via index. i.e. node[4].
If I had implemented it as an array of arrays, the index would work,
node[4]. but I wouldn't be able to call a method like nodes.size().


Use a vector of vectors.

Regards.
Jul 19 '05 #2

"csx" <cs*@ms.com> wrote in message news:bk**********@hercules.btinternet.com...
Levels[0] = new makearray(1,1,'b',1) Pretend we're not javaheads here and explain what makearray does.
I implemented mine as a vector of arrays, so that I could use
"nodes.size()", but now I cant refer to them via index. i.e. node[4].
If I had implemented it as an array of arrays, the index would work,
node[4]. but I wouldn't be able to call a method like nodes.size().


Can't refer to what by index. Sure you can refer to nodes[4] (it will
even work if there were that many items in the array). The only thing
that won't work is accessing them by index if they do not exist
(perhaps java arrays auto extend, but C++ vectors do not).

vector<node> nodes;
node[0] = node(2,3,'d',3); // illegal, nodes has zero size.

nodes.push_back( node(1,4,'G',4) ); // 0
node[0] = node(5,6,'7',8); // legal the push_back increased nodes.size() to 1.

nodes.resize(5);
node[2] = node(7,8,'9',10); // legal, resize increased the size.
Jul 19 '05 #3
csx
hi thanks
but, how would this be coded so i could index a vector via
vector[4] as in the code posted earlier?

"Julián Albo" <JU********@terra.es> wrote in message
news:3F***************@terra.es...
csx escribió:
I implemented mine as a vector of arrays, so that I could use
"nodes.size()", but now I cant refer to them via index. i.e. node[4].
If I had implemented it as an array of arrays, the index would work,
node[4]. but I wouldn't be able to call a method like nodes.size().


Use a vector of vectors.

Regards.
Jul 19 '05 #4
"csx" <cs*@ms.com> wrote in message
news:bk**********@hercules.btinternet.com...
hi thanks
but, how would this be coded so i could index a vector via
vector[4] as in the code posted earlier?


First resist top-posting.

Second, you usually construct a vector using push_back.

std::vector<std::string> v;
v.push_back( "a " );
v.push_back( "example " );
v.push_back( "simple " );

and then you use the data using operator[].

example:
std::cout << v[0] << v[2] << v[1] << std::endl;
// prints "a simple example "

Alternatively, use std::map.

std::map<int, std::string> v;
v[0] = "a ";
v[0] = "a ";

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
19
by: Carlo Milanesi | last post by:
Mathematically speaking, a 'vector' is something you can add to another vector and multiply by a number. But in C++, the following code is illegal: std::vector<double> v1(3), v2(3); v1 + v2; //...
4
by: Oliver Gebele | last post by:
/* OK, after years i'm still more into C; but i already do understand some C++. And there are still many things about the STL which i do not know... I try to put 8-character-arrays in a...
11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
5
by: Toto | last post by:
Hello, I've got a problem with the redefinition of operator in C++. My intent is to create a TVector that internally work with __int64, with an external interface of double in order to optimize...
8
by: Michael | last post by:
Hi, I could use vector to get an array to random_shuffle it. Is it possible to define an array to random_shuffle it? I tried but it did not work. Can I use array instead of vector to use...
32
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a...
2
by: Kazzie | last post by:
I have a structure struct Rulestrc { char* rulen; vector<char*>v6; char* resp; }; I then create an array of structures - Rulestrc tp. However the content of tp is constantly changing...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.