如何将std :: string转换为小写?

你离开我真会死。 提交于 2019-12-22 22:29:06

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

我想将std::string转换为小写。 我知道函数tolower() ,但是在过去我遇到了这个函数的问题,并且因为使用std::string需要迭代每个字符,所以它几乎不理想。

有没有一种方法可以100%的时间运作?


#1楼

据我所知,Boost库的性能非常糟糕。 我已经测试了他们的unordered_map到STL,平均慢了3倍(最好的情况2,最差的是10次)。 此算法看起来也太低了。

所不同的是如此之大,我相信任何另外,你需要做的tolower使其等于提高“您的需求”会比升压方式更快

我已经在Amazon EC2上完成了这些测试,因此在测试过程中性能会有所不同,但您仍然可以理解。

./test
Elapsed time: 12365milliseconds
Elapsed time: 1640milliseconds
./test
Elapsed time: 26978milliseconds
Elapsed time: 1646milliseconds
./test
Elapsed time: 6957milliseconds
Elapsed time: 1634milliseconds
./test
Elapsed time: 23177milliseconds
Elapsed time: 2421milliseconds
./test
Elapsed time: 17342milliseconds
Elapsed time: 14132milliseconds
./test
Elapsed time: 7355milliseconds
Elapsed time: 1645milliseconds

-O2就是这样的:

./test
Elapsed time: 3769milliseconds
Elapsed time: 565milliseconds
./test
Elapsed time: 3815milliseconds
Elapsed time: 565milliseconds
./test
Elapsed time: 3643milliseconds
Elapsed time: 566milliseconds
./test
Elapsed time: 22018milliseconds
Elapsed time: 566milliseconds
./test
Elapsed time: 3845milliseconds
Elapsed time: 569milliseconds

资源:

string str;
bench.start();
for(long long i=0;i<1000000;i++)
{
    str="DSFZKMdskfdsjfsdfJDASFNSDJFXCKVdnjsafnjsdfjdnjasnJDNASFDJDSFSDNJjdsanjfsdnfjJNFSDJFSD";
    boost::algorithm::to_lower(str);
}
bench.end();

bench.start();
for(long long i=0;i<1000000;i++)
{
    str="DSFZKMdskfdsjfsdfJDASFNSDJFXCKVdnjsafnjsdfjdnjasnJDNASFDJDSFSDNJjdsanjfsdnfjJNFSDJFSD";
    for(unsigned short loop=0;loop < str.size();loop++)
    {
        str[loop]=tolower(str[loop]);
    }
}
bench.end();

我想我应该在专用机器上进行测试但是我将使用这个EC2所以我真的不需要在我的机器上测试它。


#2楼

如果字符串包含ASCII范围之外的UTF-8字符,则boost :: algorithm :: to_lower将不会转换这些字符。 当涉及UTF-8时,最好使用boost :: locale :: to_lower。 请参阅http://www.boost.org/doc/libs/1_51_0/libs/locale/doc/html/conversions.html


#3楼

这是Stefan Mai的回应的后续行动:如果您想将转换结果放在另一个字符串中,则需要在调用std::transform之前预先分配其存储空间。 由于STL将转换后的字符存储在目标迭代器中(在循环的每次迭代中将其递增),因此目标字符串将不会自动调整大小,并且存在内存占用风险。

#include <string>
#include <algorithm>
#include <iostream>

int main (int argc, char* argv[])
{
  std::string sourceString = "Abc";
  std::string destinationString;

  // Allocate the destination space
  destinationString.resize(sourceString.size());

  // Convert the source string to lower case
  // storing the result in destination string
  std::transform(sourceString.begin(),
                 sourceString.end(),
                 destinationString.begin(),
                 ::tolower);

  // Output the result of the conversion
  std::cout << sourceString
            << " -> "
            << destinationString
            << std::endl;
}

#4楼

//You can really just write one on the fly whenever you need one.
#include <string>
void _lower_case(std::string& s){
for(unsigned short l = s.size();l;s[--l]|=(1<<5));
}
//Here is an example.
//http://ideone.com/mw2eDK

#5楼

Boost的另一种选择是POCO(pocoproject.org)。

POCO提供两种变体:

  1. 第一个变体制作副本而不更改原始字符串。
  2. 第二个变体将原始字符串更改为适当位置。
    “就地”版本的名称中始终包含“InPlace”。

两个版本如下所示:

#include "Poco/String.h"
using namespace Poco;

std::string hello("Stack Overflow!");

// Copies "STACK OVERFLOW!" into 'newString' without altering 'hello.'
std::string newString(toUpper(hello));

// Changes newString in-place to read "stack overflow!"
toLowerInPlace(newString);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!