Answers for "vector in c++ stl"

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
2

vector in c++

vector<int> g1; 
  
    for (int i = 1; i <= 5; i++) 
        g1.push_back(i); 
  
    cout << "Output of begin and end: "; 
    for (auto i = g1.begin(); i != g1.end(); ++i) 
        cout << *i << " "; 
  
    cout << "nOutput of cbegin and cend: "; 
    for (auto i = g1.cbegin(); i != g1.cend(); ++i) 
        cout << *i << " "; 
  
    cout << "nOutput of rbegin and rend: "; 
    for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir) 
        cout << *ir << " ";
Posted by: Guest on February-02-2021
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