how to print a vector c++ code example

Example 1: cpp print vector

for(int i = 0; i < vec.size(); i++)
    std::cout << vec[i] << ' ';

Example 2: c++ print elements of vector to the console

#include <iostream>
#include <vector>

int main()
{
	std::vector<int> myVector = {1, 2, 3, 4, 5, 6};

	for(int i = 0; i < myVector.size(); i++)
	{
		std::cout << myVector[i] << std::endl;
        
        //***** alternate method *******
		//std::cout << myVector.at(i) << std::endl;		
	}	
}

Example 3: c how to printf a vector

int i;
const int N = 10;
int vett[N];

for(i = 0; i < N; i ++)
	printf("\n%d",vett[i]);

Tags:

Misc Example