Skip to content

Instantly share code, notes, and snippets.

@ashwin
Created October 9, 2014 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashwin/adffb1bd939ba8a74003 to your computer and use it in GitHub Desktop.
Save ashwin/adffb1bd939ba8a74003 to your computer and use it in GitHub Desktop.
Sample code to demonstrate emplace_back operation of vector
// Sample code to demonstrate emplace_back operation of vector
#include <iostream>
#include <vector>
struct Creature
{
Creature() : is_mammal_(false), age_(0), population_(0)
{
std::cout << "Default constructor called\n";
}
Creature(bool is_mammal, int age, int population)
: is_mammal_(is_mammal), age_(age), population_(population)
{
std::cout << "3-arg constructor called\n";
}
bool is_mammal_;
int age_;
int population_;
};
typedef std::vector<Creature> CreatureVec;
int main()
{
CreatureVec cv;
cv.emplace_back(true, 10, 9999);
cv.emplace_back();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment