How to initialize vector with part of array in C++

1 Answer

0 votes
#include <iostream>
#include <vector>

using std::cout;
using std::endl;

template <typename T>
inline void print(const T& obj)
{
	for (auto element : obj) {
		cout << element << ' ';
	}
	cout << endl;
}

int main()
{
	int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	std::vector<int> vec(arr, arr + 5);
	
	print(vec);

	return 0;
}

/*
run:

0 1 2 3 4

*/

 



answered Feb 23, 2018 by avibootz
edited Feb 23, 2018 by avibootz

Related questions

1 answer 127 views
1 answer 127 views
2 answers 195 views
1 answer 93 views
1 answer 176 views
1 answer 215 views
1 answer 192 views
...