美文网首页
map in C++

map in C++

作者: 成江 | 来源:发表于2018-03-17 22:13 被阅读16次

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    map<string, int> phoneBook;
    int n = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
        string name;
        cin >> name;
        int number;
        cin >> number;
        phoneBook.insert(pair<string, int> (name, number));
    }
    string query;
    while(cin >> query) {
        /*find returns Iterator to an element with key equivalent to `key`. 
          If no such element is found, past-the-end iterator is returned. */
        auto search = phoneBook.find(query);

        if ( search != phoneBook.end()) {
            cout << search->first << "=" << search->second << endl;
        } else {
            cout << "Not found" << endl;
        }
    }
    return 0;
}

相关文章

  • go语言学习总结

    1、go语言的map和c++中的map有什么区别? go语言中的map是hash_table,和c++中uno...

  • map

    在其他语言诸如C++/Java中, map一般都以库的方式提供 如C++中的 std::map<> ,Java中的...

  • 实现特定场景下高性能的HashMap

    C++标准库的某些场景下的效率问题 在下面的场景中,C++标准库的unordered_map、map、multis...

  • C++ unordered_map

    hash_map ≈ unordered_map 最初的 C++ 标准库中没有类似 hash_map 的实现,但不...

  • map hash_map(挖坑)

    学习内容来自C++ STL中哈希表 hash_map 未学C++之哈希表的使用 map 使用count,返回的是被...

  • C++大厂面试真题

    C++标准库的map和set有什么区别,如何实现的? map和set都是C++的关联容器,其底层实现都是红黑树。 ...

  • C++ STL:unordered_map

    背景: hash_map 不是C++ 11 的标准 在vc中编译时: #includeusin...

  • map in C++

  • Map in C++

    map简介 是STL的一个关联容器,它提供一对一的hash。 -第一个可以称为关键字(key),每个关键字只能在m...

  • c++ map

    map的创建: map的添加:三种方法,insert and 数组 map的遍历:使用迭代器 https://ww...

网友评论

      本文标题:map in C++

      本文链接:https://www.haomeiwen.com/subject/bhrsqftx.html