Tech Off Post

Single Post Permalink

View Thread: (STL) move_if algorithm, what's your take on it ?
  • User profile image
    STL

    You should use make_move_iterator() with copy_if() instead:

    C:\Temp>type meow.cpp
    #include <algorithm>
    #include <iostream>
    #include <iterator>
    #include <ostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main() {
        vector<string> dst;
    
        {
            vector<string> src;
    
            src.push_back("The Motion Picture");
            src.push_back("The Wrath Of Khan");
            src.push_back("The Search For Spock");
            src.push_back("The Voyage Home");
            src.push_back("The Final Frontier");
            src.push_back("The Undiscovered Country");
            src.push_back("Generations");
            src.push_back("First Contact");
            src.push_back("Insurrection");
            src.push_back("Nemesis");
            src.push_back("Star Trek");
    
            copy_if(
                make_move_iterator(src.begin()),
                make_move_iterator(src.end()),
                back_inserter(dst),
                [](const string& s) {
                    return s != "The Final Frontier";
                }
            );
        }
    
        for (auto i = dst.begin(); i != dst.end(); ++i) {
            cout << *i << endl;
        }
    }
    
    C:\Temp>cl /EHsc /nologo /W4 /MTd meow.cpp
    meow.cpp
    
    C:\Temp>meow
    The Motion Picture
    The Wrath Of Khan
    The Search For Spock
    The Voyage Home
    The Undiscovered Country
    Generations
    First Contact
    Insurrection
    Nemesis
    Star Trek