dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: std::map::at()

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,570

    std::map::at()

    Can anyone remember how we used to extract entries from a std::map in the days before .at() got implemented?

    Code:
    std::map<std::string, int> myMap;
    
        myMap.insert(myMap::value_type("a", 1));
        myMap.insert(myMap::value_type("b", 2));
        myMap.insert(myMap::value_type("c", 3));
    
        std::cout << myMap.at("c") << std::endl; // Doesn't compile ( .at() wasn't available in early versions of VC++ )
    I assumed it'd be something to do with the find() function but I can't seem to make that work
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,900

    Re: std::map::at()

    You could use []

    Code:
    std::cout << myMap["c"] << std::endl;
    Note that if "c" doesn't exist it will be created!

    NB pre-C++11 compilers are to be invalidated 14 Jan 2020!
    Last edited by 2kaud; October 27th, 2019 at 09:21 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.9)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,570

    Re: std::map::at()

    Quote Originally Posted by 2kaud View Post
    You could use []

    Code:
    std::cout << myMap["c"] << std::endl;
    Note that if "c" doesn't exist it will be created!
    Thanks 2kaud - in this particular case, the code was needed in a const member function so for that reason, operator[] gave me a compile error. I ended up using find():-

    Code:
    std::map<std::string, int>::const_iterator kv;
    
    if (kv = myMap.find ("c") != myMap.end ())
        std::cout << kv->second << std::endl;
    "A problem well stated is a problem half solved. - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)