A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/algorithm/../symbol_index/../algorithm/ranges/replace_copy.html below:

std::ranges::replace_copy, std::ranges::replace_copy_if, std::ranges::replace_copy_result, std::ranges::replace_copy_if_result - cppreference.com

#include <algorithm>
#include <array>
#include <complex>
#include <iostream>
#include <vector>
 
void println(const auto rem, const auto& v)
{
    for (std::cout << rem << ": "; const auto& e : v)
        std::cout << e << ' ';
    std::cout << '\n';
}
 
int main()
{    
    std::vector<int> o;
 
    std::array p{1, 6, 1, 6, 1, 6};
    o.resize(p.size());
    println("p", p);
    std::ranges::replace_copy(p, o.begin(), 6, 9);
    println("o", o);
 
    std::array q{1, 2, 3, 6, 7, 8, 4, 5};
    o.resize(q.size());
    println("q", q);
    std::ranges::replace_copy_if(q, o.begin(), [](int x) { return 5 < x; }, 5);
    println("o", o);
 
    std::vector<std::complex<short>> r{{1, 3}, {2, 2}, {4, 8}};
    std::vector<std::complex<float>> s(r.size());
    println("r", r);
    #ifdef __cpp_lib_algorithm_default_value_type
        std::ranges::replace_copy(r, s.begin(),
                                  {1, 3}, // T1 gets deduced
                                  {2.2, 4.8}); // T2 gets deduced
    #else
        std::ranges::replace_copy(r, s.begin(),
                                  std::complex<short>{1, 3},
                                  std::complex<float>{2.2, 4.8});
    #endif
    println("s", s);
 
    std::vector<std::complex<double>> b{{1, 3}, {2, 2}, {4, 8}},
                                      d(b.size());
    println("b", b);
    #ifdef __cpp_lib_algorithm_default_value_type
        std::ranges::replace_copy_if(b, d.begin(),
            [](std::complex<double> z){ return std::abs(z) < 5; },
            {4, 2}); // Possible, since the T is deduced.
    #else
        std::ranges::replace_copy_if(b, d.begin(),
            [](std::complex<double> z){ return std::abs(z) < 5; },
            std::complex<double>{4, 2});
    #endif
    println("d", d);
}
p: 1 6 1 6 1 6
o: 1 9 1 9 1 9
q: 1 2 3 6 7 8 4 5
o: 1 2 3 5 5 5 4 5
r: (1,3) (2,2) (4,8)
s: (2.2,4.8) (2,2) (4,8)
b: (1,3) (2,2) (4,8)
d: (4,2) (4,2) (4,8)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4