Answers for "stl vector c++"

C++
1

how to use vectors c++

#include <iostream>
#include <vector>
using namespace std;

int main() {
  //vector element size
  const int size = 4; 
  //vector with int data type
  //all elements are equal to 4
  vector<int> myVect (size, 4);

  for (int i=0; i<size; i++) {
    cout << "Vector index(" << i <<") is: "<< myVect[i] << endl; 
  }
  return 0;
}
Posted by: Guest on October-31-2020
0

C++ stl vector basic

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v;
    vector<int> a(5,1);
    vector<int> last(a);
    cout<<"Print last"<<endl;
    for(int i:last){
        cout<<i<<" ";
    }cout<<endl;
    cout<<"capacity "<<v.capacity()<<endl;
    v.push_back(1);
    cout<<"capacity "<<v.capacity()<<endl;
    v.push_back(2);
    cout<<"capacity "<<v.capacity()<<endl;
    v.push_back(3);
    cout<<"capacity "<<v.capacity()<<endl;
    cout<<"size "<<v.size()<<endl;
    cout<<"Element at second index "<<v.at(2)<<endl;
    cout<<"front "<<v.front()<<endl;
    cout<<"back "<<v.back()<<endl;
    
   
}
Posted by: Guest on September-11-2021

Browse Popular Code Answers by Language