If been trying to use the STL list with my own class, to make up nodes.
In other words, what I am trying to do is that instead of just pushing in just an integer or a character for example, I want to be able to push in my own object, for example a coordinate with two int variables in it. Here is the code:
This is the node:
And now my main:Code:#ifndef COORDINATE_H #define COORDINATE_H class Coordinate { public: int yvalue; int zvalue; Coordinate( int, int ); }; Coordinate::Coordinate( int value1, int value2 ) { yvalue = value1; zvalue = value2; } #endif
My question is: how do I reference each individual member of the coordinate. Can I do just [cout << *iter->yvalue;] ?Code:#include <iostream> #include <list> #include "coordinate.h" using namespace std; int main() { list< Coordinate > myGrid; list< Coordinate >::iterator iter; Coordinate *newPtr1 = new Coordinate( 0, 1 ); Coordinate *newPtr2 = new Coordinate( 0, 2 ); Coordinate *newPtr3 = new Coordinate( 1, 1 ); myGrid.push_back( newPtr1 ); myGrid.push_back( newPtr2 ); myGrid.push_back( newPtr3 ); iter = myGrid.begin(); }
Or, do I need to create the necesary functions inside the coordinate class to return the desired data?
So far every example and tutorial that I have seen deals only with lists and simple data types like ints and strings. But none that I have seen with user defined types. If I could use the stl list it would facilitate so much for me.
Please help?
Thanks,
Figa



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.
push_back() on all of the STL containers will create a copy of the object internally, and the container will be responsible for cleanup of the object after it is erased. If you didn't create the object dynamically to pass to the container, then the original object will go out of scope eventually and be destructed, leaving only the copy in the container which will be cleaned up automatically.