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.

Revisions

  1. pebble8888 created this gist Jan 2, 2023.
    37 changes: 37 additions & 0 deletions make_move_iterator.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #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;
    }