Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ map key exists

#include <iostream>
#include <map>

using std::cout; using std::cin;
using std::endl; using std::map;
using std::string;

int main(){
    string key_to_find;
    std::map<string, string> lang_map = {{"j", "Julia",},
                                         {"p", "Python",},
                                         {"m", "MATLAB",},
                                         {"o", "Octave",},
                                         {"s", "Scala",},
                                         {"l", "Lua",}};

    for (const auto & [key, value] : lang_map) {
        cout << key << " : " << value << endl;
    }

    cout << "Enter the key to search for: ";
    cin >> key_to_find;

    if (lang_map.find(key_to_find) != lang_map.end()) {
        cout << "Key Exists!" << endl;
    }

    return EXIT_SUCCESS;
}
Source by www.delftstack.com #
 
PREVIOUS NEXT
Tagged: #map #key #exists
ADD COMMENT
Topic
Name
7+7 =