display a vector c++ code example
Example 1: 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 2: show a vector in c++
template<typename T>
static void show(T vec)
{
std::cout << vec;
}
template<typename T>
static void show(std::vector<T> vec)
{
int size = vec.size();
if (size <= 0) {
std::cout << "invalid vector";
return;
}
std::cout << '{';
for (int l = 0; l < size - 1; l++) {
show(vec[l]);
std::cout << ',';
}
show(vec[size - 1]);
std::cout << '}';
}