相关疑难解决方法(0)

C++ std :: transform()和toupper()..为什么这会失败?

我有2个std :: string.我只是想,给定输入字符串:

  1. 每封信都要大写
  2. 将大写字母分配给输出字符串.

为什么这样有效:

  std::string s="hello";
  std::string out;
  std::transform(s.begin(), s.end(), std::back_inserter(out), std::toupper);
Run Code Online (Sandbox Code Playgroud)

但这不会(导致程序崩溃)?

  std::string s="hello";
  std::string out;
  std::transform(s.begin(), s.end(), out.begin(), std::toupper);
Run Code Online (Sandbox Code Playgroud)

因为这有效(至少在相同的字符串上:

  std::string s="hello";
  std::string out;
  std::transform(s.begin(), s.end(), s.begin(), std::toupper);
Run Code Online (Sandbox Code Playgroud)

c++ string stl transform

29
推荐指数
1
解决办法
3万
查看次数

标签 统计

c++ ×1

stl ×1

string ×1

transform ×1