assignment/initialization of container - map

X

xuatla

Hi,

I want to define a map:

std::map<string, int> myMap;

e.g., the score of students. Then I can assign the value as follows:
myMap["stud1"] = 90;
myMap["stud2"] = 60;
....

My question now is: can I assign the name of many students in one line?
e.g., for array we have the following way:
int myArray[] = { 1, 3, 4, 5 };

Do we have similar way for map?
std::map<string, int> myMap = { ("stud1", 90), ("stud2", 60) };
// wrong code

Another question: how can I print the score of a given student's name?

void getScore(std::map<string, int> myMap, const std::string& stuName)
{
return myMap.find(stuName)->second();
}

Is this correct? Any better solution?

Thanks in advance!

-X
 
T

Thomas Tutone

xuatla said:
I want to define a map:

std::map<string, int> myMap;

e.g., the score of students. Then I can assign the value as follows:
myMap["stud1"] = 90;
myMap["stud2"] = 60;
...

My question now is: can I assign the name of many students in one line?
e.g., for array we have the following way:
int myArray[] = { 1, 3, 4, 5 };

Do we have similar way for map?
No.

std::map<string, int> myMap = { ("stud1", 90), ("stud2", 60) };
// wrong code

Another question: how can I print the score of a given student's name?

void getScore(std::map<string, int> myMap, const std::string& stuName)
{
return myMap.find(stuName)->second();
}

Is this correct? Any better solution?

First, unless you want to DRASTICALLY inefficient, better to pass the
map by reference instead of by value. Second, you can't return a value
if you declare the function void. Finally, although theoretically
slightly less efficient, more clear in my view is to use operator[] and
dump the separate function altogether:

myMap[stuName]

or if you insist on the separate function, make it:

int getScore(std::map<string, int>& myMap, const std::string& stuName)
{
return myMap[stuName];
}

Best regards,

Tom
 
R

Ron Natalie

xuatla said:
Hi,

I want to define a map:

std::map<string, int> myMap;

e.g., the score of students. Then I can assign the value as follows:
myMap["stud1"] = 90;
myMap["stud2"] = 60;
...

My question now is: can I assign the name of many students in one line?
e.g., for array we have the following way:
int myArray[] = { 1, 3, 4, 5 };

Do we have similar way for map?
std::map<string, int> myMap = { ("stud1", 90), ("stud2", 60) };
// wrong co
No, map's not an aggregate.

The best you can do is something like:

