multimap :: swap()是C++ STL中的内置函数,它交换两个multimap容器。调用swap()函数后,multimap1的内容在multimap2中,multimap2的内容在multimap1中。
句法:
multimap1_name.swap(multimap2_name)
参数:此函数接受一个要与multimap1_name交换的参数,
返回值:该函数不返回任何内容。
// C++ function for illustration
// multimap::swap() function
#include
using namespace std;
int main()
{
// initialize container
multimap mp1, mp2;
// insert elements in random order
mp1.insert({ 2, 30 });
mp1.insert({ 1, 40 });
mp2.insert({ 10, 60 });
mp2.insert({ 9, 20 });
cout << "\nThe multimap1 before applying swap() is : \n";
cout << "KEY\tELEMENT\n";
for (auto itr = mp1.begin(); itr != mp1.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
cout << "\nThe multimap2 before applying swap() is : \n";
cout << "KEY\tELEMENT\n";
for (auto itr = mp2.begin(); itr != mp2.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
// performs swap operation of two multimap
mp1.swap(mp2);
cout << "\nThe multimap1 after applying swap() is : \n";
cout << "KEY\tELEMENT\n";
for (auto itr = mp1.begin(); itr != mp1.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
cout << "\nThe multimap2 after applying swap() is : \n";
cout << "KEY\tELEMENT\n";
for (auto itr = mp2.begin(); itr != mp2.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
return 0;
}
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。