Skip to content

Instantly share code, notes, and snippets.

@mitsu-ksgr
Created September 30, 2014 13:42
Show Gist options
  • Save mitsu-ksgr/c0468aa21167f86158ff to your computer and use it in GitHub Desktop.
Save mitsu-ksgr/c0468aa21167f86158ff to your computer and use it in GitHub Desktop.
【C++】std::unique_ptrをvectorで扱うメモ
#include <iostream>
#include <memory>
#include <vector>
struct Test {
Test(int data = 0) : data(data) {
std::cout << "Test::Constructor: " << this->data << std::endl;
}
~Test() {
std::cout << "Test::Destructor: " << this->data << std::endl;
}
int data;
};
int main()
{
std::vector<std::unique_ptr<Test>> vec;
for(int i = 0; i < 5; ++i) {
vec.push_back(
std::unique_ptr<Test>(new Test(i)));
}
for(auto &elem : vec) {
std::cout << elem->data << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment