赞
踩
std::map::find
public member function - 公开成员函数
std::map::find
iterator find (const key_type& k);
const_iterator find (const key_type& k) const;
寻找带有特定键的元素。
Get iterator to element - 获取元素的迭代器
Searches the container for an element with a key
equivalent to k
and returns an iterator to it if found, otherwise it returns an iterator to map::end
.
在容器中搜索具有等于 k
的键的元素,如果找到则返回一个迭代器,否则返回一个到 map::end
的迭代器。
Two keys
are considered equivalent if the container’s comparison object returns false
reflexively (i.e., no matter the order in which the elements are passed as arguments).
如果容器的比较对象条件反射式地返回 false
(即无论元素作为参数传递的顺序如何),则认为两个键是等同的。
Another member function, map::count
, can be used to just check whether a particular key exists.
另一个成员函数 map::count
可用于仅检查特定键是否存在。
reflexively:adv. 反身
k
Key to be searched for. - 要搜索的键。
Member type key_type
is the type of the keys for the elements in the container, defined in map as an alias of its first template parameter (Key
).
成员类型 key_type
是容器中元素的键类型,在 map
中定义为其第一个模板参数 (Key
) 的别名。
An iterator to the element, if an element with specified key is found, or map::end
otherwise.
如果找到具有指定键的元素,则为元素的迭代器,否则为 map::end
。
If the map object is const-qualified, the function returns a const_iterator
. Otherwise, it returns an iterator
.
如果 map 对象是 const 限定的,则该函数返回 const_iterator
。否则,它返回一个迭代器 iterator
。
Member types iterator
and const_iterator
are bidirectional iterator types pointing to elements (of type value_type
).
成员类型迭代器和 const_iterator
是指向元素 (类型为 value_type
) 的双向迭代器类型。
Notice that value_type
in map containers is an alias of pair<const key_type, mapped_type>
.
请注意,map 容器中的 value_type
是 pair<const key_type, mapped_type>
的别名。
std::map::find
//============================================================================ // Name : std::map::find // Author : Yongqiang Cheng // Version : Version 1.0.0 // Copyright : Copyright (c) 2019 Yongqiang Cheng // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <map> int main() { std::map<char, int> map_data; std::map<char, int>::iterator it; map_data['a'] = 50; map_data['b'] = 100; map_data['c'] = 150; map_data['d'] = 200; std::cout << "Size of map_data: " << map_data.size() << '\n'; it = map_data.find('b'); if (it != map_data.end()) { map_data.erase(it); } std::cout << "Size of map_data: " << map_data.size() << '\n'; // print content: std::cout << "\nelements in map_data:" << '\n'; std::cout << map_data.find('a')->first << " => " << map_data.find('a')->second << '\n'; std::cout << map_data.find('c')->first << " => " << map_data.find('c')->second << '\n'; std::cout << map_data.find('d')->first << " => " << map_data.find('d')->second << '\n'; return 0; }
Size of map_data: 4
Size of map_data: 3
elements in map_data:
a => 50
c => 150
d => 200
std::map::find
//============================================================================ // Name : std::map::find // Author : Yongqiang Cheng // Version : Version 1.0.0 // Copyright : Copyright (c) 2019 Yongqiang Cheng // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <map> struct FatKey { int x; int data[1000]; }; struct LightKey { int x; }; bool operator<(const FatKey& fk, const LightKey& lk) { return fk.x < lk.x; } bool operator<(const LightKey& lk, const FatKey& fk) { return lk.x < fk.x; } bool operator<(const FatKey& fk1, const FatKey& fk2) { return fk1.x < fk2.x; } int main() { // simple comparison demo std::map<int, char> example = {{1, 'a'}, {2, 'b'}}; auto search = example.find(2); if (search != example.end()) { std::cout << "Found " << search->first << " -> " << search->second << '\n'; } else { std::cout << "Not found\n"; } // transparent comparison demo std::map<FatKey, char, std::less<>> example2 = {{{1, { }}, 'a'}, {{ 2, { }}, 'b'}}; LightKey lk = {2}; auto search2 = example2.find(lk); if (search2 != example2.end()) { std::cout << "Found " << search2->first.x << " -> " << search2->second << '\n'; } else { std::cout << "Not found\n"; } return 0; }
CDT Build Console
17:27:35 **** Build of configuration Debug for project hello_world **** make all Building file: ../src/hello_world.cpp Invoking: GCC C++ Compiler g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/hello_world.d" -MT"src/hello_world.o" -o "src/hello_world.o" "../src/hello_world.cpp" ../src/hello_world.cpp: In function ‘int main()’: ../src/hello_world.cpp:41:51: error: in C++98 ‘example’ must be initialized by constructor, not by ‘{...}’ std::map<int, char> example = {{1, 'a'}, {2, 'b'}}; ^ ../src/hello_world.cpp:41:51: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 ../src/hello_world.cpp:41:51: error: could not convert ‘{{1, 'a'}, {2, 'b'}}’ from ‘<brace-enclosed initializer list>’ to ‘std::map<int, char>’ ../src/hello_world.cpp:43:2: warning: ‘auto’ changes meaning in C++11; please remove it [-Wc++0x-compat] auto search = example.find(2); ^ ../src/hello_world.cpp:43:7: error: ‘search’ does not name a type auto search = example.find(2); ^ ../src/hello_world.cpp:45:6: error: ‘search’ was not declared in this scope if (search != example.end()) ^ ../src/hello_world.cpp:55:35: error: ‘>>’ should be ‘> >’ within a nested template argument list std::map<FatKey, char, std::less<>> example2 = {{{1, { }}, 'a'}, {{ 2, { }}, 'b'}}; ^ ../src/hello_world.cpp:55:34: error: wrong number of template arguments (0, should be 1) std::map<FatKey, char, std::less<>> example2 = {{{1, { }}, 'a'}, {{ 2, { }}, 'b'}}; ^ In file included from /usr/include/c++/5/string:48:0, from /usr/include/c++/5/bits/locale_classes.h:40, from /usr/include/c++/5/bits/ios_base.h:41, from /usr/include/c++/5/ios:42, from /usr/include/c++/5/ostream:38, from /usr/include/c++/5/iostream:39, from ../src/hello_world.cpp:9: /usr/include/c++/5/bits/stl_function.h:382:12: note: provided for ‘template<class _Tp> struct std::less’ struct less : public binary_function<_Tp, _Tp, bool> ^ ../src/hello_world.cpp:55:35: error: template argument 3 is invalid std::map<FatKey, char, std::less<>> example2 = {{{1, { }}, 'a'}, {{ 2, { }}, 'b'}}; ^ ../src/hello_world.cpp:55:84: error: scalar object ‘example2’ requires one element in initializer std::map<FatKey, char, std::less<>> example2 = {{{1, { }}, 'a'}, {{ 2, { }}, 'b'}}; ^ ../src/hello_world.cpp:58:2: warning: ‘auto’ changes meaning in C++11; please remove it [-Wc++0x-compat] auto search2 = example2.find(lk); ^ ../src/hello_world.cpp:58:7: error: ‘search2’ does not name a type auto search2 = example2.find(lk); ^ ../src/hello_world.cpp:59:6: error: ‘search2’ was not declared in this scope if (search2 != example2.end()) ^ ../src/hello_world.cpp:57:11: warning: unused variable ‘lk’ [-Wunused-variable] LightKey lk = {2}; ^ make: *** [src/hello_world.o] Error 1 src/subdir.mk:18: recipe for target 'src/hello_world.o' failed 17:27:35 Build Finished (took 566ms)
Project -> Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC C++ Compiler -> Dialect
Language standard: ISO C++11 (-std=c++0x)
CDT Build Console
17:30:37 **** Build of configuration Debug for project hello_world **** make all Building file: ../src/hello_world.cpp Invoking: GCC C++ Compiler g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/hello_world.d" -MT"src/hello_world.o" -o "src/hello_world.o" "../src/hello_world.cpp" ../src/hello_world.cpp: In function ‘int main()’: ../src/hello_world.cpp:55:34: error: wrong number of template arguments (0, should be 1) std::map<FatKey, char, std::less<>> example2 = {{{1, { }}, 'a'}, {{ 2, { }}, 'b'}}; ^ In file included from /usr/include/c++/5/string:48:0, from /usr/include/c++/5/bits/locale_classes.h:40, from /usr/include/c++/5/bits/ios_base.h:41, from /usr/include/c++/5/ios:42, from /usr/include/c++/5/ostream:38, from /usr/include/c++/5/iostream:39, from ../src/hello_world.cpp:9: /usr/include/c++/5/bits/stl_function.h:382:12: note: provided for ‘template<class _Tp> struct std::less’ struct less : public binary_function<_Tp, _Tp, bool> ^ ../src/hello_world.cpp:55:35: error: template argument 3 is invalid std::map<FatKey, char, std::less<>> example2 = {{{1, { }}, 'a'}, {{ 2, { }}, 'b'}}; ^ ../src/hello_world.cpp:55:84: error: scalar object ‘example2’ requires one element in initializer std::map<FatKey, char, std::less<>> example2 = {{{1, { }}, 'a'}, {{ 2, { }}, 'b'}}; ^ make: *** [src/hello_world.o] Error 1 src/subdir.mk:18: recipe for target 'src/hello_world.o' failed 17:30:37 Build Finished (took 674ms)
Project -> Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC C++ Compiler -> Dialect
Language standard: ISO C++1y (-std=c++1y)
CDT Build Console
17:32:42 **** Build of configuration Debug for project hello_world ****
make all
Building file: ../src/hello_world.cpp
Invoking: GCC C++ Compiler
g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/hello_world.d" -MT"src/hello_world.o" -o "src/hello_world.o" "../src/hello_world.cpp"
Finished building: ../src/hello_world.cpp
Building target: hello_world
Invoking: GCC C++ Linker
g++ -o "hello_world" ./src/hello_world.o
Finished building target: hello_world
17:32:43 Build Finished (took 896ms)
Found 2 -> b
Found 2 -> b
Logarithmic in size.
与容器大小成对数。
No changes.
The container is accessed (neither the const nor the non-const versions modify the container).
容器被访问 (const 和非 const 版本都不能修改容器)。
No mapped values are accessed: concurrently accessing or modifying elements is safe.
没有映射的值被访问:同时访问或修改元素是安全的。
Strong guarantee: if an exception is thrown, there are no changes in the container.
有力的保证:如果引发异常,则容器中没有任何更改。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。