std::map
1. insert()
#include <iostream>
#include <map>
using namespace std;
int main() {
std::map<char, int> mymap;
// first insert function version (single parameter):
mymap.insert(std::pair<char, int>('a', 100));
mymap.insert(std::pair<char, int>('z', 200));
std::pair<std::map<char, int>::iterator, bool> ret;
ret = mymap.insert(std::pair<char, int>('z', 500));
if (ret.second == false) {
std::cout << "element 'z' already existed";
std::cout << " with a value of " << ret.first->second << '\n';
}
// second insert function version (with hint position):
std::map<char, int>::iterator it = mymap.begin();
mymap.insert(it, std::pair<char, int>('b', 300)); // max efficiency inserting
mymap.insert(it, std::pair<char, int>('c', 400)); // no max efficiency inserting
// third insert function version (range insertion):
std::map<char, int> anothermap;
anothermap.insert(mymap.begin(), mymap.find('c'));
// showing contents:
std::cout << "mymap contains:\n";
for (it = mymap.begin(); it != mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
std::cout << "anothermap contains:\n";
for (it = anothermap.begin(); it != anothermap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
return 0;
}
hupeidong@ZBMac-C02F31WYM Desktop % ./main
element 'z' already existed with a value of 200
mymap contains:
a => 100
b => 300
c => 400
z => 200
anothermap contains:
a => 100
b => 300
2. emplace()
map 容器的成员函数 emplace() 可以在适当的位置直接构造新元素,从而避免复制和移动操作。它的参数通常是构造元素,也就是 pair<const K,T> 对象所需要的。只有当容器中现有元素的键与这个元素的键不同时,才会构造这个元素。
成员函数 emplace() 和 insert() 返回的 pair 对象提供的指示相同。pair 的成员变量 first 是一个指向插入元素或阻止插入的元素的迭代器;成员变量 second 是个布尔值,如果元素插入成功,second 就为 true。
// map::emplace
#include <iostream>
#include <map>
int main() {
std::map<char, int> mymap;
mymap.emplace('x', 100);
mymap.emplace('y', 200);
mymap.emplace('z', 100);
std::cout << "mymap contains:";
for (auto& x : mymap)
std::cout << " [" << x.first << ':' << x.second << ']';
std::cout << '\n';
return 0;
}
hupeidong@ZBMac-C02F31WYM Desktop % ./main
mymap contains: [x:100] [y:200] [z:100]
3. emplace_hint()
map 的成员函数 emplace_hint() 和 emplace() 生成元素的方式在本质上是一样的,除了必须为前者提供一个指示元素生成位置的迭代器,作为 emplace_hint() 的第一个参数。
// map::emplace_hint
#include <iostream>
#include <map>
int main() {
std::map<char, int> mymap;
auto it = mymap.end();
it = mymap.emplace_hint(it, 'b', 10);
mymap.emplace_hint(it, 'a', 12);
mymap.emplace_hint(mymap.end(), 'c', 14);
std::cout << "mymap contains:";
for (auto& x : mymap)
std::cout << " [" << x.first << ':' << x.second << ']';
std::cout << '\n';
return 0;
}
hupeidong@ZBMac-C02F31WYM Desktop % ./main
mymap contains: [a:12] [b:10] [c:14]
4. insert()和emplace()的区别
#include <iostream>
#include <map>
using namespace std;
class Demo {
public:
Demo(int num) : var_(num) {
std::cout << "构造函数" << endl;
}
Demo(const Demo& other) : var_(other.var_) {
std::cout << "拷贝构造函数" << endl;
}
Demo(Demo&& other) : var_(other.var_) {
std::cout << "移动构造函数" << endl;
}
private:
int var_;
};
int main() {
std::map<char, Demo> mymap;
cout << "insert():" << endl;
mymap.insert(std::pair<char, Demo>('a', Demo(1)));
cout << "emplace():" << endl;
mymap.emplace('b', 1);
cout << "emplace_hint():" << endl;
mymap.emplace_hint(mymap.begin(), 'c', 1);
return 0;
}
hupeidong@ZBMac-C02F31WYM Desktop % ./main
insert():
构造函数
移动构造函数
移动构造函数
emplace():
构造函数
emplace_hint():
构造函数
std::vector
1. push_back()和emplace_back()
#include <iostream>
#include <vector>
using namespace std;
class Demo {
public:
Demo(int num) : var_(num) {
std::cout << "构造函数" << endl;
}
Demo(const Demo& other) : var_(other.var_) {
std::cout << "拷贝构造函数" << endl;
}
Demo(Demo&& other) : var_(other.var_) {
std::cout << "移动构造函数" << endl;
}
private:
int var_;
};
int main() {
std::vector<Demo> myvec;
// 没有事先reserve,会重新申请一个空间,需要拷贝
myvec.reserve(2);
cout << "push_back():" << endl;
myvec.push_back(Demo(1));
cout << "emplace_back():" << endl;
myvec.emplace_back(2);
return 0;
}
hupeidong@ZBMac-C02F31WYM Desktop % ./main
push_back():
构造函数
移动构造函数
emplace_back():
构造函数
|