Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ vector

// REMEMBER TO REPLACE '???' with the storage type

// needed
#include <vector>
// optional
#include <bits/stdc++.h>
#include <algorithm>

// ===== BASIC USAGE

// constructor
std::vector<???> v;

// access
v[4]; // : ???
v[4] = <???>;

// ===== CHECKS

// size
v.size() // : size_type (int?)
// check size zero
v.empty() // : bool

// ===== INSERT

// 'push' (add an element at the end)
v.push_back(<???>);
// 'pop' (delete last item)
v.pop() // : void
// insert at a given position
v.insert(v.begin() + 3, <???>);
// CONSTRUCT AND INSERT
v.emplace(v.begin() + 3, <???>);

// ===== QUERY

// find something in the array
// https://m.cplusplus.com/reference/algorithm/find/
auto it = std::find(v.begin(), v.end(), <???>);
// returns v.end() if the element has not been found

// ===== DELETE

// delete everything from the vector
v.clear();
// delete given the cell number (from zero)
v.erase(v.begin( ) + 5);

// ===== SORT

// array sort
std::sort(v.begin(), v.end());

// ==== OTHERS

// iterate
for(auto x : v) {}
Source by en.cppreference.com #
 
PREVIOUS NEXT
Tagged: #vector
ADD COMMENT
Topic
Name
7+8 =