42

Сергей Жгировский — С++11/14 в STL

  • Upload
    yandex

  • View
    1.010

  • Download
    5

Embed Size (px)

DESCRIPTION

Речь пойдёт о расширении библиотеки STL в стандарте С++11/C++14. Мы поговорим о новых контейнерах, алгоритмах, многопоточности, умных указателях и других полезных вещах, которые появились в новом стандарте и активно используются в Яндексе.

Citation preview

  • 1. C++11/14: -; ; . . ++ party,

2. - 3 3. hash_set, hash_map . - ; , ; ! VS: stdext::hash_set;! GCC: __gnu_cxx::hash_set; ! C 2007: std::tr1::unordered_set, -map, ... 4 4. C++11 std::unordered_set;! std::unordered_map;! std::unordered_multiset;! std::unordered_multimap. 5 std::set;! std::map;! std::multiset;! std::multimap. 5. e 6 - (unordered_set) - (set) 6. O(1)! ! O(1)! O(1) ! O(1) O(n)! ! O(1) O(n)! O(1) O(n)! 7 - - O(log n)! ! O(log n)! O(log n)! O(log n) insert! ! erase! find! lower,upper_bound 7. rehash unordered_*::rehash(size_t bucket_count);! ( = size() / bucket_count() ); nextprime(2 * bucket_count()); // GCC only O(n) O(n2). 8 8. 9 template,! class KeyEqual = std::equal_to,! class Allocator = std::allocator! > class unordered_set; 9. std::hash ; ; std::string, wstring, u16string, u32string;! std::unique_ptr, std::shared_ptr;! std::error_code, std::thread::id;! std::bitset, std::vector. 10 : 10. std::hash std::hash : 11 std::vector, list, deque, array, set, map, ; std::pair, std::tuple. 11. class A {! public:! bool operator == (const A& rhs) const;! friend class std::hash;! ! private:! int x_;! std::string y_;! }; 12 12. namespace std {! template! struct hash {! size_t operator()(const A& rhs) const {! size_t val = 0;! val += hash()(rhs.x_) * 31;! val += hash()(rhs.y_) * 59;! // boost::hash_combine! return val;! }! };! } 13 13. 14 14. ? const A* a = new A;! ! delete a; ! throw std::exception();! 15. 16 template ! class smart_ptr! {! T* ptr;! public:! explicit smart_ptr(T* p) : ptr(p) {}! ~smart_ptr() { delete ptr; }! };! 16. std::auto_ptr! deprecated ++11 ! 17 ! : auto_ptr(auto_ptr& other)! ! std::auto_ptr x = y; // y 17. 18 #include ! ! std::unique_ptr! std::shared_ptr! std::weak_ptr 18. std::unique_ptr std::auto_ptr;! ! auto x = std::unique_ptr(new A);! auto y = x; // auto z = std::move(x); // 19 19. ; ; ; ; ; 20 20. class A {! public:! // ! ! private:! // ! class AImpl;! smart_ptr impl_;! }; 21 21. class A;! A* a = f();! delete a; // undefined behaviour 22 22. template! > class unique_ptr; 23 23. const Resource* resource = ! acquireResource();! ! std::unique_ptr up(! ! resource,! ! releaseResource);! // , up ! // ! 24 24. std::unique_ptr up(new Derived);! ! 25 ! std::unique_ptr b;! std::unique_ptr d(new Derived);! b = std::move(d); 25. std::make_unique (C++14!) std::unique_ptr up(new ! TransactionalDataSourceContextBase(1,2,3)); 26 26. std::make_unique (C++14!) f(unique_ptr(new A(1,2,3)),! unique_ptr(new B(1,2)));! ! auto up = std::make_unique(1,2,3); 27 27. std::shared_ptr auto x = std::shared_ptr(new A);! auto y = x; // // A , ! auto z = std::move(x); // , . 28 ; 28. 29 ptr ref Object shared_ptr! ! ! 29. std::make_shared 30 ptr ref Object shared_ptr! ! ! ( ) Exception safe We know were you live optimization 30. 31 31. std::thread;! std::async, std::future;! mutexes;! atomics;! conditional variables;! thread_local data. 32 32. std::thread #include ! void f();! ! struct Doer {! void operator()() const;! };! ! int main() {! std::thread th1(f); // ! std::thread th2(Doer{});! std::thread th3([]{ ... });! ! th1.join(); th2.join(); th3.join();! ! return 0;! } 33 33. ! ! ! ! ! { ! std::thread th(...);! // :! join(); detach(); std::terminate(); // ?! } std::thread 34 34. std::vector> results;! for (int i=0; i