insert vs emplace

Magnus Fromreide magfr@lysator.liu.se
Sat Mar 14 12:35:00 GMT 2015


Hello.

I have been pondering the attached program for a while and can't figure
out if it invokes undefined behaviour or what the proper output of it
should be so I am hoping someone here could help me finding the relevant
parts of the c++ standard.

/MF
-------------- next part --------------
#include <iostream>
#include <vector>
int main()
{
	{
		std::vector<int> v { 1, 2, 3 };
		v.reserve(4);
		v.insert(v.begin(), v[2]);
		std::cout << v[0] << ' ' << v[1] << ' ' << v[2] << ' ' << v[3] << '\n';
	}
	{
		std::vector<int> v { 1, 2, 3 };
		v.reserve(4);
		v.emplace(v.begin(), v[2]);
		std::cout << v[0] << ' ' << v[1] << ' ' << v[2] << ' ' << v[3] << '\n';
	}
}


More information about the Gcc-help mailing list