Advertisement
fcamuso

C++ 20 - lezione 6

Oct 16th, 2022
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <format>
  3. #include <span>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int sommatoria(const int v[], size_t dimensione)
  9. {
  10.     int risultato = 0;
  11.     for (size_t i = 0; i < dimensione; i++) risultato += v[i];
  12.  
  13.     return risultato;
  14. }
  15.  
  16. void reset(span<int> v)
  17. {
  18.     for (int &n : v) n = 0;
  19. }
  20.  
  21. int sommatoria_con_span(span<const int> v)
  22. {
  23.     int risultato = 0;
  24.     for (int n : v) risultato += n;
  25.  
  26.     return risultato;
  27. }
  28.  
  29. void stampa(span <const int> v)
  30. {
  31.     for (int n : v) cout << n << " ";
  32.     cout << endl;
  33. }
  34.  
  35.  
  36.  
  37. int main()
  38. {
  39.     int valori[]{ 20, 45, 71, 100, -87, 22, 9 };
  40.     cout << format("La sommatoria vale: {}\n", sommatoria(valori, 3));
  41.     cout << format("La sommatoria vale: {}\n", sommatoria_con_span(valori));
  42.  
  43.     //reset(valori);
  44.     //stampa(valori);
  45.  
  46.     span<const int> v{ valori };
  47.     cout << format("Primo elemento span: {}, ultimo elemento: {}\n", v.front(), v.back());
  48.  
  49.     stampa(v.subspan(2, v.size()-4));
  50.     stampa(v.first(3));
  51.     stampa(v.last(3));
  52.  
  53.     cout << v[0] << endl;
  54.  
  55.     vector<int> vect{ 4,67,89,11 };
  56.     stampa(vect);
  57.  
  58.     span<int> span{ vect };
  59.  
  60.    
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement