fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. bool myfunction(int i, int j){
  6. return (i<j);
  7. }
  8. int main()
  9. {
  10. //1
  11. vector<int> myvector;
  12. //2
  13. for(int i=0; i<10; i++) myvector.push_back(i);
  14. cout<<"size"<<myvector.size()<<"\n";
  15. cout<<"capacity: "<<myvector.capacity()<<"\n";
  16. for(int i=0; i<7; i++){
  17. int n;
  18. cin>>n;
  19. myvector.push_back(n);
  20. }
  21. //3
  22. vector<int>::iterator it2;
  23. it2=myvector.begin();
  24. it2+=2;
  25. cout<<*it2;
  26. it2=myvector.end();
  27. cout<<*it2;
  28. for(vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
  29. cout<<' '<<*it;
  30. cout<<endl;
  31. //4
  32. myvector.pop_back();
  33. for(vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
  34. cout<<' '<<*it;
  35. //5
  36. cout<<endl<<myvector.front()<<" "<<myvector.back()<<endl;
  37. //6
  38. sort(myvector.begin(), myvector.end(), myfunction);
  39. for(vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
  40. cout<<' '<<*it;
  41. //7
  42. cout<<endl;
  43. cout<<myvector.size()<<endl;
  44. //8
  45. bool comp=false;
  46. int x=3;
  47. for(vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it){
  48. if(*it==x){
  49. comp=true;
  50. }
  51. }
  52. cout<<comp<<endl;
  53. //9
  54. myvector.clear();
  55. cout<<myvector.size();
  56. /*
  57.   cout<<"size"<<myvector.size()<<"\n";
  58.   cout<<"capacity: "<<myvector.capacity()<<"\n";
  59.   cout<<"max-size: "<<myvector.max_size()<<"\n";
  60.   for(int i=0; i<7; i++){
  61.   myvector.pop_back();
  62.  
  63.  
  64.   cout<<"size"<<myvector.size()<<"\n";
  65.   cout<<"capacity: "<<myvector.capacity()<<"\n";
  66.  }
  67.  */
  68. return 0;
  69. }
Success #stdin #stdout 0.01s 5292KB
stdin
1 2 3 4 5 6 7 8 9 0
stdout
size10
capacity: 16
20 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7
 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6
0 6
 0 1 1 2 2 3 3 4 4 5 5 6 6 7 8 9
16
1
0