c++ - 我如何将 C++ 中数组中名称的每个字母大写?

标签 c++

当我尝试编译我的程序时,提示“无法将参数 ‘1’ 的 ‘std::string’ 转换为 ‘int’ 到 ‘int toupper(int)’”

#include <iostream>
#include <cstring>
using namespace std;

int main(){
  string names[10];

  for (int i=0;i<=9;i++){
    cout<<"Please enter name for student "<<i+1<<": ";
    cin>>names[i];
    }

for(int j=0;j<=9;j++){
names[j]=toupper(names[j]);
cout<<names[j]<<endl;
}


return 0;
}

最佳答案

您必须将 char 传递给 std::toupper 而不是整个 string:

for (auto &x : names)
  std::transform(x.begin(), x.end(), x.begin(), ::toupper);

关于c++ - 我如何将 C++ 中数组中名称的每个字母大写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23596653/

相关文章:

java - 在 OpenCv4Android 中存储测试数据

c++ - 给定数组时 cout 如何工作?

C++ 包装器 DLL 到静态 LIB

c++ - OpenCV:运算符 "+"错误;添加

c++ - async_accept 处理程序的用法

c++ - Pthreads,与 pthread_join(pthread_t, void**) 混淆

c++ - 类无法访问其自己的私有(private)静态 constexpr 方法 - Clang 错误?

c++ - RcppParallel没有匹配的函数来调用 'transform'

c++ - 更好的 boost 引用?

c++ - 临时生命周期延长在现代 C++ 中何时有用?