大佬教程收集整理的这篇文章主要介绍了C++ set,STL set详解,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
template < class Key,class Pred = less<Key>,class A = allocator<Key> > class set {...}
pair<iterator,bool> insert(const T & val);
如果 set 的 insert 成员函数的返回值是 pair 模板类对象 x,如果 x.second 为 true,则说明插入成功,此时 x.first 就是指向被插入元素的迭代器;如果 x.second 为 false,则说明要插入的元素已在容器中,此时 x.first 就是指向原有那个元素的迭代器。pair<iterator,iterator> equal_range(const T & val);
返回值对象中的 first 就是 lower_bound 的值,second 就是 upper_bound 的值。#include <iostream> #include <set> //使用set须包含此文件 using namespace std; int main() { typedef set<int>::iterator IT; int a[5] = { 3,4,6,1,2 }; set<int> st(a,a+5); // st里是 1 2 3 4 6 pair< IT,bool> result; result = st.insert(5); // st变成 1 2 3 4 5 6 if(result.second) //插入成功则输出被插入元素 cout << * result.first << " inserted" << endl; //输出: 5 inserted if(st.insert(5).second) cout << * result.first << endl; else cout << * result.first << " already exists" << endl; //输出 5 already exists pair<IT,IT> bounds = st.equal_range(4); cout << * bounds.first << "," << * bounds.second ; //输出:4,5 return 0; }程序的输出结果是:
以上是大佬教程为你收集整理的C++ set,STL set详解全部内容,希望文章能够帮你解决C++ set,STL set详解所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。