Sign in to follow this  
baker

simple class and vector question.

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
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
oh snap...this is starting to get crazy for me...

what do i do about the 95 warnings?

also since my map has integer and a vector of strings, i must also define an iterator for the vector of my strings in my map object?

Share this post


Link to post
Share on other sites
Can you post your code so we have some hints to what may be the cause for the error ?

Share this post


Link to post
Share on other sites
hello, basically now, i am experimenting with the map class. when i compile i get a bunch of warnings. maybe vc++ is not up to date? well at least i am not getting any error.

in the map class iterator, if my iterator is pointing to something, it seems that i have access to first and second fields? thats pretty cool. i am just wondering if/when i create my map<int, vector <string>>, how will i insert new items into my map, and new strings to the string vector?



#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
//#include "history.h"

using namespace std;


int main()
{
//typedef vector<string> history;
map<int,string> myHistory;
map<int,string>::iterator pos;

myHistory.insert(map<int, string>::value_type(5,"hello"));
myHistory.insert(map<int, string>::value_type(6,"77.33"));
myHistory.insert(map<int, string>::value_type(7,"hellothere"));

pos = myHistory.begin();
cout << " size = " << myHistory.size() << endl;

for (pos = myHistory.begin(); pos != myHistory.end(); ++pos)
{
if (pos->first == 6)
{
cout<< "found it" << endl;
}
}

pos = myHistory.find(5);
cout<< "pos = " << pos->first << endl;

return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
C++ compilers will reject that.
C++ compilers parse >> as the right shift operator, even inside template declarations.
Whilst I would still suggest putting in the space (for portability), MS actually made VS2005 smart enough to work without the space, i.e. >> doesn't get parsed as right shift within template declarations.

(Yeah I was shocked too![smile])

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
Quote:
Original post by rip-off
C++ compilers will reject that.
C++ compilers parse >> as the right shift operator, even inside template declarations.
Whilst I would still suggest putting in the space (for portability), MS actually made VS2005 smart enough to work without the space, i.e. >> doesn't get parsed as right shift within template declarations.

(Yeah I was shocked too![smile])


I wouldn't call it smart, because it fail to treat it as a shift operator when you want a shift operator.

#include <iostream>

using namespace std;

template<int T> struct foo
{
int bar() const {return T;}
};

int main()
{
foo< 4>>1 > baz; // baz is of type foo<2>

cout << baz.bar() << endl;

return 0;
}


Not a common situation maybe, but still, it fails while, as far as I know, it shouldn't. g++ 3.4 on FreeBSD compiles it just fine.

In other words, they fixed one thing and broke another.

Share this post


Link to post
Share on other sites
Quote:
Original post by baker
i am just wondering if/when i create my map<int, vector <string> >, how will i insert new items into my map, and new strings to the string vector?

Some examples how to use are given in the manual i posted before:
http://www.sgi.com/tech/stl/Map.html


int main()
{
map<const char*, int, ltstr> months;

months["january"] = 31;
months["february"] = 28;
months["march"] = 31;
...
cout << "june -> " << months["june"] << endl;
}


Also using typedefs would make things easier to read.


Next thing is your declare:

map<int,string> myHistory;


This means that map<int,string>::key_type is int and map<int,string>::value_type is string.

Then you call

myHistory.insert(map<int, string>::value_type(5,"hello"));


You should better use:

myHistory[5] = "hello";




Share this post


Link to post
Share on other sites
hi, thanks again for the reply. i took a look at the example and they show something very similiar to the C++ book i am looking at.

i see how the map is int and string, but is it possible to have an array of strings or a vector of strings? if its a vector of strings, how can i access the vector or strings through the map and add and delete?

Share this post


Link to post
Share on other sites
Quote:
Original post by Brother Bob
I wouldn't call it smart, because it fail to treat it as a shift operator when you want a shift operator.
*** Source Snippet Removed ***
Not a common situation maybe, but still, it fails while, as far as I know, it shouldn't. g++ 3.4 on FreeBSD compiles it just fine.

In other words, they fixed one thing and broke another.
Oh so they did![rolleyes]
I wasn't convinced that it was a good move on their part anyway. But breaking code that should be valid is teh suxors.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this