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
单词不太可能按照字典序输出
Copy 1 map< string, size_t> word_count;
2 set< string> exclude = { "The" , "But" , "And" , "Or" , "An" , "A" , "the" , "but" , "and" , "or" , "an" , "a" };
5 string word;
6 while ( cin >> word)
7 if ( exclude. find( word) == exclude. end())
8 ++ word_count[ word];
9 for ( 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
;
Copy 1 set< int > iset = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
3 iset. find( 1 ); // 返回指向1的迭代器
4 iset. find( 11 ); // 返回iset.end()
5 iset. count( 1 ) // 返回1
6 iset. count( 11 ); // 返回0
multiset
如果unordered_multimap和unordered_multiset中有多个关键字相同的元素, 这些元素在同一个桶中, 且存储位置相邻
unordered_multimap
unordered_multiset
Copy 1 string search_item ( "Alain de Botton" ); // 作者名
2 auto entries = authors. count( search_item); // 书籍个数
3 auto iter = authors. find( search_item); // 第一本书的位置
5 while ( 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 ;
Copy 1 string search_item ( "Alain de Botton" ); // 作者名
3 for ( 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相等
Copy 1 string search_item ( "Alain de Botton" ); // 作者名
3 for ( 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];
5 for ( const auto & w : word_count)
6 cout << w. first << " occurs " << w. second << (( w. second > 1 ) ? " times" : "time" ) << endl;
使用unordered_map替换map
单词不太可能按照字典序输出
Copy 1 map< string, size_t> word_count;
2 set< string> exclude = { "The" , "But" , "And" , "Or" , "An" , "A" , "the" , "but" , "and" , "or" , "an" , "a" };
5 string word;
6 while ( cin >> word)
7 if ( exclude. find( word) == exclude. end())
8 ++ word_count[ word];
9 for ( 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 ;
Copy 1 set< int > iset = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
3 iset. find( 1 ); // 返回指向1的迭代器
4 iset. find( 11 ); // 返回iset.end()
5 iset. count( 1 ) // 返回1
6 iset. count( 11 ); // 返回0
multiset
如果unordered_multimap和unordered_multiset中有多个关键字相同的元素, 这些元素在同一个桶中, 且存储位置相邻
unordered_multimap
unordered_multiset
Copy 1 string search_item ( "Alain de Botton" ); // 作者名
2 auto entries = authors. count( search_item); // 书籍个数
3 auto iter = authors. find( search_item); // 第一本书的位置
5 while ( 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 ;
Copy 1 string search_item ( "Alain de Botton" ); // 作者名
3 for ( 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相等
Copy 1 string search_item ( "Alain de Botton" ); // 作者名
3 for ( 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 ;