STL Map Problem

Discussion in 'C++' started by Mike Copeland, Mar 30, 2013.

  1. 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
     
    Mike Copeland, Mar 30, 2013
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Mar 31, 2013
    #2
    1. Advertising

  3. Mike Copeland

    Ian Collins Guest

    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
     
    Ian Collins, Mar 31, 2013
    #3
  4. Mike Copeland

    terminator Guest

    are familiar with basics of c++?
    you need to read some text books 1st.
    then design your algorithm.
     
    terminator, Mar 31, 2013
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    590
  2. Marcus
    Replies:
    2
    Views:
    591
    Marcus
    Dec 9, 2005
  3. Replies:
    2
    Views:
    556
    klaus hoffmann
    Feb 22, 2006
  4. kl
    Replies:
    7
    Views:
    1,289
    James Kanze
    Jan 1, 2008
  5. Luca Risolia

    STL map to STL vector

    Luca Risolia, Jan 13, 2014, in forum: C++
    Replies:
    32
    Views:
    371
    Seungbeom Kim
    Jan 18, 2014
Loading...

Share This Page