Jump to content
  • Advertisement
Sign in to follow this  
baker

simple class and vector question.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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 ->...
|
...

Share this post


Link to post
Share on other sites
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;

}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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; ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
whoa, i got like 77 warnings..how about this?

class history
{
vector<string> test;
}

main{

map< int, history > myHistory;

}


????

Share this post


Link to post
Share on other sites
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;
}



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!