STL Map Problem

M

Mike Copeland

I am developing a small (and seemingly simple) STL map application,
yet I can't get a clean compile. Below is code that produces a compiler
error on the "find" call, where the compiler doesn't accept the
assignment operator for the iterator. Please advise. TIA

map<string, int> convMap;
map<string, int>::iterator kIter;
string str = "Oops";
convMap[str] = 17;
kIter = convMap.find(str) != convMap.end(); // <== error!
if(kIter != convMap.end())
{
int ppp = kIter->second;
}
else // not found
 
V

Victor Bazarov

I am developing a small (and seemingly simple) STL map application,
yet I can't get a clean compile. Below is code that produces a compiler
error on the "find" call, where the compiler doesn't accept the
assignment operator for the iterator. Please advise. TIA

map<string, int> convMap;
map<string, int>::iterator kIter;
string str = "Oops";
convMap[str] = 17;
kIter = convMap.find(str) != convMap.end(); // <== error!

You're trying to assign the boolean to the iterator. Why? Your
expression is evaluated this way:

kIter = (convMap.find(str) != convMap.end())

(since the inequality operator has precedence over assignment). Did you
mean to assign first, like

(kIter = convMap.find(str))

and then compare it? Why compare it if you aren't going to use the
result of the comparison? Could it be that you need to remove
everything starting with "!=" and until the semicolon (excluding it)?
if(kIter != convMap.end())
{
int ppp = kIter->second;
}
else // not found

V
 
I

Ian Collins

Mike said:
I am developing a small (and seemingly simple) STL map application,
yet I can't get a clean compile. Below is code that produces a compiler
error on the "find" call, where the compiler doesn't accept the
assignment operator for the iterator. Please advise. TIA

When you ask about an error, post it!
map<string, int> convMap;
map<string, int>::iterator kIter;
string str = "Oops";
convMap[str] = 17;
kIter = convMap.find(str) != convMap.end(); // <== error!

Why are you trying to assign a bool (the result of !=) to an iterator?
 
T

terminator

are familiar with basics of c++?
you need to read some text books 1st.
then design your algorithm.
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top