Skip to content

Latest commit

 

History

History
25 lines (20 loc) · 567 Bytes

transform.md

File metadata and controls

25 lines (20 loc) · 567 Bytes
title description author tags
Transform
Transform a string with a function
majvax
string,transform,c++23
#include <ranges>
#include <string>

template <typename F>
std::string transform(const std::string& str, F&& transformer) {
    return str
        | std::ranges::views::transform(std::forward<F>(transformer))
        | std::ranges::to<std::string>();
}



// Usage:
std::string str = "Hello, World!";
std::string transformed = transform(str, [](char c){ return std::toupper(c); });
std::cout << transformed << std::endl; // HELLO, WORLD!