0

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.

4
  • "making a copy of the source and then calling std::transform on the copy, and then returning the copy " Is as efficient as you would get if you're not changing the source. Commented Nov 6, 2014 at 3:03
  • do you mean that somehow the string is turned uppercase at compilation time, while being lowercase in the source? Commented Nov 6, 2014 at 6:01
  • 3
    std::string udata; std::transform(data.begin(), data.end(), std::back_inserter(udata), ::toupper);. Nothing says the transformation must happen in place. Commented Nov 6, 2014 at 6:04
  • If you post that comment as an answer, I will flag it as the correct answer, and the question can be marked as answered. I never knew about std::back_inserter. Most cool. Thanks. Commented Nov 6, 2014 at 21:48

1 Answer 1

2

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);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.