回答
如果boost
是一个选项:
#include <boost/algorithm/string.hpp>
std::string str = "wHatEver";
boost::to_lower(str);
否则,你可以使用std::transform
:
std::string str = "wHatEver";
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
您也可以使用其他的功能,如果你有一些自定义的区域识别tolower
。
没有内置函数可以做到这一点,而且由于语言环境等原因,这样做的确很复杂。如果tolower
做你需要的,这可能是你最好的选择。
std::transform(myString.begin(), myString.end(), myString.begin(), std::tolower);
这个和其他的transform + tolower答案应该考虑到这不一定会编译,这取决于这个文件包含了什么标准头文件。“
请注意,这个答案(和所有其他'变换'答案)可能导致未定义的行为,因为'cstdlib'的'std :: tolower'需要一个非负的参数 – 2015-08-16 15:18:50
像ereOn说: std::transform(str.begin(), str.end(), str.begin(), std::tolower);
或通过的for_each: std::for_each(str.begin(), str.end(), std::tolower);
变换可能是更好的两个。
我敢打赌。 for_each不会改变str。 – 2010-08-04 08:49:11
这是从袖口注销。您可以使用lamda为每个角色调用tolower。 – 2010-08-04 09:03:02
对于这个问题,你可以使用STL的变换方法来解决这个问题:
std::string str = "simple";
std::transform(str.begin(), str.end(), str.begin(), std::tolower);
我已经实现,我发现它比快的std ::变换,编译在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;
}
我会很高兴知道,如果性能可以进一步提高。
- 1. ForeignKey字符串ToLower实体框架
- 2. C字符串函数
- 3. CMake的函数字符串转换为C字符串字面
- 4. 字符串上的c函数
- 5. C#中的左侧字符串函数#
- 6. C中的反向字符串函数
- 7. C++数组字符串函数
- 8. 如何对非ASCII字符使用tolower函数
- 9. 使用malloc替换字符串中子字符串的C函数 - 无字符串函数
- 10. 从C函数返回字符串接受两个字符串
- 11. 字符串和字符串函数用C
- 12. C函数输入字符串
- 13. 用C++ Lua函数返回字符串
- 14. C#长子字符串函数
- 15. C函数之间传递字符串
- 16. 反向字符串函数C++
- 17. 函数getline(isstream,字符串)在C++
- 18. 调试字符串替换函数C
- 19. C++空字符串构造函数
- 20. 替换字符串函数C++
- 21. Unicode字符串visual C++构造函数
- 22. C++ Dll字符串函数崩溃
- 23. 字符串子字符串函数
- 24. 字符串函数
- 25. 字符串函数
- 26. 字符串函数
- 27. C函数大写指针字符串的第一个字符
- 28. C#4.0函数检查字符串中的前四个字符
- 29. C#字符串函数来得到字符两个符号
- 30. 从一个函数到另一个函数的C#字符串
在德国,tolower(“STRASSE”)应该导致“straße”。在瑞士,它应该是“strasse”。世界各地还有更多类似的案例。内置功能必须正确处理这些情况。如果你不在乎,只需在每个人物上使用tolower(),就像大多数答案显示的那样。 – Sjoerd 2010-08-04 08:56:13
@Sjoerd:很好的例子。你有没有听说过任何优雅地处理这些案件的图书馆?我可能对它感兴趣。 – ereOn 2010-08-04 09:07:06
@ereOn:不,我从来不需要一个。我知道tolower()存在问题,但我住的地方和我写的应用程序中,tolower()通常“足够好”。 – Sjoerd 2010-08-04 09:23:09