Search code examples
c++11toupper

converting std::string to upper case without modifying source


I'm acquainted with the usage of std::transform(data.begin(), data.end(), data.begin(), ::toupper), which can change the string in data to all uppercase. I am wondering, however, if there is a clean solution that can get the all-uppercase version of a string without modifying the source? The workaround of making a copy of the source and then calling std::transform on the copy, and then returning the copy seems a bit like a kludge, and I'm wondering if there's a more efficient and elegant solution.

I am looking for a pure C++11 solution... without dependency on any even widely available C++ libraries such as boost.


Solution

  • Per Igor's comment above, the solution is to use an std::back_inserter on the destination.... std::transform(src.begin(), src.end(), std::back_inserter(dest), ::toupper);