using Emplace_back in-place construction
Emplace_back can be in- situ through the parameters of the construction of objects, do not need to copy or move memory, compared to push_back can better avoid the memory copy and move, so that the performance of the container insert element is further improved. in most cases, emplace_back should be preferred instead of push_back.
All of the standard storage (except for the array, since it is immutable and cannot be inserted) adds a similar approach: Emplace, Emplace_hint, Emplace_front, Emplace_after, Emplace_back.
If you need to use EMPLACE_XX to construct in-place, you need to provide a constructor for the class or struct.
struct a{ int A; int b; A (int x, int y): A (x), B (y) {};}; int main () { vector<a> v; (v.emplace_back); In-place construction requires a struct or class to provide constructors cout << v.size () << Endl; return 0;}
using unordered container unordered containers
c++11 added unordered container unordered_map/unordered_multimap/unordered_set/ Unordered_multiset, the elements in these containers are not sorted, are found and stored using hashes, and are therefore more efficient. &NBSP;
because a hash table is used internally for unordered containers, the key of the unordered container needs to provide the Hash_value function , other usages and map/set are the same, and for a custom key, you need to provide a hash function and a comparison function .
struct key{ std::string first; std::string second;}; struct keyhash{ std::size_t operator () (const key& k) const{ return std::hash<std::string> () ( K.first) ^ (std::hash<std::string> () (K.second) << 1);}; }; struct keyequal{ bool operator () (const key& K1, const key& K2) { return K1.first = = K2.first && K1. Second = = K2.second; }}; int main () { std::unordered_map<std::string, int> M1; Decltype (m1) m2 = {{"Hello", 1}, {"World", 2}}; Std::unordered_map<key, std::string, Keyhash, keyequal> M6 = {{ "John", "Doe"}, "Example"}, {"Mary," Sue "}," another "}; For a custom type key, you need to provide a hash function and a comparison function return 0;};
+
c++11--Improving Container performance