Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Latest commit

 

History

History
35 lines (29 loc) · 1.2 KB

replace_copy_if.md

File metadata and controls

35 lines (29 loc) · 1.2 KB

replace_copy_if

Description : Copies elements from the range [first, last), to another range beginning at d_first, replacing the elements which satisfy specific criteria by a new_value.

Example:

    auto isOdd = [](int i) {
        return ((i%2) == 1);
    };

    std::vector<int> origin {1, 2, 3, 4, 5};
    std::vector<int> destination;

    // Copy elements to destination replacing elements that return true for isOdd by 0
    std::replace_copy_if(origin.begin(),                  //first
                         origin.end(),                    //last
                         std::back_inserter(destination), //d_first 
                         isOdd,                           //condition
                         0                                //new_value
                         );
    
    // origin is still {1, 2, 3, 4, 5}
    for (auto value : origin) { 
        std::cout << value << " "; 
    }
    std::cout << std::endl;

    // destination is {0, 2, 0, 4, 0}
    for (auto value : destination) { 
        std::cout << value << " "; 
    }
    std::cout << std::endl;

See Sample code Run Code