如何將std :: string轉換爲小寫?

我想將std::string轉換爲小寫。 我知道函數tolower() ,可是在過去我遇到了這個函數的問題,而且由於使用std::string須要迭代每一個字符,因此它幾乎不理想。 html

有沒有一種方法能夠100%的時間運做? ios


#1樓

據我所知,Boost庫的性能很是糟糕。 我已經測試了他們的unordered_map到STL,平均慢了3倍(最好的狀況2,最差的是10次)。 此算法看起來也過低了。 算法

所不一樣的是如此之大,我相信任何另外,你須要作的tolower使其等於提升「您的需求」會比升壓方式更快ide

我已經在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就是這樣的: oop

./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 spa


#3樓

這是Stefan Mai的迴應的後續行動:若是您想將轉換結果放在另外一個字符串中,則須要在調用std::transform以前預先分配其存儲空間。 因爲STL將轉換後的字符存儲在目標迭代器中(在循環的每次迭代中將其遞增),所以目標字符串將不會自動調整大小,而且存在內存佔用風險。 code

#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);
相關文章
相關標籤/搜索