How to copy part of a vector into middle of another in C++

1 Answer

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

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

void print(int n) {
	cout << n << " ";
}

int main()
{
	vector<int> vec1{ 13, 12, 300, 1, 98, 81 }, vec2(6);
	
	copy(vec1.begin() + 2, vec1.end() - 1, vec2.begin() + 2);

	for_each(vec2.begin(), vec2.end(), print);

	cout << endl;

	return 0;
}

/*
run:

0 0 300 1 98 0

*/

 



answered Apr 28, 2018 by avibootz

Related questions

1 answer 206 views
2 answers 251 views
251 views asked Apr 28, 2018 by avibootz
1 answer 172 views
2 answers 207 views
207 views asked Aug 15, 2022 by avibootz
2 answers 252 views
1 answer 188 views
188 views asked Apr 28, 2018 by avibootz
2 answers 198 views
...