c++11--Improving Container performance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.