添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
深沉的野马  ·  JavaIO流 ...·  昨天    · 
捣蛋的牙膏  ·  java ...·  昨天    · 
霸气的跑步机  ·  Strings with ...·  昨天    · 
谦和的沙滩裤  ·  主题配置 | ...·  昨天    · 
坚韧的乌冬面  ·  搜索结果-隧道网·  3 月前    · 
考研的投影仪  ·  滴答清单PC版v3.7.1.1 ...·  4 月前    · 
开朗的硬盘  ·  Docker - SonarCloud ...·  5 月前    · 
4 ++ word_count [ word ]; 5 for ( const auto & w : word_count ) 6 cout << w . first << " occurs " << w . second << (( w . second > 1 ) ? " times" : "time" ) << endl ;
  • 使用unordered_map替换map

    单词不太可能按照字典序输出
     1map<string, size_t> word_count;
     2set<string> exclude = {"The", "But", "And", "Or", "An", "A", "the", "but", "and", "or", "an", "a"};
     5string word;
     6while (cin >> word)
     7    if (exclude.find(word) == exclude.end())
     8        ++word_count[word];
     9for (const auto &w : word_count)
    10    cout << w.first << " occurs " << w.second << ((w.second > 1) ? " times" : "time") << endl;
    8 map < int , int > exc { { 0 , 0 }, { 1 , 1 } }; 9 auto it = exc . find ( 1 ); 10 if ( it != exc . end ()) 11 cout << it -> first << " \t " << it -> second << endl ; 12 return 0 ;
    1set<int> iset = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    3iset.find(1);        // 返回指向1的迭代器
    4iset.find(11);       // 返回iset.end()
    5iset.count(1)        // 返回1
    6iset.count(11);      // 返回0
    multiset 
  • 如果unordered_multimap和unordered_multiset中有多个关键字相同的元素, 这些元素在同一个桶中, 且存储位置相邻

    unordered_multimap

    unordered_multiset
  •  1string search_item("Alain de Botton");        // 作者名
     2auto entries = authors.count(search_item);    // 书籍个数
     3auto iter = authors.find(search_item);        // 第一本书的位置
     5while(entries)
     7    cout << iter->second << endl;             // 输出书名
     8    ++iter;                                   // 直接后移
     9    --entries;
    

    无序关联容器: unordered_multimap

    9 unordered_multimap<int, string> us{ {0, "start"}, 10 {0, "begin"}, 11 {1, "first"}, 12 {3, "third"} }; 14 cout << us.bucket_count() << endl; 16 auto cnt = us.count(0); 17 auto it = us.find(0); 19 for (; cnt > 0; ++it, --cnt) 20 { 21 cout << it->first << "\t" << it->second << endl; 22 } 24 return 0;
    1string search_item("Alain de Botton");        // 作者名
    3for (auto beg = authors.lower_bound(search_item),
    4        end = authors.upper_bound(search_item);
    5    beg != end; ++beg)
    6    cout << beg->second << endl;    // 输出书名
    8// 如果关键字不在容器中, beg和end相等
    
    1string search_item("Alain de Botton");        // 作者名
    3for (auto pos = authors.equal_range(search_item);        
    4        pos.first != pos.second;
    5        ++pos.first)
    6    cout << pos.first->second << endl;

    无序关联容器: unordered_multimap

    9 unordered_multimap<int, string> us{ {0, "start"}, 10 {0, "begin"}, 11 {1, "first"}, 12 {3, "third"} }; 14 auto range = us.equal_range(0); 15 for(; range.first != range.second; ++range.first) 16 { 17 cout << range.first->first << "\t" << range.first->second << endl; 18 } 20 return 0; 4 ++word_count[word]; 5for (const auto &w : word_count) 6 cout << w.first << " occurs " << w.second << ((w.second > 1) ? " times" : "time") << endl;
  • 使用unordered_map替换map

    单词不太可能按照字典序输出
     1map<string, size_t> word_count;
     2set<string> exclude = {"The", "But", "And", "Or", "An", "A", "the", "but", "and", "or", "an", "a"};
     5string word;
     6while (cin >> word)
     7    if (exclude.find(word) == exclude.end())
    
    
    
    
    
        
     8        ++word_count[word];
     9for (const auto &w : word_count)
    10    cout << w.first << " occurs " << w.second << ((w.second > 1) ? " times" : "time") << endl;
    8 map<int, int> exc{ {0, 0}, {1, 1} }; 9 auto it = exc.find(1); 10 if (it != exc.end()) 11 cout << it->first << "\t" << it->second << endl; 12 return 0;
    1set<int> iset = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    3iset.find(1);        // 返回指向1的迭代器
    4iset.find(11);       // 返回iset.end()
    5iset.count(1)        // 返回1
    6iset.count(11);      // 返回0
    multiset 
  • 如果unordered_multimap和unordered_multiset中有多个关键字相同的元素, 这些元素在同一个桶中, 且存储位置相邻

    unordered_multimap

    unordered_multiset
  •  1string search_item("Alain de Botton");        // 作者名
     2auto entries = authors.count(search_item);    // 书籍个数
     3auto iter = authors.find(search_item);        // 第一本书的位置
     5while(entries)
     7    cout << iter->second << endl;             // 输出书名
     8    ++iter;                                   // 直接后移
     9    --entries;
    

    无序关联容器: unordered_multimap

    9 unordered_multimap<int, string> us{ {0, "start"}, 10 {0, "begin"}, 11 {1, "first"}, 12 {3, "third"} }; 14 cout << us.bucket_count() << endl; 16 auto cnt = us.count(0); 17 auto it = us.find(0); 19 for (; cnt > 0; ++it, --cnt) 20 { 21 cout << it->first << "\t" << it->second << endl; 22 } 24 return 0;
    1string search_item("Alain de Botton");        // 作者名
    3for (auto beg = authors.lower_bound(search_item),
    4        end = authors.upper_bound(search_item);
    5    beg != end; ++beg)
    6    cout << beg->second << endl;    // 输出书名
    8// 如果关键字不在容器中, beg和end相等
    
    1string search_item("Alain de Botton");        // 作者名
    3for (auto pos = authors.equal_range(search_item);        
    4        pos.first != pos.second;
    5        ++pos.first)
    6    cout << pos.first->second << endl;

    无序关联容器: unordered_multimap

    9 unordered_multimap<int, string> us{ {0, "start"}, 10 {0, "begin"}, 11 {1, "first"}, 12 {3, "third"} }; 14 auto range = us.equal_range(0); 15 for(; range.first != range.second; ++range.first) 16 { 17 cout << range.first->first << "\t" << range.first->second << endl; 18 } 20 return 0;