multiset :: insert()是C++ STL中的内置函数,可在多集容器中插入元素或将元素从一个位置插入到另一个位置,从一个多集插入到另一个多集。
- 句法:
iterator multiset_name.insert(element)
参数:该函数接受要插入到多集容器中的强制性参数元素。
返回值:该函数返回一个迭代器,该迭代器指向在multiset容器中插入的元素。
下面的程序说明了上述函数:
C++
// C++ program to demonstrate the
// multiset::insert(element) function
#include
using namespace std;
int main()
{
multiset s;
// Function to insert elements
// in the set container
s.insert(1);
s.insert(4);
s.insert(1);
s.insert(5);
s.insert(1);
cout << "The elements in multiset are: ";
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
return 0;
}
C++
// C++ program to demonstrate the
// multiset::insert(iterator, element) function
#include
using namespace std;
int main()
{
multiset 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, 1);
itr = s.insert(itr, 5);
// Slow insertion as position is
// not given correctly
itr = s.insert(s.begin(), 3);
cout << "The elements in multiset are: ";
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
return 0;
}
C++
// C++ program to demonstrate the
// multiset::insert(iteratorposition1, iteratorposition2) function
#include
using namespace std;
int main()
{
multiset s1;
// Function to insert elements
// in the set container
s1.insert(1);
s1.insert(4);
s1.insert(1);
s1.insert(5);
s1.insert(1);
s1.insert(3);
cout << "The elements in multiset1 are: ";
for (auto it = s1.begin(); it != s1.end(); it++)
cout << *it << " ";
multiset s2;
// Function to insert one multiset to another
// all elements from where 3 is to end is
// inserted to multiset2
s2.insert(s1.find(3), s1.end());
cout << "\nThe elements in multiset2 are: ";
for (auto it = s2.begin(); it != s2.end(); it++)
cout << *it << " ";
return 0;
}
输出:
The elements in multiset are: 1 1 1 4 5
- 句法:
iterator multiset_name.insert(iterator position, element)
参数:该函数接受以下两个参数:
- element:它指定要在多集容器中插入的元素。
- position:不指定要插入的位置,它仅指向要开始搜索操作以插入的位置,以加快处理速度。插入是根据多集容器遵循的顺序完成的。
返回值:该函数返回一个迭代器,该迭代器指向在multiset容器中插入的元素。
下面的程序说明了上述函数:
C++
// C++ program to demonstrate the
// multiset::insert(iterator, element) function
#include
using namespace std;
int main()
{
multiset 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, 1);
itr = s.insert(itr, 5);
// Slow insertion as position is
// not given correctly
itr = s.insert(s.begin(), 3);
cout << "The elements in multiset are: ";
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
return 0;
}
输出:
The elements in multiset are: 1 1 3 4 5
- 句法:
iterator multiset_name.insert(iterator position1, iterator position2)
参数:该函数接受两个参数position1和position2 ,它们指定元素的范围。范围[position1,last)范围内的所有元素都插入到另一个set容器中。
返回值:该函数返回一个多重集,该多重集具有所有在[position1,last)范围内的元素。
下面的程序说明了上述函数:
C++
// C++ program to demonstrate the
// multiset::insert(iteratorposition1, iteratorposition2) function
#include
using namespace std;
int main()
{
multiset s1;
// Function to insert elements
// in the set container
s1.insert(1);
s1.insert(4);
s1.insert(1);
s1.insert(5);
s1.insert(1);
s1.insert(3);
cout << "The elements in multiset1 are: ";
for (auto it = s1.begin(); it != s1.end(); it++)
cout << *it << " ";
multiset s2;
// Function to insert one multiset to another
// all elements from where 3 is to end is
// inserted to multiset2
s2.insert(s1.find(3), s1.end());
cout << "\nThe elements in multiset2 are: ";
for (auto it = s2.begin(); it != s2.end(); it++)
cout << *it << " ";
return 0;
}
输出:
The elements in multiset1 are: 1 1 1 3 4 5
The elements in multiset2 are: 3 4 5
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。