Answers for "vector function in c++"

C++
3

vector functions c++

/*Member functions*/
                 Iterators
 -----------------------------------------
 begin | Returns an iterator to the beginning
 end   | Returns an iterator to the end
  
                 Capacity
 -----------------------------------------
 empty    | Checks whether the container is empty
 size     | Returns the number of elements
 reserve  | Reserves storage
 capacity | Returns the number of elements that can be held in currently allocated storage
     
              Element access
 -----------------------------------------
 at	     	| Access specified element with bounds checking
 front  	| Access the first element
 back   	| Access the last element
 operator[] | Access specified element
    
                 Modifiers
 -----------------------------------------
 clear        | Clears the contents
 insert       | Inserts elements
 emplace      | Constructs element in-place
 erase        | Erases elements
 push_back    | Adds an element to the end
 emplace_back | Constructs an element in-place at the end  
 pop_back     | Removes the last element        
 resize       | Changes the number of elements stored     
 swap         | Swaps the contents
  
  *Notes*
  - https://en.cppreference.com/w/cpp/container/vector
  - https://www.geeksforgeeks.org/vector-in-cpp-stl/
  - https://www.tutorialspoint.com/cpp_standard_library/vector.htm
Posted by: Guest on May-13-2021
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

vector operations in c++

#include <iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    int n;
    cin>>n;
    vector<int>vec(n);
    for(int i=0;i<n;i++)
    {
        cin>>vec[i];
    }
    cout<<"----------"<<endl;
    for(int i=0;i<n;i++)
    {
      cout<<vec[i]<<" ";
    }
    vec.push_back(100);
    vec.push_back(200);
    cout<<endl;
    auto it=vec.begin();
    vec.insert(it,3,1000);
    for(vector<int>::iterator it1=vec.begin();it1!=vec.end();it1++)
    {
        cout<<*it1<<" ";
    }
    cout<<endl;
    vector<int>vec2;
    vec2.push_back(15);
    vec2.push_back(25);
    vec2.push_back(35);
    vec.insert(it,vec2.begin(),vec2.end());
    for(vector<int>::iterator it1=vec.begin();it1!=vec.end();it1++)
    {
        cout<<*it1<<" ";
    }
    cout<<endl;
    vec.pop_back();
    vec.pop_back();
    for(vector<int>::iterator it1=vec.begin();it1!=vec.end();it1++)
    {
        cout<<*it1<<" ";
    }
    cout<<endl;
    int k;
    cin>>k;
    cout<<endl;
    vector<int>::iterator its=vec.begin()+k-1;
    int s=vec.size();
    cout<<s<<endl;
    vec.erase(its);
    for(vector<int>::iterator it1=vec.begin();it1!=vec.end();it1++)
    {
        cout<<*it1<<" ";
    }
    

    return 0;
}
Posted by: Guest on June-04-2021

Browse Popular Code Answers by Language