struct apair {
const char* s;
int i;
} maptab[] = { { "stud1", 90 }, ....

for(apair* ap = maptab; ap != sizeof maptab/sizeof (apair); ++ap)
myMap[ap->s] = ap->i;
 
V

Victor Bazarov

Ron said:
xuatla said:
Hi,

I want to define a map:

std::map<string, int> myMap;

e.g., the score of students. Then I can assign the value as follows:
myMap["stud1"] = 90;
myMap["stud2"] = 60;
...

My question now is: can I assign the name of many students in one
line? e.g., for array we have the following way:
int myArray[] = { 1, 3, 4, 5 };

Do we have similar way for map?
std::map<string, int> myMap = { ("stud1", 90), ("stud2", 60) };
// wrong co
No, map's not an aggregate.

The best you can do is something like:

struct apair {
const char* s;
int i;
} maptab[] = { { "stud1", 90 }, ....

for(apair* ap = maptab; ap != sizeof maptab/sizeof (apair); ++ap)
myMap[ap->s] = ap->i;

Shouldn't this work

std::map<string,int> myMap(maptab, maptab +
sizeof(maptab)/sizeof(*maptab));

? You might find that using [] instead of .insert() is less efficient.

V
 
M

Marcus Kwok

Thomas Tutone said:
xuatla said:
Another question: how can I print the score of a given student's name?

void getScore(std::map<string, int> myMap, const std::string& stuName)
{
return myMap.find(stuName)->second();
}

Is this correct? Any better solution?

First, unless you want to DRASTICALLY inefficient, better to pass the
map by reference instead of by value. Second, you can't return a value
if you declare the function void. Finally, although theoretically
slightly less efficient, more clear in my view is to use operator[] and
dump the separate function altogether:

myMap[stuName]

or if you insist on the separate function, make it:

int getScore(std::map<string, int>& myMap, const std::string& stuName)
{
return myMap[stuName];
}

However, if stuName is not already in the map, the myMap[stuName]
version will automatically create an entry for it, and
default-initialize(?, maybe it's zero-initialize) it. Using
myMap.find(), this case can be handled separately if desired.

// untested and uncompiled
int getScore(const std::map<std::string, int>& myMap,
const std::string& stuName)
{
std::map<std::string, int>::const_iterator i = myMap.find(stuName);

if (i != myMap.end()) {
return i->second;
}
else {
return -1;
}
}

I do agree that the myMap[stuName] syntax is easier to read though,
however it cannot be used on a const map (or a reference to a const map)
because of the automatic-entry-creation behavior.
 
T

Thomas Tutone

Victor said:
Ron said:
xuatla wrote:
I want to define a map:

std::map<string, int> myMap;

e.g., the score of students. Then I can assign the value as follows:
myMap["stud1"] = 90;
myMap["stud2"] = 60;
...

My question now is: can I assign the name of many students in one
line? e.g., for array we have the following way:
int myArray[] = { 1, 3, 4, 5 };

Do we have similar way for map?
std::map<string, int> myMap = { ("stud1", 90), ("stud2", 60) };
// wrong co
No, map's not an aggregate.

The best you can do is something like:

struct apair {
const char* s;
int i;
} maptab[] = { { "stud1", 90 }, ....

for(apair* ap = maptab; ap != sizeof maptab/sizeof (apair); ++ap)
myMap[ap->s] = ap->i;

Shouldn't this work

std::map<string,int> myMap(maptab, maptab +
sizeof(maptab)/sizeof(*maptab));

I could be wrong about this, but I don't think that would work. The
map<string, int> constructor would be expecting iterators pointing to
pair<string, int>, and would get instead an iterator to struct { const
char*, int }. Unless there's some sort of implicit conversion going on
that I don't understand, your example shouldn't compile. Even if it
were a map<const char*, int>, I think it still wouldn't work, because a
struct { const char*, int } is different from a pair<const char*, int>.

Or maybe I'm wrong.

Best regards,

Tom
 
R

Ron Natalie

Victor Bazarov wrote:
\
Shouldn't this work

std::map<string,int> myMap(maptab, maptab +
sizeof(maptab)/sizeof(*maptab));

? You might find that using [] instead of .insert() is less efficient.

I would have if I had used pairs. However, you can't use aggregate
initializers on pairs so I had to choose between making the insert
nice or the static initializer.
 
M

mlimber

Thomas said:
xuatla said:
I want to define a map:

std::map<string, int> myMap;

e.g., the score of students. Then I can assign the value as follows:
myMap["stud1"] = 90;
myMap["stud2"] = 60;
...

My question now is: can I assign the name of many students in one line?
e.g., for array we have the following way:
int myArray[] = { 1, 3, 4, 5 };

Do we have similar way for map?

No.

There are several similar but not identical ways for std::map. See the
other responses in this thread for initializing from an array, and also
consider a helper class that uses method chaining (see the FAQ for more
on that):

template<class K, class V>
class MapInitializer
{
typedef std::map<K,V> Map;
Map m_;
public:
operator Map() const { return m_; }

MapInitializer& Add( const K& k, const V& v )
{
m_[k] = v;
return *this;
}
};

const std::map<int,std::string> msgMap
= MapInitializer<int,std::string>()
.Add( 1, "Msg 1" )
.Add( 2, "Msg 2" )
.Add( 42, "Msg 3" );

Cheers! --M
 
X

xuatla

Thomas said:
xuatla said:
I want to define a map:

std::map<string, int> myMap;

e.g., the score of students. Then I can assign the value as follows:
myMap["stud1"] = 90;
myMap["stud2"] = 60;
...

My question now is: can I assign the name of many students in one line?
e.g., for array we have the following way:
int myArray[] = { 1, 3, 4, 5 };

Do we have similar way for map?
No.

std::map<string, int> myMap = { ("stud1", 90), ("stud2", 60) };
// wrong code

Another question: how can I print the score of a given student's name?

void getScore(std::map<string, int> myMap, const std::string& stuName)
{
return myMap.find(stuName)->second();
}

Is this correct? Any better solution?

First, unless you want to DRASTICALLY inefficient, better to pass the
map by reference instead of by value. Second, you can't return a value
if you declare the function void. Finally, although theoretically
slightly less efficient, more clear in my view is to use operator[] and
dump the separate function altogether:

myMap[stuName]

or if you insist on the separate function, make it:

int getScore(std::map<string, int>& myMap, const std::string& stuName)
{
return myMap[stuName];
}

Thanks for your reply. "void" is my typo. I think I was dumb when I
stated my question in above way. I got the answers from the replies
here now. Thanks to all.

- X

btw : an off-topic question: I use thunderbird to read newsgroups.
Today I found that I couldn't read the latest threads in it (the most
recent ones shown was posted at yesterday). Is there anyone else
encountered same questions? Which software do you think is best for
newsgroups reading/posting? Now I am using Google groups to read the
threads here. (I just came back from Mars and this is my first time
using Google groups for newsgroups...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top