Vectors & Array : C++ cheatsheet
.inner-wrap {
margin: 0 auto;
max-width: 1750px;
}
Hi everyone. I tried to sum up all c++ vector operations required in competitive programming like leetcode , topcoder , hackerrank etc.. for quick reference. Please do comment if anything more can be added that I have missed and would be beneficial in the competition.
A.) Different ways of initializing vector array
B.) 2-D vector array Initialisation
C.) Insert into Vector [ arr.push_back(12) ]
D.) Remove elements from Vector By given index
E.) Remove Element from Vector by given value
F.) Sort Vector& custom sort
G.) Min & Max in one line :
auto lu = minmax_element(nums.begin(), nums.end()); int l = *lu.first, u = *lu.second;
A.) Different ways of initializing vector array
int n = 13 ; int arr[] = {7,3,5,9,1,2,4,6}; vector<int> arr1; vector<int> arr2(length); // Initialize length of vector array vector<int> arr3(length, value); // Initialize length of vector with default value vector<int> arr4{8,7,6,5,4,3,2,6,7}; OR vector<int> arr3 = {8,7,6,5,4,3,2,6,7} // default value array vector<int> arr5( arr , arr+2 ); // copy array value in vector arr4 = [7,3,5];
B.) 2-D vector array Initialisation
vector<vector<int>> arr(10 , vector<int>(50 , 3)); // 10 - rows with 50 column having default value 3.
C.) Insert into Vector
arr1d.erase(arr1.begin()+index-1); //Remove multiple index element ; Vectorvariable.erase(startPos , endPos) arr1d.erase( arr1.begin()+index1-1 , arr1.begin()+index5-1 ); // will erase element from index index1 to index5 //delete 0th index element arr1d.erase(0) //ERROR won't work arr1d.erase(arr1.begin() + 0) OR arr1d.erase(arr1.begin()) vector::clear will remove all elements from vector container making it size 0. (it doesn't deletes the element from memory) vector<int> arrClear{9,8,7,6,5,4,3,2,1}; arrClear.clear(); cout<<arrClear.size(); // prints 0 cout<<arrClear[2]; // prints 7
D.) Remove elements from Vector By given index
Function
1.) vector::clear: This will remove all elements from the vector container making it size 0. (it doesn’t delete the element from memory)
2.) vector::erase: This delete/remove element from the specified index or within the range index.
//Remove given index element arr1d.erase(arr1.begin()+index-1); //Remove multiple index element ; Vectorvariable.erase(startPos , endPos) arr1d.erase( arr1.begin()+index1-1 , arr1.begin()+index5-1 ); // will erase element from index index1 to index5 //delete 0th index element arr1d.erase(0) //ERROR won't work arr1d.erase(arr1.begin() + 0) OR arr1d.erase(arr1.begin()) vector::clear will remove all elements from vector container making it size 0. (it doesn't deletes the element from memory) vector<int> arrClear{9,8,7,6,5,4,3,2,1}; arrClear.clear(); cout<<arrClear.size(); // prints 0 cout<<arrClear[2]; // prints 7
E.) Remove Element from Vector by given value
Its the Erase-remove idiom
As stated: the Below algorithm does not remove elements from the container but move all elements that don’t fit the removal criteria to the front of the range, keeping the relative order of the elements. This is done in a single pass through the data range.
As no elements are actually removed and the container retains the same size, the tail of the array has a length equal to the number of “removed” items; these items remain in memory but in an unspecified state. remove
returns an iterator pointing to the first of these tail elements so that they can be deleted using a single call to erase
.
//remove all value 8 from given vector #include <algorithm> ... vec.erase(std::remove(vec.begin(), vec.end(), 8), vec.end());
F.) Sort Vector
sort(arr.begin() , arr.end()); //custom sort function sort(arr.begin() , arr.end() , []( const int &a , const int &b) { return a < b; } );