federico-busato / Modern-CPP-Programming

Modern C++ Programming Course (C++03/11/14/17/20/23/26)
https://federico-busato.github.io/Modern-CPP-Programming/
11.91k stars 798 forks source link

Code fix in std::span example #76

Closed eugenefil closed 7 months ago

eugenefil commented 7 months ago

On p.41 in 17.Iterators_Containers_Alg.pdf under title "std::span":

-    std::array2<int, 3> array2 = {1, 2, 3};
+    std::array<int, 3> array2 = {1, 2, 3};
     std::span s2{array2}; // static extent

     auto array3 = new int[3];
-    std::span s3{array3}; // dynamic extent
+    std::span s3{array3, 3}; // dynamic extent

     std::vector<int> v{1, 2, 3};
     std::span s4{v.data(), v.size()}; // dynamic extent
+
+    std::span s5{v}; // dynamic extent

In hunk order:

  1. Typo: std::array2 -> std::array
  2. std::span can't be made from pointer, only from pointer and size like for s4
  3. It turns out span can be made from vector directly, see here. Could possibly be added to the list of examples.
federico-busato commented 7 months ago

thanks, @eugenefil. I didn't know that std::span can be created starting directly from a std::vector. I will add it