C++ 如何在c++; #包括 #包括 #包括 使用名称空间std; int main(){ 字符串str(“你好,世界!”); 用于(自动&c:str) c=toupper(c); cout

C++ 如何在c++; #包括 #包括 #包括 使用名称空间std; int main(){ 字符串str(“你好,世界!”); 用于(自动&c:str) c=toupper(c); cout,c++,C++,在C++11x之前,在算法头中定义了每个的。 只需使用: #include <cstdlib> #include <iostream> #include <string> using namespace std; int main() { string str("hello world!"); for (auto &c : str) c = toupper(c); cout << str;

在C++11x之前,在
算法
头中定义了每个的
。
只需使用:

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main() {
    string str("hello world!");
    for (auto &c : str)
        c = toupper(c);
    cout << str;
    return 0;
}
其中,
fn
是元素将被传递到的函数,前两个参数是输入迭代器

另外,在包含
字符串
算法
之后,您可以使用


std::transform(str.begin(),str.end(),str.begin(),::toupper);

如图所示,该代码是有效的


请参阅编译器文档以确保启用了C++11。此选项通常称为
-std=C++11
。您可能需要下载升级;请检查您的软件包管理器以了解GCC(当前为4.8)或Clang(当前为3.3).

它存在于C++11中。请确保使用能够处理C++11的编译器,并确保为此启用所需的选项。
std::for_每个
仍在
算法
头中定义,即使在C++11中也是如此。(C++11在语言核心中引入的基于范围的for循环并没有取代
std::for_each
算法,即使用例中有一些重叠。)
for_each (vec.begin(), vec.end(), fn);