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.

markt1964
  • 2,638
  • 2
  • 22
  • 54
  • "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. – Jagannath Nov 06 '14 at 03:03
  • do you mean that somehow the string is turned uppercase at compilation time, while being lowercase in the source? – didierc Nov 06 '14 at 06:01
  • 3
    `std::string udata; std::transform(data.begin(), data.end(), std::back_inserter(udata), ::toupper);`. Nothing says the transformation must happen in place. – Igor Tandetnik Nov 06 '14 at 06: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. – markt1964 Nov 06 '14 at 21:48

1 Answers1

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

markt1964
  • 2,638
  • 2
  • 22
  • 54