C++ 字符串的 tolower 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3403844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
tolower function for C++ strings
提问by brett
Is there an inbuilt function to convert C++ string from upper case letters to lowercase letters ? If not converting it to cstring and using tolower on each char is the only option ?
是否有内置函数将 C++ 字符串从大写字母转换为小写字母?如果不将其转换为 cstring 并在每个字符上使用 tolower 是唯一的选择?
Thank you very much in advance.
非常感谢您提前。
回答by ereOn
If boost
is an option:
如果boost
是一个选项:
#include <boost/algorithm/string.hpp>
std::string str = "wHatEver";
boost::to_lower(str);
Otherwise, you may use std::transform
:
否则,您可以使用std::transform
:
std::string str = "wHatEver";
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
You can also use another function if you have some custom locale-aware tolower
.
如果您有一些自定义语言环境感知,您也可以使用另一个功能tolower
。
回答by TortoiseTNT
std::transform(myString.begin(), myString.end(), myString.begin(), std::tolower);
回答by graham.reeds
Like ereOn says:
std::transform(str.begin(), str.end(), str.begin(), std::tolower );
就像 ereOn 说:
std::transform(str.begin(), str.end(), str.begin(), std::tolower );
Or via for_each:
std::for_each(str.begin(), str.end(), std::tolower );
或者通过 for_each:
std::for_each(str.begin(), str.end(), std::tolower );
Transform is probably better of the two.
转换可能是两者中更好的。
回答by graham.reeds
There is no built-in function to do this, and doing it is surprisingly complicated, because of locales et al. If tolower
does what you need, it may be your best bet.
没有内置函数可以执行此操作,并且由于 locales 等人的原因,执行此操作非常复杂。如果tolower
可以满足您的需求,这可能是您最好的选择。
回答by Nanne
For this problem you can use the STL's transform method to solve it:
对于这个问题可以使用STL的transform方法来解决:
std::string str = "simple";
std::transform(str.begin(), str.end(), str.begin(), std::tolower);
回答by Anand Rathi
I have an implementation I found it faster than std::transform , Compiled in g++ -03 Fedora 18. my example converts std::string
我有一个实现,我发现它比 std::transform 更快,在 g++ -03 Fedora 18 中编译。我的示例转换 std::string
performance time in seconds : transform took : 11 s my implementation took : 2 s Test data size = 26*15*9999999 chars
inline void tolowerPtr(char *p) ;
inline void tolowerStr(std::string& s)
{char* c=const_cast<char*>(s.c_str());
size_t l = s.size();
for(char* c2=c;c2<c+l;c2++)tolowerPtr(c2);
};
inline void tolowerPtr(char *p)
{
switch(*p)
{
case 'A':*p='a'; return;
case 'B':*p='b'; return;
case 'C':*p='c'; return;
case 'D':*p='d'; return;
case 'E':*p='e'; return;
case 'F':*p='f'; return;
case 'G':*p='g'; return;
case 'H':*p='h'; return;
case 'I':*p='i'; return;
case 'J':*p='j'; return;
case 'K':*p='k'; return;
case 'L':*p='l'; return;
case 'M':*p='m'; return;
case 'N':*p='n'; return;
case 'O':*p='o'; return;
case 'P':*p='p'; return;
case 'Q':*p='q'; return;
case 'R':*p='r'; return;
case 'S':*p='s'; return;
case 'T':*p='t'; return;
case 'U':*p='u'; return;
case 'V':*p='v'; return;
case 'W':*p='w'; return;
case 'X':*p='x'; return;
case 'Y':*p='y'; return;
case 'Z':*p='z'; return;
};
return ;
}
void testtransform( std::string& word )
{
std::string word2=word;
time_t t;
time_t t2;
time(&t);
std::cout << "testtransform: start " << "\n";
int i=0;
for(;i<9999999;i++)
{ word2=word;
std::transform(word2.begin(), word2.end(), word2.begin(), ::tolower);
}
time(&t2);
std::cout << word2 << "\n";
std::cout << "testtransform: end " << i << ":"<< t2-t << "\n";
}
void testmytolower( std::string& word )
{
std::string word2=word;
time_t t;
time_t t2;
time(&t);
std::cout << "testmytolower: start " << "\n";
int i=0;
for(;i<9999999;i++)
{ word2=word;
cstralgo::tolowerStr(word2);
}
time(&t2);
std::cout << word2 << "\n";
std::cout << "testmytolower: end " << i << ":"<< t2-t << "\n";
}
int main(int argc, char* argv[])
{
std::string word ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
word =word+word+word+word+word+word+word+word+word+word+word+word+word+word+word;
testtransform( word);
testmytolower( word);
return 0;
}
I will be glad to know if performance can be improved further.
我很高兴知道性能是否可以进一步提高。