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.

1. Initial

/************** 1. Initializing like arrays **************/
vector<int> vect{1, 2, 3, 4};

/***** 2. Specifying size and initializing all values *****/
// Create a vector of size n with all values as 10.
int n = 3;
vector<int> vect(n, 10);

2. erase and clear

refer to: https://www.geeksforgeeks.org/vector-erase-and-clear-in-cpp/
erase : erase() function is used to remove elements from a container from the specified position or range.
clear : clear() function is used to remove all the elements of the vector container, thus making it size 0.

/**************** erase ****************/
// Removing an element from a particular position:
vector<int> myvector{ 1, 2, 3, 4, 5 };
vector<int>::iterator it = myvector.begin();
myvector.erase(it);

//Removing elements within a range:
vector<int> myvector{ 1, 2, 3, 4, 5 };
vector<int>::iterator it1, it2;
it1 = myvector.begin();
it2 = myvector.end();
myvector.erase(it1, it2);

/**************** clear ****************/
vector<int> myvector;
myvector.push_back(1);
myvector.push_back(2);
// Vector becomes 1, 2

myvector.clear();
// vector becomes empty

3. Copy

/******************** Deep copy ********************/
// 1. Loop push back
vector<int> vect1{1, 2, 3, 4}; 
vector<int> vect2; 
for (int i=0; i<vect1.size(); i++) 
        vect2.push_back(vect1[i]); 

// 2. Constructor
vector<int> vect1{1, 2, 3, 4};
vector<int> vect2(vect1);

// 3. assign
vector<int> vect1{1, 2, 3, 4};
vector<int> vect2;
vect2.assign(vect1.begin(), vect2.end());

// 4. insert
vector<int> vect1{1, 2, 3, 4};
vector<int> vect2;
vect2.insert(vect2.begin(), vect1.begin(), vect1.end());

4. Insert

/******** insert element ********/
vector<int> vect{1, 2, 3, 4};
vect.insert(vect.begin(), 100);  // insert to position 0
vect.insert(vect.begin() + 1, 100);  // insert to position 1

/******** insert vector ********/
// Deep copy
vector<int> vect1{1, 2, 3, 4};
vector<int> vect2;
vect2.insert(vect2.begin(), vect1.begin(), vect1.end());

5. reverse

vector<int> a = { 1, 45, 54, 71, 76, 12 };
reverse(a.begin(), a.end());  // reverse(start_index, last_index);

6. get last element

vector<int> a = { 1, 45, 54, 71, 76, 12 };
int b = a.back();

7. exist

// check if single element exists in vector
vector<int> vec{1,2,3};
int element = 2;

if(find(vec.begin(), vec.end(), element) != vec.end())
{
    // found
}

// check if sub vector exists in vector
vector<int> vec{1,2,3};
vector<int> subVec{2,3};

if(search(vec.begin(), vec.end(), subVec.begin(), subVec.end()) != vec.end())
{
    // found
}

8. locate element or subvector

// single element
int getIntVectorElementIndex(vector<int> v, int K)
{
    auto it = find(v.begin(), v.end(), K);
  
    // If element was found
    if (it != v.end()) 
    {
        // calculating the index
        int index = it - v.begin();
        return index;
    }
    else {
        // If the element is not present in the vector
        return -1;
    }
}

// sub vector
int getIntVectortSubVectorIndex(vector<int> v, vector<int> subV)
{
    auto it = search(v.begin(), v.end(), subV.begin(), subV.end());

    // If element was found
    if (it != v.end()) 
    {
        // calculating the index
        int index = it - v.begin();
        return index;
    }
    else {
        // If the element is not present in the vector
        return -1;
    }
}

9. get max or min element

// array
int a[6] = { 1, 45, 54, 71, 76, 12 };
auto b = *max_element(a, a+6);
auto c = *min_element(a, a+6);

// vector
vector<int> a = { 1, 45, 54, 71, 76, 12 };
auto b = *max_element(a.begin(), a.end());
auto c = *min_element(a.begin(), a.end());

10. get sum

Using Accumulate Method
The accumulate method in c++ used to find the array sum. This function can be accessed from the numeric library in c++.

// array
#include <numeric>
int arr[] = { 1, 5};
int n = 2, initial_sum = 0; sum = 0;
sum = accumulate(arr, arr+n, initial_sum);  // n=0 sum=0; n=1 sum=1; n=2 sum=6

// vector
#include <numeric>
vector<int> v{1, 2, 3};
int initial_sum = 0, sum = 0;
sum = accumulate(v.begin(), v.begin() + 1, initial_sum);  // sum = 1
posted @ 2022-12-06 09:40  shendawei  阅读(35)  评论(0)    收藏  举报