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.
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.
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?
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: -
map<string, list<string> > etol;
-
string e = "speak", l1="loquor", l2="voco";
-
list<string> k;
-
k.push_back(l1);
-
etol.insert(pair<string, list<string> >(e, k);
-
etol[e].push_front(l2);
-
//"speak" now maps to a list of "voco", "loquor"
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: gbgbgbgb |
last post by:
Hi,
I have a definition
bool operator<(string s_s, string s_t)
{
....
}
and a variable
list<string> concomp;
|
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;
|
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!
|
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...
|
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:
...
| |
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:
|
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...
|
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...
|
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; }
|
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)
...
|
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,...
| |
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...
|
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,...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |