日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++,string,compare,nocase,for copy

發(fā)布時間:2023/12/29 c/c++ 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++,string,compare,nocase,for copy 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

總結(jié):如何在c++中進(jìn)行大小寫忽略的比較,基于std:string?

ref1,ref2,ref3


問題由來:

標(biāo)準(zhǔn)字符 (typedef basic_string<char> string)由于國際化過程中,沒有實(shí)現(xiàn)每一種語言的大小寫轉(zhuǎn)換函數(shù),也沒要提供基于ansi字符的大小寫忽略比較。


解決方案1:先轉(zhuǎn)換大小寫再比較

策略1. std method

C++ — Convert string to upper/lower?case


To convert an?std::string?to upper case you can use the following:

#include <algorithm> #include <string> … std::string data = “Abc”; std::transform(data.begin(), data.end(), data.begin(), ::toupper);

For converting to lower case, you need to replace::toupper?with?::tolower.

If you really hate tolower(), here's an alternative:

char easytolower(char in){if(in<='Z' && in>='A')return in-('Z'-'z');return in; } std::transform(data.begin(), data.end(), data.begin(), easytolower);

策略2. boost method

There is a Boost string algorithm for this:

#include <boost/algorithm/string.hpp> std::string str = "HELLO, WORLD!"; boost::algorithm::to_lower(str);

解決方案2:直接比較

策略1

Boost includes a handy algorithm for this:

#include <boost/algorithm/string.hpp>std::string str1 = "hello, world!"; std::string str2 = "HELLO, WORLD!";if (boost::iequals(str1, str2)) {// Strings are identical } 策略2

Take advantage of the standard?char_traits. Recall that a?std::string?is in fact a typedef for?std::basic_string<char>, or more explicitly,std::basic_string<char, std::char_traits<char> >. The?char_traits?type describes how characters compare, how they copy, how they cast etc. All you need to do is typedef a new string over?basic_string, and provide it with your own customchar_traits?that compare case insensitively.

struct ci_char_traits : public char_traits<char> {static bool eq(char c1, char c2) { return toupper(c1) == toupper(c2); }static bool ne(char c1, char c2) { return toupper(c1) != toupper(c2); }static bool lt(char c1, char c2) { return toupper(c1) < toupper(c2); }static int compare(const char* s1, const char* s2, size_t n) {while( n-- != 0 ) {if( toupper(*s1) < toupper(*s2) ) return -1;if( toupper(*s1) > toupper(*s2) ) return 1;++s1; ++s2;}return 0;}static const char* find(const char* s, int n, char a) {while( n-- > 0 && toupper(*s) != toupper(a) ) {++s;}return s;} };typedef std::basic_string<char, ci_char_traits> ci_string;

策略3

bool caseInsensitiveStringCompare(const string& str1, const string& str2) {if (str1.size() != str2.size()) {return false;}for (string::const_iterator c1 = str1.begin(), c2 = str2.begin(); c1 != str1.end(); ++c1, ++c2) {if (tolower(*c1) != tolower(*c2)) {return false;}}return true; } 策略5

The trouble with boost is that you have to link with and depend on boost. Not easy in some cases (e.g. android).

And using char_traits means?all?your comparisons are case insensitive, which isn't usually what you want.

This should?suffice. It should be reasonably efficient. Doesn't handle unicode or anything though.

bool iequals(const string& a, const string& b) {unsigned int sz = a.size();if (b.size() != sz)return false;for (unsigned int i = 0; i < sz; ++i)if (tolower(a[i]) != tolower(b[i]))return false;return true; } 策略6

ere is a method of doing this, although it does transforming the strings, and is not Unicode friendly, it should be portable which is a plus:

bool caseInsensitiveStringCompare( const std::string& str1, const std::string& str2 ) {std::string str1Cpy( str1 );std::string str2Cpy( str2 );std::transform( str1Cpy.begin(), str1Cpy.end(), str1Cpy.begin(), ::tolower );std::transform( str2Cpy.begin(), str2Cpy.end(), str2Cpy.begin(), ::tolower );return ( str1Cpy == str2Cpy ); }

總結(jié)

以上是生活随笔為你收集整理的c++,string,compare,nocase,for copy的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。