Thanks for using Compiler Explorer
Jakt
C++
Ada
Analysis
Assembly
C
Carbon
C++ (Circle)
CIRCT
Clean
CMake
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Erlang
Fortran
F#
Go
Haskell
HLSL
Hook
ispc
Java
Julia
Kotlin
LLVM IR
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Scala
Solidity
Swift
Toit
TypeScript Native
Visual Basic
Zig
c++ source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Compiler
FreeBSD clang 13.0.0
p0849 (modified clang 12)
Options
Source code
#include <string> #include <algorithm> #include <vector> #include <iostream> #include <iomanip> template<class R, class T, class U> void replace_prefixes(R& v, T const& prefix, U const& replacement) { for (auto&& element : v) { if (element.starts_with(prefix)) element.replace(0, prefix.size(), replacement); } } using namespace std::string_literals; template<class R, class S> auto print_list(S& out, R const& v) -> S& { auto n = std::size(v); decltype(n) i = 0; out << "{ "; switch (n) { case 0: for (; i < n; ++i) { out << ", "; default: out << std::quoted(v[i]); } } out << " }"; return out; } int main() { std::vector ls1 = { "/home/zy"s, "/home/zy/devel"s, "/home/zy/temp"s, }; auto ls2 = ls1; replace_prefixes(ls1, ls1[0], "."); print_list(std::cout, ls1) << '\n'; replace_prefixes(ls2, auto(ls2[0]), "."); print_list(std::cout, ls2) << '\n'; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
About the author
Changelog
Version tree