C++集 insert()用于在集中插入新元素。
由于元素键在集合中唯一,因此插入操作首先检查给定键是否已存在于集合中,如果键已存在于集合中,则不会将其插入到集合中。 set并返回现有键的迭代器,否则在set中插入新元素。
single element (1) pair<iterator,bool> insert (const value_type& val); //until C++ 11 with hint (2) iterator insert (iterator position, const value_type& val); //until C++ 11 range (3) template <class InputIterator> void insert (InputIterator first, InputIterator last); //until C++ 11 single element (1) pair<iterator,bool> insert (const value_type& val); pair<iterator,bool> insert (value_type&& val); //since C++ 11 with hint (2) iterator insert (const_iterator position, const value_type& val); iterator insert (const_iterator position, value_type&& val); //since C++ 11 range (3) template <class InputIterator> void insert (InputIterator first, InputIterator last); //since C++ 11 initializer list (4) void insert (initializer_list<value_type> il); //since C++ 11
val - 要插入到集合中的值。
position - 提示在元素中插入元素的位置。
first - 要插入的参数的开始。
last - 要插入的参数的末尾。
il - 初始化列表。
返回布尔对以指示是否发生插入,并返回指向新插入元素的迭代器。
让我们看一下将元素插入到集合中的简单示例:
#include <iostream> #include <set> using namespace std; int main() { set<int> s; //Function to insert elements //in the set container s.insert(1); s.insert(4); s.insert(2); s.insert(5); s.insert(3); cout << "The elements in set are: "; for (auto it = s.begin(); it != s.end(); it++) cout << *it << " "; return 0; }
输出:
The elements in set are: 1 2 3 4 5
在上面的示例中,它只是使用给定的键插入元素。
让我们看一个简单的示例,将元素插入指定位置:
#include <iostream> #include <set> using namespace std; int main() { set<int> s; //Function to insert elements //in the set container auto itr = s.insert(s.begin(), 1); //the time taken to insertion //is very less as the correct //position for insertion is given itr = s.insert(itr, 4); itr = s.insert(itr, 2); itr = s.insert(itr, 5); itr = s.insert(itr, 3); cout << "The elements in set are: "; for (auto it = s.begin(); it != s.end(); it++) cout << *it << " "; return 0; }
输出:
The elements in set are: 1 2 3 4 5
在上面的示例中,元素被插入到定义的位置。
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-set-insert-function.html
来源:LearnFk无涯教程网
让我们看一个简单的示例,将给定参数内的一组元素插入到另一组:
#include <iostream> # include<iostream> # include<set> using namespace std; int main() { set<int> s1; //Function to insert elements //in the set container s1.insert(1); s1.insert(4); s1.insert(2); s1.insert(5); s1.insert(3); cout << "The elements in set1 are: "; for (auto it = s1.begin(); it != s1.end(); it++) cout << *it << " "; set<int> s2; //Function to insert one set to another //all elements from where 3 is to end is //inserted to set2 s2.insert(s1.find(3), s1.end()); cout << "\nThe elements in set2 are: "; for (auto it = s2.begin(); it != s2.end(); it++) cout << *it << " "; return 0; }
输出:
The elements in set1 are: 1 2 3 4 5 The elements in set2 are: 3 4 5
让我们看一个简单的示例,从初始化列表中插入元素:
#include <iostream> #include <set> using namespace std; int main(void) { set<string> m = {"Java", "C++", "SQL"}; //Insert the elements from an initializer_list m.insert({"VB", "Oracle"}); cout << "Set contains following elements" << endl; for (auto it = m.begin(); it != m.end(); ++it) cout << *it<< endl; return 0; }
输出:
Set contains following elements C++ Java Oracle SQL VB
在上面的示例中,元素是从初始化列表中插入的。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)