Quick actions

cmd+k|ctrl+k

Navigation

Languages

vectors in cpp

Snippet info

Language

Cpp

Visibility

public

Author

umashankar

Created

2021-04-28T18:30:06.850376Z

Updated

2021-04-30T06:24:30.308331Z

#include <iostream>
#include <vector>
/*

    Vectors are same as dynamic arrays with the ability to resize itself
    automatically when an element is inserted or deleted, with their storage being handled automatically
    by the container

*/
using namespace std;

int main() {
    cout << "Enter number of elements you want to enter into Vector: ";
    int n;
    cin >> n;
    vector<int> v1 = {1,2,3,6,4};
    cout << "The vector is : \n";
    for(int i=0; i<v1.size(); ++i){
        cout << v1[i] << "\t";
    }
    // cout << "Another way to print the vector is: \t";
    // for(int i= v1.begin(); i
    cout << "\nThe ending value of vector v1 is: ";
    cout << *(v1.end());
    cout << "\nAnother way to Print the vector is: \t";
    
    //cend points to next element of last element in the vector
    for(auto i = v1.cbegin(); i!=v1.cend(); ++i){
        cout << *i << "\t";
    }
    vector<int> v2{10,12,123};
    fill(v2.begin(),v2.end(),5);
    cout << "\nSecond vector is: \t";
    for(auto i = v2.cbegin(); i != v2.cend(); ++i){
        cout << *i << "\t";
    }
    cout << "\nPrinting the vector in Reverse order: \t";
    for(auto i = v1.rbegin();i != v1.rend(); ++i){
        cout << *i << "\t";
    }
    v1.swap(v2);
    cout << "\nElements in vector1 after swapping : \t";
    for(auto i = v1.cbegin();i!=v1.cend();++i){
        cout << *i << "\t";
    }
    cout << "\nPrinting cend() by dereferencing : " << *(v1.cend());
    cout << "\nPrinting cbegin() by dereferencing : " << *(v1.begin());
    cout << "\n\n The capacity of vector1 with 3 elements is: " << v1.capacity();
    vector<int> v3;
    cout << "\n The capacity of v3 without initializing: " << v3.capacity();
    v3.assign({1,2,3});
    cout << "\n The capacity of v3 with 3 elements is : " << v3.capacity();
        cout << "\n\n The value of rbegin is : " << *(v3.rbegin());
    cout << "\n\n The value of rend is : " << *(v3.rend());
    
    return 0;
}
INFO