tddd38 726g82 advanced programming in c
play

TDDD38/726G82 - Advanced programming in C++ Introducon STL - PowerPoint PPT Presentation

TDDD38/726G82 - Advanced programming in C++ Introducon STL Christoffer Holm Department of Computer and informaon science 1 Introducon 2 IO 3 Sequenal Containers 1 Introducon 2 IO 3 Sequenal Containers 3 / 47


  1. 20 / 47 IO Error flags ‚ Mul�ple flags can be set at once ‚ except goodbit ; it is set when no other flag is set ‚ This means that several errors can occur at once ‚ Do note that these flags are set a�er a stream opera�on failed ‚ The stream does not magically detect an error if no opera�on has been performed

  2. 21 / 47 IO Conver�ng from strings int main(int argc, char* argv[]) int main(int argc, char* argv[]) { { int x; int x; istringstream iss{argv[1]}; try if (!(iss >> x)) { { x = stoi(argv[1]); // error } catch (invalid_argument& e) // reset flags { iss.clear(); // error } } // continue // continue } }

  3. 22 / 47 IO Conver�ng from strings istringstream version stoi version ` More general ` No extra objects ` Cheaper error path ` Easier error handling ´ Requires a stream ´ Expensive error path ´ Must check flags ´ Only works for int Prefer the istringstream version because of generality, but as always; there are no universal solu�ons

  4. 23 / 47 IO What will be printed? #include <sstream> #include <iostream> #include <string> using namespace std; int main() { stringstream ss{}; ss << "123a bc hello"; int n{}; char c{}; string str{}; if (ss >> n >> n >> c) cout << n << " "; ss.clear(); if (ss >> c >> c) cout << c << " "; ss.clear(); if (ss >> str) cout << str << " "; }

  5. 1 Introduc�on 2 IO 3 Sequen�al Containers

  6. 25 / 47 Containers Containers ‚ Sequen�al Containers ‚ Associa�ve Containers ‚ Container Adaptors

  7. 26 / 47 Sequen�al Containers Important concepts ‚ Memory alloca�ons ‚ Different containers have different models of alloca�on. ‚ Calling new is very slow, ‚ So the number of memory alloca�ons is an important factor in the effec�veness of a container ‚ CPU caching ‚ Pointer invalida�on

  8. 26 / 47 Sequen�al Containers Important concepts ‚ Memory alloca�ons ‚ CPU caching ‚ Modern CPU:s perform what is known as caching. ‚ Whenever the CPU fetch data from the RAM it will fetch a block of data and store that in the cache. ‚ Accessing data in the CPU cache is several magnitudes faster than accessing data in the RAM. ‚ Pointer invalida�on

  9. 26 / 47 Sequen�al Containers Important concepts ‚ Memory alloca�ons ‚ CPU caching ‚ We always read data in blocks, so we know that the element a�er the data we just read is almost guaranteed to be in the cache. ‚ So containers that read data in sequence is a lot faster than those that do not. ‚ Pointer invalida�on

  10. 26 / 47 Sequen�al Containers Important concepts ‚ Memory alloca�ons ‚ CPU caching ‚ On the flip side: if the elements of a container is spread all around the RAM, then it will be a lot slower since we almost always have to read the data from RAM rather than cache. ‚ Usually we talk about the cache locality of a container: how much of the cache it can leverage for speedups. ‚ Pointer invalida�on

  11. 26 / 47 Sequen�al Containers Important concepts ‚ Memory alloca�ons ‚ CPU caching ‚ Pointer invalida�on ‚ If we have pointers or references to data in containers we have to know whenever these gets invalidated . ‚ A pointer (or reference) points to a specific address in memory,

  12. 26 / 47 Sequen�al Containers Important concepts ‚ Memory alloca�ons ‚ CPU caching ‚ Pointer invalida�on ‚ So if the container for some reason moves the element to another address in memory, then the pointer doesn’t refer to the same element (and chances are it doesn’t even point to a valid object) ‚ This can prove to be a big impact in how we use containers.

  13. 27 / 47 Sequen�al Containers What is a sequen�al container? ‚ Data stored in sequence ‚ Accessed with indices ‚ Ordered but not (necessarily) sorted

  14. 28 / 47 Sequen�al Containers Which sequen�al containers are there? ‚ std::array ‚ std::vector ‚ std::list ‚ std::forward_list ‚ std::deque

  15. 29 / 47 Sequen�al Containers std::array 0 0 0 0 data std::array<int, 4> std::array<int, 4> array{};

  16. 29 / 47 Sequen�al Containers std::array 0 0 0 0 data std::array<int, 4> array[0] = 1;

  17. 29 / 47 Sequen�al Containers std::array 1 0 0 0 data std::array<int, 4> array[0] = 1;

  18. 29 / 47 Sequen�al Containers std::array 1 0 0 0 data std::array<int, 4> array[1] = 2;

  19. 29 / 47 Sequen�al Containers std::array 1 2 0 0 data std::array<int, 4> array[1] = 2;

  20. 29 / 47 Sequen�al Containers std::array 1 2 0 0 data std::array<int, 4> array[2] = 3;

  21. 29 / 47 Sequen�al Containers std::array 1 2 3 0 data std::array<int, 4> array[2] = 3;

  22. 29 / 47 Sequen�al Containers std::array 1 2 3 0 data std::array<int, 4> array[3] = 4;

  23. 29 / 47 Sequen�al Containers std::array 1 2 3 4 data std::array<int, 4> array[3] = 4;

  24. 30 / 47 Sequen�al Containers std::array ‚ inser�on: not applicable ‚ dele�on: not applicable ‚ lookup: O p 1 q

  25. 31 / 47 Sequen�al Containers std::array ` No memory alloca�ons ` Data never move in memory ´ Fixed size ´ Size must be known during compila�on

  26. 32 / 47 Sequen�al Containers Example #include <array> // ... int main() { std::array<int, 5> data{}; for (unsigned i{}; i < data.size(); ++i) { cin >> data.at(i); } for (auto&& i : data) { cout << i << endl; } }

  27. 33 / 47 Sequen�al Containers std::vector data size 0 capacity 4 std::vector<T> std::vector<int> vector{};

  28. 33 / 47 Sequen�al Containers std::vector data size 0 capacity 4 std::vector<T> vector.push_back(1);

  29. 33 / 47 Sequen�al Containers std::vector 1 data size 1 capacity 4 std::vector<T> vector.push_back(1);

  30. 33 / 47 Sequen�al Containers std::vector 1 data size 1 capacity 4 std::vector<T> vector.push_back(2);

  31. 33 / 47 Sequen�al Containers std::vector 1 2 data size 2 capacity 4 std::vector<T> vector.push_back(2);

  32. 33 / 47 Sequen�al Containers std::vector 1 2 data size 2 capacity 4 std::vector<T> vector.push_back(3);

  33. 33 / 47 Sequen�al Containers std::vector 1 2 3 data size 3 capacity 4 std::vector<T> vector.push_back(3);

  34. 33 / 47 Sequen�al Containers std::vector 1 2 3 data size 3 capacity 4 std::vector<T> vector.push_back(4);

  35. 33 / 47 Sequen�al Containers std::vector 1 2 3 4 data size 4 capacity 4 std::vector<T> vector.push_back(4);

  36. 33 / 47 Sequen�al Containers std::vector 1 2 3 4 data size 4 capacity 4 std::vector<T> vector.push_back(5);

  37. 33 / 47 Sequen�al Containers std::vector 1 2 3 4 data size 5 capacity 8 std::vector<T> vector.push_back(5);

  38. 33 / 47 Sequen�al Containers std::vector 1 2 3 4 5 data size 5 capacity 8 std::vector<T> vector.push_back(5);

  39. 33 / 47 Sequen�al Containers std::vector 1 2 3 4 5 data size 5 capacity 8 std::vector<T> vector.erase(vector.begin() + 2);

  40. 33 / 47 Sequen�al Containers std::vector 1 2 4 5 data size 4 capacity 8 std::vector<T> vector.erase(vector.begin() + 2);

  41. 33 / 47 Sequen�al Containers std::vector 1 2 4 5 data size 4 capacity 8 std::vector<T> vector.erase(vector.begin() + 2);

  42. 33 / 47 Sequen�al Containers std::vector 1 2 4 5 data size 4 capacity 8 std::vector<T> vector.erase(vector.begin() + 2);

  43. 33 / 47 Sequen�al Containers std::vector 1 2 4 5 data size 4 capacity 8 std::vector<T> vector.erase(vector.begin() + 2);

  44. 33 / 47 Sequen�al Containers std::vector 1 2 4 5 data size 4 capacity 8 std::vector<T> vector.erase(vector.begin() + 2);

  45. 34 / 47 Sequen�al Containers std::vector ‚ inser�on: ‚ at end: O p 1 q ‚ otherwise: O p n q ‚ dele�on: ‚ last element: O p 1 q ‚ otherwise: O p n q ‚ lookup: O p 1 q

  46. 35 / 47 Sequen�al Containers std::vector ` Data is sequen�al in memory ` Dynamic size ´ En�re data range can move in memory ´ Dynamic alloca�ons are slow

  47. 36 / 47 Sequen�al Containers Example #include <vector> // ... int main() { std::vector<int> data{}; int x{}; while (cin >> x) { data.push_back(x); } for (auto&& i : data) cout << i << endl; }

  48. 37 / 47 Sequen�al Containers std::list first last size 0 std::list<T> std::list<int> list{};

  49. 37 / 47 Sequen�al Containers std::list first last size 0 std::list<T> list.push_back(1);

  50. 37 / 47 Sequen�al Containers std::list first 1 last size 1 std::list<T> list.push_back(1);

  51. 37 / 47 Sequen�al Containers std::list first 1 last size 1 std::list<T> list.push_back(2);

  52. 37 / 47 Sequen�al Containers std::list first 1 2 last size 2 std::list<T> list.push_back(2);

  53. 37 / 47 Sequen�al Containers std::list first 1 2 last size 2 std::list<T> list.push_back(3);

  54. 37 / 47 Sequen�al Containers std::list first 1 2 3 last size 3 std::list<T> list.push_back(3);

Recommend


More recommend