青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

清風竹林

ぷ雪飄絳梅映殘紅
   ぷ花舞霜飛映蒼松
     ----- Do more,suffer less

Utilities for STL std::string

Lots of programmers have been familiar with some routines for string object, such as length, substring, find, charAt, toLowerCase, toUpperCase, trim, equalsIgnoreCase, startsWith, endsWith, parseInt, toString, split, and so on.

Now, if you are using STL and its string class std::string, how to do something which above routines do?

Of course, std::string supplies some methods to implements some routines above. They are,

length(), get the length of the string.
substr(), get a substring of the string.
at()/operator [], get the char at specified location in the string.
find/rfind(), search a string in a forward/backward direction for a substring.
find_first_of(), find the first character that is any of specified characters.
find_first_not_of(), find the first character that is not any of specified characters.
find_last_of(), find the last character that is any of specified characters.
find_last_not_of(), find the last character that is not any of specified characters.

Please refer document for more std::string's methods

Some routines are not implemented as std::string's methods, but we can find way in algorithm.h to do that. Of course, the existed methods of std::string are also used to implement them.

Transform a string to upper/lower case

Collapse
std::transform(str.begin(), str.end(), str.begin(), tolower);
std::transform(str.begin(), str.end(), str.begin(), toupper);

Please refer document for detail of std::transform function

Trim spaces beside a string

Trim left spaces

Collapse
string::iterator i;
for (i = str.begin(); i != str.end(); i++) {
if (!isspace(*i)) {
break;
}
}
if (i == str.end()) {
str.clear();
} else {
str.erase(str.begin(), i);
}

Trim right spaces

Collapse
string::iterator i;
for (i = str.end() - 1; ;i--) {
if (!isspace(*i)) {
str.erase(i + 1, str.end());
break;
}
if (i == str.begin()) {
str.clear();
break;
}
}

Trim two-sided spaces

Trim left spaces then trim right spaces. Thus two-sided spaces are trimed.

Create string by repeating character or substring

If you want create a string by repeating substring, you must use loop to implement it.

Collapse
string repeat(const string& str, int n) {
string s;
for (int i = 0; i < n; i++) {
s += str;
}
return s;
}

But if you need just to repeat character, std::string has a constructor.

Collapse
string repeat(char c, int n) {
return string(n, c);
}

Compare ignore case

It's funny. We should copy the two strings which attend compare. Then transform all of them to lower case. At last, just compare the two lower case strings.

StartsWith and EndsWith

StartsWith

Collapse
str.find(substr) == 0;

If result is true, the str starts with substr.

EndsWith

Collapse
size_t i = str.rfind(substr);
return (i != string::npos) && (i == (str.length() - substr.length()));

If result is true, the str ends with substr

There is another way to do that. Just get left substring or right substring to compare. Because I don't want to calculate if string's length is enough, so I use find and rfind to do that.

Parse number/bool from a string

For these routines, atoi, atol and some other C functions are OK. But I want use C++ way to do. So I choose std::istringstream. the class is in sstream.h.

A template function can do most excludes bool value.

Collapse
template<class T> parseString(const std::string& str) {
T value;
std::istringstream iss(str);
iss >> value;
return value;
}

The template function can parse 0 as false and other number as true. But it cannot parse "false" as false and "true" as true. So I write a special function.

Collapse
template<bool>
bool parseString(const std::string& str) {
bool value;
std::istringstream iss(str);
iss >> boolalpha >> value;
return value;
}

As you saw, I pass a std::boolalpha flag to the input stream, then the input stream can recognize literal bool value.

It is possible to use a similar way to parse hex string. This time I should pass a std::hex flag to the stream.

Collapse
template<class T> parseHexString(const std::string& str) {
T value;
std::istringstream iss(str);
iss >> hex >> value;
return value;
}

To string routines

Like parsing from string, I will use std::ostringstream to get string from other kinds of value. The class is also in sstream.h. The relative 3 functions are followed.

