Showing posts with label Vector. Show all posts
Showing posts with label Vector. Show all posts

vector swap() in C++

swap(): It is a build-in function in STL. It swaps data with another vector.

File: stl_vector.h

Parameter:

x – A vector of the same element and allocator types. This exchanges the elements 

between two vectors in constant time. 

Syntax:

vec.swap(x)

For Example:

vec ={1,2,3,4}

x  = {5,4,3,2,1}

vec.swap(x) =>

vec = {5,4,3,2,1}

x = {1,2,3,4}

Approach

C++

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

int main()
{
    vector<intvec1 = {1234};

    vector<intvec2 = {54321};

    vec1.swap(vec2);

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

    cout << "\n";
    for (int i = 0i < vec2.size(); i++)
        cout << vec2[i] << " ";
    
    return 0;
}


vector size() in C++

size(): It returns the number of elements in the vector. 

File: stl_vector.h

Parameter: No parameters are required for this function.

Syntax:

vecName.size()

For Example:

vec ={1,2,3,4,5}

vec.size() = > It returns size of vector (i.e 5)

Approach

C++

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

int main()
{
    vector<intvec = {12345};

    cout << vec.size() << "\n";

    return 0;
}


vector shrink_to_fit() in C++

shrink_to_fit(): It is a build-in function in STL. 

A non-binding request to reduce capacity() to size(). 

File: stl_vector.h

Parameter: No parameter required.

Syntax:

vecName.shrink_to_fit()

For Example :

vec ={1,2,3,4,5}

capacity  = 8, size = 5

vec.shrink_to_fit()

capacity = 5, size  = 5

Approach

C++

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

int main()
{
    vector<intvec;
    for (int i = 0i < 5i++)
        vec.push_back(i + 1);

    cout << vec.capacity() << " " << vec.size() << "\n";

    vec.shrink_to_fit();

    cout << vec.capacity() << " " << vec.size() << "\n";
 
    return 0;
}


vector resize() in C++

resize(): It resizes the vector to the specified number of elements. This function will resize the vector 

to the specified number of elements. 

If the number is smaller than the vector's current size then the vector is truncated, otherwise 

default constructed elements are appended.

Parameters:

new_size:- Number of elements the vector should contain.

Syntax:

vecName.resize(new_size)

For Example:

vec ={1,2,3,4}

vec.resize(10) => It resizes the vector of 10 length.

Approach

C++

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

int main()
{
    vector<intvec = {1234};

    vec.resize(10);

    cout << vec.size() << "\n";
    
    return 0;
}


vector reserve() in C++

reserve(): It attempts to preallocate enough memory for the specified number of elements. 

Parameters: 

n: Number of elements required. 

File: vector.tcc

Syntax:

vecName.reserve(n)

For Example:

vector<int> vec

vec.reserve(5) => It allocate size of 5.

Approach

C++

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

int main()
{
    vector<intvec;

    vec.reserve(5);
    for (int i = 1i <= 5i++)
    {
        vec[i - 1] = i;
    }
    for (int i = 0i < 5i++)
        cout << vec[i] << " ";
    
    return 0;
}


vector rend() in C++

rend(): It returns a read/write reverse iterator that points to one before the first element in the vector.

Note: Iteration is done in reverse element order. 

File: stl_vector.h

Parameter: No parameters required.

Syntax:

vector<data_type>::reverse_iterator iteratorName = vecName.rend()

For Example:

vec  ={1,2,3,4}

vec.rend() => It points one before the first element (i.e 1).

Approach

C++

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

int main()
{
    vector<intvec = {1234};

    vector<int>::reverse_iterator it = vec.rend();

    cout << *(it - 1<< "\n";
    
    return 0;
}


vector rbegin() in C++

rbegin(): It returns a read/write reverse iterator that points to the last element in the vector. 

Note: Iteration is done in reverse element order. 

File: stl_vector.h

Parameter: No parameters required for this function.

Syntax:

vector<data_type>::reverse_iterator iteratorName = vecName.rbegin()

For Example:

vec ={1,2,3,4}

it = vec.rbegin() = > It points to the last element (i.e 4)

Approach

C++

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

int main()
{
    vector<intvec = {1234};

    vector<int>::reverse_iterator it = vec.rbegin();

    cout << *it << "\n";
    return 0;
}


vector push_back() in C++

push_back(): It adds element at end of the vector. 

By this size of the vector increases by 1.

Parameter : 

data: Data to be adds at the end of the vector.

Syntax:

vecName.push_back(data)

For Example:

vec = {1,2,3,4}

vec.push_back(5)= > It adds 5 at end of the vector.

vec = {1.2.3.4.5}

Approach

C++

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

int main()
{
    vector<intvec = {1234};

    vec.push_back(5);

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


vector pop_back() in C++

pop_back(): It removes the last element from the vector. 

This is a typical stack operation. It shrinks the vector by one.

Note that no data is returned, and if the last element's data is needed, it should be retrieved before pop_back() is called.

Parameters: No parameters required for this function.

Syntax:

vecName.pop_back()

For Example:

vec = {1,2,3,4,5}

vec.pop_back() => It removes last element (i.e 5) from the vector.

So vector becomes ={1,2,3,4}

Approach

C++

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

int main()
{
    vector<intvec = {12345};

    vec.pop_back();

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

    return 0;
}


vector max_size() in C++

max_size(): It is a build-in function in STL. It returns the size() of the

largest possible vector.

File: stl_vector.h

Parameter: No parameters are required for this function.

Syntax:

vecName.max_size()

For Example:

vec = {1,2,3,4}

vec.max_size() = > It retuns max size (4611686018427387903).

Approach

C++

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

int main()
{
    vector<intvec = {1234};

    //prints the maximum size of vector
    cout << vec.max_size() << "\n";

    return 0;
}


vector insert() in C++

insert(): It is a build-in function in STL. It inserts the given value into a vector before 

specified iterator.

It returns an iterator that points to the inserted data. This function will insert a copy of the given value

before the specified location. 

Note that this kind of operation could be expensive for a vector.

Parameters: 

position: A const_iterator into the vector.

data: Data to be inserted. 

Syntax:

vecName.insert(position, data) 

For Example:

vec = {1,3,4,5}

vec.insert(vec.begin()+1,2) = > It insert 2 after first element of the vector.

So, vector becomes (i.e vec = {1,2,3,4,5})

Approach

C++

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

int main()
{
    vector<intvec = {1345};

    //insert element after first element
    //of the vector
    vec.insert(vec.begin() + 12);

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

    return 0;
}


vector front() in C++

front(): It is a build-in function in STL. It returns a read/write reference to 

the data at the first element of the vector.

Parameter: No parameter is required for this function.

Syntax:

vecName.front()

For Example:

vec = {1,2,3,4}

vec.front() = > It returns the first element (i.e 1)

Approach

C++

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

int main()
{
    vector<intvec = {1234};

    //print the first element of the vector
    cout << vec.front() << "\n";

    return 0;
}


vector erase() in C++

erase(): It is a build-in function in STL. It is used to remove element at given position from the

vector. 

It returns an iterator pointing to the next element (or end()) of the given vector.

This function will erase the element at the given position and thus shorten the vector by one. 

Note: This operation could be expensive.

Parameters

position: Iterator pointing to element to be erased.

Syntax:

vecName.erase(position)

For Example:

vec = {1,2,3,4}

vec.erase(vec.begin()) = > It removes 1 from the vector.

So , vector becomes {2,3,4}

Approach

C++

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

int main()
{
    vector<intvec = {1234};

    vec.erase(vec.begin());

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

    return 0;
}


vector end() in C++

end(): It returns a read/write iterator that points to one past the last element in the vector.

Note: Iteration is done in ordinary element order.

File: stl_vector.h

Parameter: No parameter is required for this function.

Syntax:

vecName.end()

For Example:

vec = {1,2,3,4,5}

vec.end() = > It points beyond 5.

So, to print last element we can subtract 1 from the iterator and then print.

Approach

C++

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

int main()
{

    vector<intvec = {12345};

    vector<int>::iterator it = vec.end();

    cout << *(it - 1<< "\n";

    return 0;
}


vector empty() in C++

empty(): It returns true if the vector is empty , otherwise returns false.

If vector is empty then, begin() == end().

Parameters: Their is no parameter for this function.

Syntax:

vecName.empty()

For Example:

vec = {1,2,3,4}

vec.empty() => It returns false.

vec = {}

vec.empty() = > It returns true

Approach

C++

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

int main()
{
    vector<intvec = {1234};

    if (vec.empty())
        cout << "Vector is empty\n";
    else
        cout << "Vector is not empty\n";

    return 0;
}


vector emplace() in C++

emplace(): It is a build-in function in STL. It inserts an object in a vector before a specified position. It returns

an iterator that points to the inserted data.

Parameters:

position: It is the constant iterator to the vector.

value: It is the value to be inserted.

Syntax:

vecName.empace(position, value)

Note: This kind of operation are expensive for a vector.

For Example:

vec= {1,2,4,5}

vec.emplace(vec.begin()+2,3) = > vector becomes  {1,2,3,4,5}

Approach

C++

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

int main()
{
    vector<int> vec = {1245};

    auto it = vec.begin();

    it = it + 2;
    vec.emplace(it, 3);

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

    return 0;
}


vector data() in C++

data(): It is a build-in function in the STL library. It returns the pointer to the first element of the

vector.

Syntax:

dataType *pointer = vecName.data()

Parameters:

Their is no parameter requird for this funstion

File: stl_vector.h

For Example:

vec = {1,2,3,4,5}

*pointer = vec.data() => It points to the first element (i.e 1)

Approach

C++

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

int main()
{
    vector<intvec = {12345};

    int *it = vec.data();

    for (int i = 0i < vec.size(); i++)
        cout << *it++ << " ";

    return 0;
}