vectors in cpp code example
Example 1: declare vectors c++
vector<int> vec;
vector<int> vec(4);
vector<int> vec(4, 42);
vector<int> vec(4, 42);
vector<int> vec2(vec);
Example 2: vector in c++
vector<int> g1;
for (int i = 1; i <= 5; i++)
g1.push_back(i);
cout << "Output of begin and end: ";
for (auto i = g1.begin(); i != g1.end(); ++i)
cout << *i << " ";
cout << "\nOutput of cbegin and cend: ";
for (auto i = g1.cbegin(); i != g1.cend(); ++i)
cout << *i << " ";
cout << "\nOutput of rbegin and rend: ";
for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir)
cout << *ir << " ";
Example 3: how to use vectors c++
#include <iostream>
#include <vector>
using namespace std;
int main() {
const int size = 4;
vector<int> myVect (size, 4);
for (int i=0; i<size; i++) {
cout << "Vector index(" << i <<") is: "<< myVect[i] << endl;
}
return 0;
}
Example 4: vectors in c++
vector <int> vc;
Example 5: defining vectors in c#
var v1 = Vector.Create<double>(5);