Collapse
template<class T> std::string toString(const T& value) {
std::ostringstream oss;
oss << value;
return oss.str();
}
string toString(const bool& value) {
ostringstream oss;
oss << boolalpha << value;
return oss.str();
}
template<class T> std::string toHexString(const T& value, int width) {
std::ostringstream oss;
oss << hex;
if (width > 0) {
oss << setw(width) << setfill('0');
}
oss << value;
return oss.str();
}

Do you take note of setw and setfill? They are still flags which need an argument. std::setw allow the output thing in the stream occupy fixed width. If itself length is not enough, default uses space to fill. std::setfill is used to change the spaceholder. If you want control the alignment, there are std::left and std::right flags.

Oh, I forgot to tell you, setw and setfill need iomanip.h header file.

Split and tokenizer

I think split function should be implemented with a tokenizer. So I write a tokenizer at first. We can use find_first_of and find_first_not_of methods to get each token. Follows is nextToken method of Tokenizer class.

Collapse
bool Tokenizer::nextToken(const std::string& delimiters) {
// find the start character of the next token.
size_t i = m_String.find_first_not_of(delimiters, m_Offset);
if (i == string::npos) {
m_Offset = m_String.length();
return false;
}

// find the end of the token.
size_t j = m_String.find_first_of(delimiters, i);
if (j == string::npos) {
m_Token = m_String.substr(i);
m_Offset = m_String.length();
return true;
}

// to intercept the token and save current position
m_Token = m_String.substr(i, j - i);
m_Offset = j;
return true;
}

The whole Tokenizer is in the source code archive. You can download it at above. All other functions are still in the source code files.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

posted on 2009-05-15 15:45 李現(xiàn)民 閱讀(2035) 評論(1)  編輯 收藏 引用 所屬分類: 絕對盜版

評論

# re: Utilities for STL std::string 2009-08-28 09:49 xuxiangrong

