shreyanshgupta838's blog

By shreyanshgupta838, history, 5 years ago, In English

Vector is a Data Type which can solve your most of the problems very efficiently.We Visit different websites and get confused, but with this article your all doubts about vector would be cleared,atleast you would know the important topics and can read more about it from different websites.So As a Competitive Programmer You Must Know it.

In the following few lines I will provide you some interesting features of vector which will make you love this data type. Here are some necessary features of vector you must know:-

Different methods of initilisation


Here i have used 'int' as data type, you can use any other data type also.

1 vector v; //vector declaration without predefined size v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40);

2. vector v(n); //by defining the vector size n. Assume n to be 4 v[0]=10; v[1]=20; v[2]=30; v[3]=40;

3. vector v(n,10); //here n is the size and the above statement will copy 10 to all the indices in the vector

4. vector v{ 10, 20, 30, 40 }; //same as array initialization

5. vector v1{10,20,30,40}; vector v2(v1.begin(),v1.end()) //initializing from another vector

6. int a[] = {10,20,30,40} int n = sizeof(a)/sizeof(a[0]); vector v(a,a+n); //initialization through array

7. vector v; v += 1,2,3,4; If other libraries are allowed. One can use #include <boost/assign/std/vector.hpp> header file It's only for knowledge you don't need to use this.

Different ways of element access

First Way : Same Like Array vector v{10,20,30};

for(int i=0;i<v.size();i++) { cout<<v[i]<<" "; }

Second Way

for(auto it=v.begin();it!=v.end();it++) { cout<<*it<<" "; }

Different Necessary Functions of vector

1. begin()-Returns an iterator pointing to the first element in the vector.begin()

2. end()-Returns an iterator pointing to the theoretical element that follows the last element in the vector.end()

3. size()- Returns the number of elements in the vector.size()

4 empty()-Returns whether the container is empty.empty()

5 reserve()-Requests that the vector capacity be at least enough to contain n elements.reserve()

6 push_back()- It push the elements into a vector from the back.push_back()

7 pop_back()-It is used to pop or remove elements from a vector from the back.pop_back()

8 insert() — It inserts new elements before the element at the specified position.insert()

9 erase() — It is used to remove elements from a container from the specified position or range.erase()

10 clear() — It is used to remove all the elements of the vector container.clear()

For more functions you can visit geeks for geeks

Some Clever tricks With which Vector becomes very efficient

1 If you are using mapped values but not able to sort it then use it inside vector.

include<bits/stdc++.h> using namespace std;

int main() { // Declaring vector of pairs vector< pair <int,int> > vect;

int arr[] = {10, 20, 5, 40 }; 
int arr1[] = {30, 60, 20, 50}; 
int n = sizeof(arr)/sizeof(arr[0]); 

/
for (int i=0; i<n; i++) 
    vect.push_back( make_pair(arr[i],arr1[i]) ); 

cout << "The vector before sort operation is:\n" ; 
for (int i=0; i<n; i++) 
{ 

    cout << vect[i].first << " "
       << vect[i].second << endl; 

} 

// Using simple sort() function to sort 
sort(vect.begin(), vect.end()); 

// Printing the sorted vector(after using sort()) 
cout << "The vector after sort operation is:\n" ; 
for (int i=0; i<n; i++) 
{ 

    cout << vect[i].first << " "
       << vect[i].second << endl; 
} 

return 0; 

}

Output

The vector before applying sort operation is:
10 30
20 60
5 20
40 50
The vector after applying sort operation is:
5 20
10 30
20 60
40 50

2.Deleting Repeating Element in vector in two lines

vector v{30,20,30,10,15};

sort(vect.begin(),vect.end());

vect.erase(unique(vect.begin(),vect.end()),vect.end());

int n = sizeof(arr)/sizeof(arr[0]);

for (int i=0; i<n; i++)

cout << vect[i] << " ";

Output ~~~~~ 10 15 20 30 ~~~~~

3.Finding Permutations

int arr[] = {5, 10, 15, 20, 20, 23, 42, 45};

int n = sizeof(arr)/sizeof(arr[0]);

vector<int> vect(arr, arr+n);

next_permutation(vect.begin(), vect.end());

cout << "\nVector after performing next permutation:\n";

for (int i=0; i<n; i++)

    cout << vect[i] << " ";

 prev_permutation(vect.begin(), vect.end());

cout << "\nVector after performing prev permutation:\n";

for (int i=0; i<n; i++)

   cout << vect[i] << " "

Output ~~~~~ Vector after performing next permutation:

5 10 15 20 20 23 45 42

Vector after performing prev permutation:

5 10 15 20 20 23 42 45 ~~~~~

This Much Information is Enough for you If you are new with vectors,with time you will come to know about its features automatically.

I Hope you understand The importance of Vector in CP.

Full text and comments »

  • Vote: I like it
  • -20
  • Vote: I do not like it