Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   STL Map Problem (http://www.velocityreviews.com/forums/t959231-stl-map-problem.html)

Mike Copeland 03-30-2013 10:24 PM

STL Map Problem
 
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


Victor Bazarov 03-31-2013 12:20 AM

Re: STL Map Problem
 
On 3/30/2013 6:24 PM, Mike Copeland wrote:
> 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 do not respond to top-posted replies, please don't ask


Ian Collins 03-31-2013 02:36 AM

Re: STL Map Problem
 
Mike Copeland wrote:
> 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?

--
Ian Collins

terminator 03-31-2013 03:10 PM

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



All times are GMT. The time now is 05:00 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.