[NEWBIE] HowTo access complex STL-Vector structures...

M

Markus Kern

Hi!

I have defined a structure:

struct Coor3d
{
float x,y,z;
};

This is supposed to be stored in a 2d-vector:

------------
typedef vector< vector<Coor3d> > DCDStep;
DCDStep vDCDStep;
-------------

since I will have multiple sets of those coordinates.

However, here my trouble starts. In order to
access 16nth sets x(22), I have done this (which surely
is a newbies way to do so, suggestions welcome)

------------
typedef vector<Coor3d>::iterator Ic3d;
Ic3d ic3d(vDCDStep[16].begin());
cout << (ic3d+22)->x;
------------

Nevertheless, it works, but here comes
where I'm lost: I need to introduce another
vector, in which vDCDStep is stored, so I
did this:

-----------
typedef vector<DCDStep> DCDFile;
DCDFile vDCDFile;
DCDFile::iterator iDCDFile;
-----------

vDCDFile.push_back(vDCDStep) works, but how
can I access x,y,z, now?


This whole thing looks cruel, anyway. STL was
supposed to make life easier, however, *this*
has not become particulary easy compared to
C's array-solutions.

Anyone any idea where my thinking has gone wrong?

Thank to all for anwers

M.K.
 
V

Victor Bazarov

Markus Kern said:
I have defined a structure:

struct Coor3d
{
float x,y,z;
};

This is supposed to be stored in a 2d-vector:

------------
typedef vector< vector<Coor3d> > DCDStep;
DCDStep vDCDStep;
-------------

since I will have multiple sets of those coordinates.

However, here my trouble starts. In order to
access 16nth sets x(22), I have done this (which surely
is a newbies way to do so, suggestions welcome)

------------
typedef vector<Coor3d>::iterator Ic3d;
Ic3d ic3d(vDCDStep[16].begin());
cout << (ic3d+22)->x;
------------

That's perfectly fine as long as your container is a vector.
For some containers their iterators do not have operator+(int)
defined for them.
Nevertheless, it works, but here comes
where I'm lost: I need to introduce another
vector, in which vDCDStep is stored, so I
did this:

iDCDFile = vDCDFile.begin();
// now each (*iDCDFile) is a DCDStep:
DCDStep & step = *iDCDFile;
// use 'step' here like you did 'vDCDStep' before
// or
cout << step[16][22].x;
This whole thing looks cruel, anyway. STL was
supposed to make life easier, however, *this*
has not become particulary easy compared to
C's array-solutions.

Really? Did you have to allocate your memory yourself?
Did you have to check bounds (if you used 'at')?
Come on, now...
Anyone any idea where my thinking has gone wrong?

As soon as you stop comparing standard library containers
and their iterators to arrays and pointers and begin just
using them, you'll be alright.

V
 
M

Markus Kern

Hi!

Thank you very much for that quick and very useful response!
As soon as you stop comparing standard library containers
and their iterators to arrays and pointers and begin just
using them, you'll be alright.

Yes, you may be right. It's hard to get used to this
way after years of using arrays. And:
cout << step[16][22].x;

indeed *is* easy, I just didn't get this.

Thanks again and cheers

M.K.
 
D

Donovan Rebbechi

Hi!

I have defined a structure:

struct Coor3d
{
float x,y,z;
};

This is supposed to be stored in a 2d-vector:

------------
typedef vector< vector<Coor3d> > DCDStep;
DCDStep vDCDStep;
-------------

since I will have multiple sets of those coordinates.

However, here my trouble starts. In order to
access 16nth sets x(22), I have done this (which surely
is a newbies way to do so, suggestions welcome)

------------
typedef vector<Coor3d>::iterator Ic3d;
Ic3d ic3d(vDCDStep[16].begin());
cout << (ic3d+22)->x;
------------

Nevertheless, it works, but here comes
where I'm lost: I need to introduce another
vector, in which vDCDStep is stored, so I
did this:

So at this stage, you have a vector of vectors, and a vector of
handles (iterators) into those vectors ... that's some complexity.
There are invariants that need to be maintained (for example, if
an element gets removed, all the iterators are invalidated). The
solution is probably to make a fairly small class to manage this
complexity.

each element of vCDFile is a DCDStep, which is a
vector of vectors, so you just unpack it all.

DCDStep& s = vDCDFile [0] ;
Coord& c = x[16] [22];
c.x; // x

or if you want to use range checking:

Coord c = vDCDStep.at(0).at(16).at(22);

but let's put it in a function, with named parameters,

Coord& getCoord( DCDFile& vDCDStep, int stepno, int listno, int itemno)
{
return vDCDStep.at(stepno).at(listno).at(itemno);
}

Coord c = getCoord(vDCDStep,0,16,22);
std::cout << c.x << "," << c.y << "," << c.z << std::endl;
This whole thing looks cruel, anyway. STL was
supposed to make life easier, however, *this*
has not become particulary easy compared to
C's array-solutions.

The array solutions would not solve any of the complexity. If you think
they'd be any easier, try to correctly implement a dynamic 3 dimensional
array in C.

Cheers,
 
D

Donovan Rebbechi

So at this stage, you have a vector of vectors, and a vector of
handles (iterators) into those vectors ... that's some complexity.
There are invariants that need to be maintained (for example, if
an element gets removed, all the iterators are invalidated). The
solution is probably to make a fairly small class to manage this
complexity.

Doh! This paragraph was based on a misreading, thought I'd deleted it until
after the post. Never mind (though the idea of using a small class
to manage this still makes sense)

Cheers,
 
M

Markus Kern

Am Mon, 21 Feb 2005 01:00:18 +0000 schrieb Donovan Rebbechi:

Dear Donovan,

thank you for your explanations that made things clearer to me.
The array solutions would not solve any of the complexity. If you think
they'd be any easier, try to correctly implement a dynamic 3 dimensional
array in C.

As I already responsed Victor, that remark was
due to my lack of understandig container-classes.
However, to answer your questions: I already *did*
implement dynamic 3-(and more) dimensional arrays
in C. You're right, it was hard in the beginning,
but now I got used to it and I feel now that I'm
getting used to C++ I'm just there where I was
when I was building those malloc()-based arrays.

Cheers

M.K.
 
M

Markus Kern

Am Sun, 20 Feb 2005 15:15:07 -0500 schrieb Victor Bazarov:
That's perfectly fine as long as your container is a vector.
For some containers their iterators do not have operator+(int)
defined for them.


BWT, what would have been the appropiate way do deal with
those containers in such a case?

Sincerley

M.K.
 
V

Victor Bazarov

Markus said:
Am Sun, 20 Feb 2005 15:15:07 -0500 schrieb Victor Bazarov:





BWT, what would have been the appropiate way do deal with
those containers in such a case?

You could use 'advance' function, you could probably use ++ so
many times... However, since the use indicates the need in what
is known as "random access" container, you're better off just
sticking to vector or deque. For more good information on how
to pick your containers, see "Effective STL" by Meyers, or "The
C++ Standard Library" by Josuttis. I am sure other books cover
that topic to some degree as well.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top