fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int i;
  9.  
  10. // vectors are data types that allow one to store an array
  11. // of items, each having the same type (for example, here
  12. // we create a vector with name v of 10 integers
  13.  
  14. vector<int> v(10);
  15.  
  16. // to move through an array, we can access each element by
  17. // its index, in our case the element with index i is v[i];
  18. // index starts at 0, so for us the last index is 9
  19.  
  20. for (i=0; i<10; i++)
  21. v[i] = i%8;
  22.  
  23. // number of elements in a vector is accessed by ".size()"
  24. // after the vector's name (this is a function that returns
  25. // the size of the vector); the elements can be accessed
  26. // as described above; this prints out the vector (incl.
  27. // size)
  28.  
  29. cout << "vector size: " << v.size() << endl;
  30.  
  31. cout << "elements: ";
  32. for (i=0; i<10; i++)
  33. cout << " " << v[i];
  34. cout << endl;
  35.  
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0.02s 2856KB
stdin
Standard input is empty
stdout
vector size: 10
elements:    0 1 2 3 4 5 6 7 0 1