fork download
  1. #include <iostream>
  2. #include<vector>
  3. using namespace std;
  4. vector<int>a;//create a vector
  5. int main()
  6. {
  7. int n=10,temp;
  8. for(int i=0;i<n;i++)
  9. {
  10. cin>>temp;
  11. a.push_back(temp);//insert the element to the last position in the vector
  12. }
  13. int mean=0;
  14. for(int i=0;i<n;i++)
  15. {
  16. cout<<"Element "<<i<<":"<<a[i]<<endl;
  17. mean+=a[i];//you can access the elements as you do in an array
  18. }
  19. cout<<"mean:"<<mean/10<<endl;
  20. a.clear();//clear the vector
  21. a.push_back(10);
  22. cout<<a[0]<<endl;
  23. return 0;
  24. }
Success #stdin #stdout 0s 3032KB
stdin
1
2
3
4
5
6
7
8
9
10
stdout
Element 0:1
Element 1:2
Element 2:3
Element 3:4
Element 4:5
Element 5:6
Element 6:7
Element 7:8
Element 8:9
Element 9:10
mean:5
10