學習!!!  回復  更多評論   

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            一区二区三区欧美| 国产亚洲女人久久久久毛片| 亚洲精品乱码久久久久久按摩观 | 韩国三级电影久久久久久| 久久久精品tv| 免费中文日韩| 亚洲网站在线| 欧美在线不卡视频| 亚洲伦伦在线| 亚洲欧美日韩高清| 影音先锋欧美精品| 亚洲精品久久久久久一区二区| 欧美激情1区2区| 亚洲男人影院| 狂野欧美性猛交xxxx巴西| 99视频超级精品| 先锋a资源在线看亚洲| 亚洲黄色av一区| 亚洲午夜精品福利| 亚洲国产一成人久久精品| 在线亚洲国产精品网站| 精品91在线| 99av国产精品欲麻豆| 好看的日韩视频| 99精品国产一区二区青青牛奶| 国产欧美一区二区色老头| 欧美成人激情视频免费观看| 国产精品扒开腿爽爽爽视频| 欧美3dxxxxhd| 国产婷婷色一区二区三区四区| 亚洲国产精品传媒在线观看| 国产日韩精品在线观看| 亚洲精品一区二区三区99| 国产综合亚洲精品一区二| 一区二区三区精品国产| 亚洲精品1区2区| 欧美亚洲午夜视频在线观看| 中文亚洲欧美| 欧美.日韩.国产.一区.二区| 久久久久久久精| 国产精品毛片| 日韩一级在线观看| 亚洲人成网站在线观看播放| 久久成人一区| 久久精品国产一区二区三区免费看| 欧美精品二区| 亚洲国产精品久久久| 一色屋精品视频在线观看网站| 亚洲一区二区在线免费观看| 亚洲视频你懂的| 欧美日韩精品免费看| 91久久国产综合久久蜜月精品| 亚洲第一区色| 毛片av中文字幕一区二区| 久久野战av| 黄色在线成人| 久久人人爽人人爽| 久久天堂国产精品| 一区在线免费| 久久综合色8888| 亚洲第一精品夜夜躁人人躁| 亚洲丰满在线| 欧美激情免费观看| 99精品国产在热久久下载| 一区二区三区蜜桃网| 欧美三级网址| 亚洲午夜羞羞片| 久久精品成人| 亚洲第一福利视频| 欧美韩国在线| 99riav国产精品| 欧美一区二视频| 激情视频一区| 欧美高清在线观看| 一本色道久久综合| 久久国产精品黑丝| 亚洲成色精品| 欧美激情精品久久久久久大尺度 | 欧美成ee人免费视频| 亚洲欧洲精品一区二区三区波多野1战4 | 久久在精品线影院精品国产| 在线观看日产精品| 欧美国产精品日韩| av成人黄色| 久久成人精品电影| 91久久极品少妇xxxxⅹ软件| 欧美日韩小视频| 亚洲欧美中文日韩v在线观看| 狼人天天伊人久久| 在线综合亚洲| 红桃av永久久久| 欧美日韩成人在线播放| 欧美亚洲午夜视频在线观看| 亚洲国产精品成人一区二区| 性久久久久久久久久久久| 亚洲国产精品久久久久久女王| 欧美日韩在线观看视频| 久久久999国产| 9i看片成人免费高清| 久久亚洲精品欧美| 亚洲无亚洲人成网站77777| 狠狠入ady亚洲精品| 欧美视频在线观看一区| 老色批av在线精品| 亚洲欧美美女| 日韩一级大片在线| 欧美电影资源| 久久精品亚洲精品国产欧美kt∨| 亚洲精品一区二区三区樱花| 国产伊人精品| 国产精品综合不卡av| 欧美精品一区二区三区在线播放 | 性8sex亚洲区入口| 99re国产精品| 亚洲黄色高清| 黄色一区二区在线观看| 国产精品综合| 国产精品久久一级| 欧美日韩精品免费在线观看视频| 久久久国产91| 欧美在线观看视频在线| 亚洲午夜免费福利视频| 亚洲精品中文字| 亚洲国产精品久久久久| 欧美高清免费| 免费欧美日韩| 美日韩精品视频免费看| 久久国产毛片| 欧美自拍偷拍午夜视频| 香蕉成人伊视频在线观看| 亚洲与欧洲av电影| 亚洲午夜91| 亚洲欧美影院| 欧美亚洲一区二区在线观看| 亚洲一区二区综合| 亚洲综合社区| 性欧美长视频| 欧美在线影院| 久久久久久亚洲精品杨幂换脸| 久久精品国产77777蜜臀 | 亚洲伦理精品| 9久草视频在线视频精品| 99在线精品观看| 亚洲少妇诱惑| 亚洲欧美一区二区原创| 久久精品免费看| 久久综合网色—综合色88| 欧美国产三级| 亚洲全部视频| 亚洲图色在线| 香港久久久电影| 开元免费观看欧美电视剧网站| 玖玖精品视频| 欧美性猛交xxxx乱大交退制版| 国产精品黄色在线观看| 国产亚洲欧洲997久久综合| 一区二区三区中文在线观看 | 日韩亚洲在线| 午夜精品一区二区三区四区| 久久成年人视频| 欧美高清不卡| 在线中文字幕不卡| 久久精品视频免费播放| 欧美成人综合| 国产精品日韩在线一区| 黄色成人片子| 一本色道久久综合亚洲精品不卡| 亚洲欧美精品一区| 老色鬼久久亚洲一区二区| 欧美国产亚洲视频| 亚洲一区免费观看| 久久综合久色欧美综合狠狠| 欧美午夜精品理论片a级大开眼界 欧美午夜精品理论片a级按摩 | 另类成人小视频在线| 亚洲精品五月天| 欧美在线黄色| 欧美日韩精品免费看| 红桃视频国产精品| 亚洲欧美精品| 亚洲国产成人av在线| 午夜精品av| 欧美日韩在线电影| 亚洲国产第一| 久久精品国产v日韩v亚洲 | 亚洲剧情一区二区| 久久精品欧美| 国产精品―色哟哟| 99热这里只有精品8| 久久夜色精品国产欧美乱极品| 99精品欧美一区二区三区| 久久久久天天天天| 国产精品女主播一区二区三区| 亚洲精品男同| 免费av成人在线| 先锋影音网一区二区| 国产精品第十页| 日韩小视频在线观看专区| 欧美成人国产| 狂野欧美激情性xxxx| 国内久久精品视频|