simple class and vector question.

Started by
17 comments, last by iMalc 18 years, 1 month ago
i am having trouble defining this since i am totally new. i would like to define a vector A that contains a interger number and a pointer to another vector B. vector B is a vector of Strings. so will this work? class vectorA { int mynumber; std::vector<vectorB> * myptr; } class vectorB { std::string s; } main() { std::vector<vectorA> test; //???? } thanks!
Advertisement
basically in my mind id like it to look like this. this list needs to grow 10 steps to the right and maybe 100 or so downward.


vectorA

number1-> string1 -> string2 ->...
|
number2-> string1 -> string2 ->...
|
number3-> string1 -> string2 ->...
|
...
i think i may of got it..



class myB
{
public:
std::vector<std::string> ovnhistory;
std::vector<std::string>::iterator pos;
};


class myA
{
public:
int ovn;
myB *myptr;
};


main()
{
vector<myA> v1;

}
hey thanks for the reply.. what i am trying to do is having a growing number of ID's. each ID will have up to 10messages relating to that ID.

it a message history class i am trying to make. basically i need to store the latest 10 message that i send out to another system for a particular source.

example:
like if i have 20 diffent airplanes in my system, airplane 1 sends 10 messages, airplane 2 sends 5 messagess, airplane 3 sends 8 messages... etc...

i would like to keep track of all those messages for each plane. if the next message for a plane > 10, i will delete the oldest message in the history queue for that plane.

i dont think i make sense. please let me know if i can clarify more.

So basically what you want to do is to map a number (the ID) to a list of strings:

int -> std::vector<string>

Then why not use a std::map or std::hash_map for this ?


If you say your buffer can keep a maximum number of messages, you may also want to implement a ring-buffer instead. You need two indices into an array (or vector), one for read and one for write. If you hit the maximum, they wrap around to zero.
yes, that makes much more sense.

a map with interger for the ID and a vector of strings.

so something like map<int ,vector<string>> myHistory; ?
Quote:Original post by baker
yes, that makes much more sense.

a map with interger for the ID and a vector of strings.

so something like map<int ,vector<string>> myHistory; ?


C++ compilers will reject that. You will have to use

map< int, vector < string > /* notice the space here! */ > myHistory;


C++ compilers parse >> as the right shift operator, even inside template declarations.
whoa, i got like 77 warnings..how about this?

class history
{
vector<string> test;
}

main{

map< int, history > myHistory;

}


????
Quote:Original post by baker
whoa, i got like 77 warnings..how about this?

class history
{
vector<string> test;
}

main{

map< int, history > myHistory;

}


????


Perhaps I was already 10 minutes ahead of you [grin].

But if you wanted to do that there is an easier way:
typedef vector<string> history;main(){map<int, history> historyMap;}
Here is a good reference for the STL:
http://www.sgi.com/tech/stl/

Note that some compilers don't support std::hash_map, but they should support std::map at least.

This topic is closed to new replies.

Advertisement