vector + vector cpp code example

Example 1: c++ vector

#include <vector>

int main() {
  std::vector<int> v;
  v.push_back(10); // v = [10];
  v.push_back(20); // v = [10, 20];
  
  v.pop_back(); // v = [10];
  v.push_back(30); // v = [10, 30];
  
  auto it = v.begin();
  int x = *it; // x = 10;
  ++it;
  int y = *it; // y = 30
  ++it;
  bool is_end = it == v.end(); // is_end = true
  
  return 0;
}

Example 2: cpp vector structure

#include <vector>

typedef struct test1 {
  int a;
  char b;
} TOTO;

std::vector<TOTO> _v;

_v.push_back((TOTO){10, 'a'});
_v[0].a = 101;

Tags:

Cpp Example