find函数
语法:find
1 2 |
iterator find(const key_type& __x); const_iterator find(const key_type& __x) const; |
find()返回一个指向被查找到元素的迭代器
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// _GLIBCXX_RESOLVE_LIB_DEFECTS // 214. set::find() missing const overload //@{ /** * @brief Tries to locate an element in a %set. * @param x Element to be located. * @return Iterator pointing to sought-after element, or end() if not * found. * * This function takes a key and tries to locate the element with which * the key matches. If successful the function returns an iterator * pointing to the sought after element. If unsuccessful it returns the * past-the-end ( @c end() ) iterator. */ iterator find(const key_type& __x) { return _M_t.find(__x); } const_iterator find(const key_type& __x) const { return _M_t.find(__x); } |
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <set> using namespace std; int main() { set<int> s1; s1.insert(10); s1.insert(12); s1.insert(13); s1.insert(9); //iterator find(const key_type& __x); set<int>::iterator iter = s1.find(9); cout<<*iter<<endl; //const_iterator find(const key_type& __x) const; set<int>::const_iterator iter1 = s1.find(9); cout<<*iter1<<endl; } |
测试结果:
1 2 |
9 9 |