| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need Help with STL (map)
Hi,
I'm trying to creat a <map> from the STL that will be somthing like that : map<string,<vector>> map_name; I defined it like that : Code:
typedef map <string, vector<int>,ltstr> word_ret;
class wordinfo
{
private:
word_ret mapi;
public:
void add_info(const string);
wordinfo();
};
but i'm not sure how to insert data into the map i want that each of the map cells will have his own vector the "ltstr" is small struct that defines which string is bigger thanks ahead =) |
|
#2
|
||||
|
||||
|
Quick example, with char * instead of string though:
Code:
#include <map>
#include <vector>
#include <iostream>
using namespace std;
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
int main(void)
{
vector<int> *v;
map<const char *, vector<int>, ltstr > complicatedMap;
v = new vector<int>();
// like this:
complicatedMap["blah"] = *v;
}
|
|
#3
|
|||
|
|||
|
note that with Icon's code you'll have to explicitly delete all of those vectors.
you can just do this instead, and the vectors will get automatically destructed when the map goes out of scope: Code:
#include <map>
#include <vector>
using namespace std;
struct ltstr
{
bool operator()(const char *s1, const char *s2) const
{
return strcmp(s1, s2) < 0;
}
};
int main()
{
map<const char*, vector<int>, ltstr> complicatedMap;
// like this:
complicatedMap["blah"] = vector<int>();
return 0;
}
|
|
#4
|
||||
|
||||
|
Agreed. It depends on what he wants to do with it and where the vector comes from right? When writing c++ i always find myself thinking so much about these questions, the memory issues, that it really bites into my time of thinking of the problem (i.e. algorithm) at hand.. Many people write c-style programs with c++ and do not use the mechanism you describe ubergeek, which I prefer by the way.
|
|
#5
|
|||
|
|||
|
it seems more logicaly use it with the vector commands
like : Code:
map_name[str_var].push_back(value); - so i did it ![]() i pushed some data in - and then tried to read it back to and see if all fine ... well new problems i tierd to pull the data like that : Code:
for(iter = mapi.begin(); iter != mapi.end(); iter++)
{
cout << iter->second.pop_back;
cout << (*iter).first << endl;
}
well it didn't work - the first line (iter->second.pop_back) gave me that error : Quote:
the second line worked - so i figured how to see the first of the pair - what should i do to pull the second ? thanks ahead |
|
#6
|
|||
|
|||
|
you have to actually call pop_back by including parentheses (even though you aren't passing it any parameters, you need to do that, or the compiler thinks you want a function pointer (which should do something, not what you want but something, but apparently the compiler chokes on it).
Code:
cout << iter->second.pop_back(); |
|
#7
|
|||
|
|||
|
oh it's just typo - i've put there () ...
but i made another mistake - it seems that pop_back don't return the value - therefor how should i show the content of the vector ? with ".front" ? |
|
#8
|
|||
|
|||
|
odd...did you try using (*iter).second like you did in the second line (instead of iter->second)? you could also try putting parentheses in:
Code:
cout << (iter->second).pop_back(); //or cout << ((*iter).second).pop_back(); |
|
#9
|
|||
|
|||
|
i'm kinda lost with those iterators
![]() I believe that i made mistake and stucked the iterator and i'm always pointing to the same cell in the vector pfff those STL should be user friendly |
|
#10
|
|||
|
|||
|
can I see all of your code, including the part that loads values into the vectors? Also specify exactly what you expect the output to be.
|
|
#11
|
|||
|
|||
|
this function insert strings in loop into the map from file
the "w_num" is counter that i use only to see changes in the input (if i'll insert "aaa" twich i'll want to see when i've insert that string (with the data in vector) Code:
void wordinfo::add_info(const string rword,const int w_num)
{
mapi[rword].push_back(w_num);
return;
}
now the problem is pull all the data - right now i tring lots of wierd ways to do it Code:
void wordinfo::printall()
{
string test;
int count = 0;
for(iter = mapi.begin(); iter != mapi.end(); iter++)
{
test = iter->first;
cout << iter->first;
while(mapi[test].size() != 0)
{
cout <<" "<< mapi[test].front()<<endl;
mapi[test].pop_back();
}
return;
}
i can't figure how move the pointer in the vector so i'll be able print all the vector values thanks by the way ![]() |
|
#12
|
|||
|
|||
|
found the problem
![]() it was so simple and i just somhow missed it it should be : Code:
void wordinfo::printall()
{
string test;
int count = 0;
for(iter = mapi.begin(); iter != mapi.end(); iter++)
{
test = iter->first;
cout << iter->first<<endl;
for(count = 0;count < mapi[test].size();count++)
{
cout<<iter->second[count]<<endl;
}
}
return;
}
thanks again ![]() |
|
#13
|
|||
|
|||
|
you can iterate through all STL containers in a similar way. you're already doing it for the map, now do it for the inner vector as well. change your loop thusly:
Code:
for (map<string, vector<int>, ltstr>::iterator miter = mapi.begin(); miter = mapi.end(); ++miter)
{
cout << miter->first;
for (vector<int>::iterator viter = miter->second.begin(); viter != miter->second.end(); ++viter)
{
cout << " " << *viter << endl;
}
}
EDIT: whoops, you beat me. your way works too, but many people will consider the iterator route "cleaner". It really doesn't matter though. Last edited by ubergeek : March 5th, 2006 at 08:36 PM. Reason: fatal typo! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Need Help with STL (map) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|