473,503 Members | 1,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL Map of list<string>

3 New Member
I am trying to create a map that contains a string as the key and a list of strings as the value. Essentially it will be a dictionary where the key is an English word and the list will contain all Latin equivalents. My program reads from a file containing Latin words and their English equivalents. I am using the operator[] from map so that I can add to the list<string> should the Key already exist in my map. I am not sure of the syntax that I need to be using for this. The operator[] returns a reference to the element, should I be storing this reference in a map iterator? I get an error when I attempt this

mit = unit[key];

mit is my map iterator, unit is my map, and key is the string I am attempting to store in the map.
Nov 26 '07 #1
4 12080
Ganon11
3,652 Recognized Expert Specialist
You could also do something like this:

1) Use the map's find() operation to get the pair<string, list<string> > containing your key and its value (the list).
2) Manually add your new value to the list.

I'm not sure how you could correctly overload the operator[], but my guess is you'd have to return a reference to the value type - in this case, returning a list, not a map iterator.
Nov 26 '07 #2
Maikeru
3 New Member
You could also do something like this:

1) Use the map's find() operation to get the pair<string, list<string> > containing your key and its value (the list).
2) Manually add your new value to the list.

I'm not sure how you could correctly overload the operator[], but my guess is you'd have to return a reference to the value type - in this case, returning a list, not a map iterator.
I was actually misunderstanding the problem that I am having. Basically the compiler doesn't like that I am trying to insert just the key into the map so it is expecting a pair. In order to properly insert a pair, (i am using insert() now) how would I go about this considering my value is a list? Should I be passing the key and a list that I create prior to calling insert or should I be passing the key and the string value that I want in the list?
Nov 26 '07 #3
Laharl
849 Recognized Expert Contributor
You have a map<string, list<string> >. To insert to a preexisting value list, use map[key].*appropriate list insert method*. If the list does not exist beforehand, create it before insertion.

For example:
Expand|Select|Wrap|Line Numbers
  1. map<string, list<string> > etol;
  2. string e = "speak", l1="loquor", l2="voco";
  3. list<string> k;
  4. k.push_back(l1);
  5. etol.insert(pair<string, list<string> >(e, k);
  6. etol[e].push_front(l2);
  7. //"speak" now maps to a list of "voco", "loquor"
  8.  
Nov 27 '07 #4
Maikeru
3 New Member
Thanks for all of your help! I have got it working now for the most part. Just a few formatting tweaks to be made. Thanks again.
Nov 27 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2504
by: gbgbgbgb | last post by:
Hi, I have a definition bool operator<(string s_s, string s_t) { .... } and a variable list<string> concomp;
2
5042
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
3
8478
by: aquanutz | last post by:
Ok, I have a list of strings (list<string> stringList) that I want to sort alphabetcially, only "sort(stringList.begin(), stringList.end()); ) does not work. Any insight would be helpful. Thanks!
3
15142
by: Abhi | last post by:
In the following hypothetical example I want to build a generic list of unique string items. How should I implement the pred function so that it returns true/false if target string exists in the...
6
46158
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
4
23925
by: Mark Rae | last post by:
Hi, Is it possible to create a case-insensitive List<stringcollection? E.g. List<stringMyList = new List<string>; MyList.Add("MyString"); So that:
1
3754
by: cakeathon | last post by:
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck I have the follwing class in a header file: class String_list { public: String_list(); void...
2
5856
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
4
3931
by: parez | last post by:
Hi, I am trying to serialize List<List<string>. With the following code public List<List<string>DataRows { get; set; }
2
2296
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, what are the differences (performance, features, capabilities, etc.) between a List of string and an array of string? I'd like to know when it's better one or another (I'm using C# 2.0) ...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7281
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.