c++ std::vector, .emplace_back

Method .emplace_back, appends element at the end, by constructing.

c++ exmple

#include <vector>
#include <iostream>

namespace dir
{
	class pet_president
	{
	private:
		const int __id;
		const int __name;
	public:
		pet_president(const int id__, const int name__):
			__id{id__},
			__name{name__}
		{
		}
	public:
		void print() const
		{
			std::cout << __id  << ", " << __name << std::endl;
		}
	};
}

int main()
{
	std::vector<dir::pet_president> presidents;
	for (int i=-5; i<=5; ++i)
	{
		presidents.emplace_back(i, i+10000);
	}
	for (dir::pet_president & president: presidents)
	{
		president.print();
	}
}
-5, 9995
-4, 9996
-3, 9997
-2, 9998
-1, 9999
0, 10000
1, 10001
2, 10002
3, 10003
4, 10004
5, 10005

Index

Home