insert函数
语法:
1 2 3 4 5 6 7 |
1.直接插入 pair<iterator,bool> insert(const value_type& __x); 2.指定位置插入 iterator insert(iterator __position, const value_type& __x); 3.插入一段迭代器指示位置 template<class _InputIterator> void insert(_InputIterator __first, _InputIterator __last) |
insert()在集合中插入元素
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
/** * @brief Attempts to insert an element into the %set. * @param x Element to be inserted. * @return A pair, of which the first element is an iterator that points * to the possibly inserted element, and the second is a bool * that is true if the element was actually inserted. * * This function attempts to insert an element into the %set. A %set * relies on unique keys and thus an element is only inserted if it is * not already present in the %set. * * Insertion requires logarithmic time. */ pair<iterator,bool> insert(const value_type& __x) { pair<typename _Rep_type::iterator, bool> __p = _M_t.insert_unique(__x); return pair<iterator, bool>(__p.first, __p.second); } /** * @brief Attempts to insert an element into the %set. * @param position An iterator that serves as a hint as to where the * element should be inserted. * @param x Element to be inserted. * @return An iterator that points to the element with key of @a x (may * or may not be the element passed in). * * This function is not concerned about whether the insertion took place, * and thus does not return a boolean like the single-argument insert() * does. Note that the first parameter is only a hint and can * potentially improve the performance of the insertion process. A bad * hint would cause no gains in efficiency. * * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4 * for more on "hinting". * * Insertion requires logarithmic time (if the hint is not taken). */ iterator insert(iterator __position, const value_type& __x) { typedef typename _Rep_type::iterator _Rep_iterator; return _M_t.insert_unique((_Rep_iterator&)__position, __x); } /** * @brief A template function that attemps to insert a range of elements. * @param first Iterator pointing to the start of the range to be * inserted. * @param last Iterator pointing to the end of the range. * * Complexity similar to that of the range constructor. */ template<class _InputIterator> void insert(_InputIterator __first, _InputIterator __last) { _M_t.insert_unique(__first, __last); } |
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <set> using namespace std; int main() { set<int> s1; set<int> s2; //pair<iterator,bool> insert(const value_type& __x); s1.insert(10); s1.insert(10); s1.insert(12); s1.insert(13); s1.insert(9); cout<<"s1集合中元素数量:"<<s1.size()<<endl; //自动去除重复元素 set<int>::iterator it; for(it = s1.begin(); it!=s1.end(); it++){ //自动排序元素 cout<<*it<<","; } cout<<endl; //iterator insert(iterator __position, const value_type& __x); s1.insert(s1.begin(), 2); for(it = s1.begin(); it!=s1.end(); it++){ //自动排序元素 cout<<*it<<","; } cout<<endl; //template<class _InputIterator> //void insert(_InputIterator __first, _InputIterator __last) s2.insert(1); s2.insert(3); s2.insert(5); s1.insert(s2.begin(), s2.end()); s1.insert(s1.begin(), 2); for(it = s1.begin(); it!=s1.end(); it++){ //自动排序元素 cout<<*it<<","; } cout<<endl; } |
测试结果:
1 2 3 4 |
s1集合中元素数量:4 9,10,12,13, 2,9,10,12,13, 1,2,3,5,9,10,12,13, |
注:set容器会对重复元素去除,对元素进行排序(默认从小到大)