c++ what is a vector code example
Example 1: c++ vector
#include <vector>
int main() {
std::vector<int> v;
v.push_back(10);
v.push_back(20);
v.pop_back();
v.push_back(30);
auto it = v.begin();
int x = *it;
++it;
int y = *it;
++it;
bool is_end = it == v.end();
return 0;
}
Example 2: vector of vectors c++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n = 5;
int m = 7;
vector<vector<int>> vec(n, vector<int>(m, 0));
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < vec[i].size(); j++)
{
cout << vec[i][j] << " ";
}
cout << endl;
}
}