Skip to content

Instantly share code, notes, and snippets.

@pebble8888
Created January 2, 2023 06:14
Show Gist options
  • Save pebble8888/9aab0f870299397bea1b8d3ae25c7d9b to your computer and use it in GitHub Desktop.
Save pebble8888/9aab0f870299397bea1b8d3ae25c7d9b to your computer and use it in GitHub Desktop.
make move iterator
#include <iostream>
#include <vector>
struct Point {
int x;
Point(int x)
: x(x)
{
}
};
int main(int argc, const char * argv[]) {
std::vector<std::unique_ptr<Point>> v;
for (int i = 0; i < 5; ++i) {
v.push_back(std::make_unique<Point>(i));
}
auto it = v.begin() + 2;
it = v.erase(it);
std::vector<std::unique_ptr<Point>> v2;
v2.assign(std::make_move_iterator(v.begin()),
std::make_move_iterator(v.end()));
for (const auto& pt: v) {
if (pt) {
printf("%d\n", pt->x);
} else {
printf("empty\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment