要转换字符串并使其为小写,我们可能会执行以下操作:
#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string str("Sample STRING");
cout << str << endl;
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
cout << str << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道这std::transform是怎么回事; 但是范围操作员::在功能面前做了tolower什么?
如果我删除范围运算符,则编译器会抱怨函数不匹配.如果我std在::运算符前面添加,那么编译器也会抱怨函数不匹配.范围运营商面前的目的是什么tolower?我不知道它叫什么,我到处寻找解释,但无济于事.
#include <algorithm>使用std::transform.tolower您想要的功能在ctype.h或中定义cctype.您应该包含其中一个标题.前者tolower在全局命名空间中声明; 后者在std命名空间中声明它.::,你可能会拿起标题中std::tolower声明的函数模板<locale>.当然,这只会因为你的而发生using namespace std;.这是一个如何using namespace std;危险的特殊例子.::用什么左边是指名称权将是"在全球范围内抬头",并会发现全球tolower,而不是std::tolower.(因此,您应该#include <ctype.h>确保获得全局声明.)| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |