1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
/* Vector examples
Written by Bernhard Firner
*/
#include <iostream>
#include <vector>
using std::vector;
using std::cout;
int main(int argc, char** argv) {
vector<int> a = { 1, 2, 3, 4 };
vector<int> b = { 1, 2, 3, 4 };
vector<int> c = { 1, 2, 3, 4 };
if (a == b) {
cout << "a is equal to b!\n";
}
if (a == c) {
cout << "a is equal to c!\n";
}
//Copy an existing vector (copy constructor)
//The first argument sets the size of the new vector
//The second argument sets the default value of each item
vector<int> d(10, 5);
//Insert all of a into d
d.insert(d.end(), a.begin(), a.end());
d.erase(d.begin(), d.begin() + 5);
d.push_back(25);
if (d == a) {
cout << "d and a are equal!\n";
}
for (int i = 0; i < d.size(); ++i) {
cout << d[i] << '\n';
}
//A two-dimensional vector with 5 rows, 2 columns, all values are 42
vector<vector<int>> twod(5, vector<int>(2, 42));
/*
for (int i = 0; i < twod.size(); ++i) {
for (int j = 0; j < twod.at(i).size(); ++j) {
cout << twod.at(i).at(j) << ' ';
}
cout << '\n';
}
*/ }
|