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

清風竹林

ぷ雪飄絳梅映殘紅
   ぷ花舞霜飛映蒼松
     ----- 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 李現民 閱讀(2047) 評論(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>
            另类av一区二区| 欧美精品福利在线| 国产亚洲福利一区| 久久高清福利视频| 久久国产精品99久久久久久老狼| 国产精品一区二区三区乱码 | 久久久美女艺术照精彩视频福利播放| 国产精品久久一区主播| 欧美影院午夜播放| 久久久国产精品一区二区三区| 黄色国产精品| 欧美电影免费| 欧美日韩一区二区在线视频| 亚洲一区免费视频| 欧美一区二区三区四区高清 | 国产偷国产偷精品高清尤物| 久久精品国语| 欧美成人资源网| 亚洲欧美中文日韩在线| 欧美在线观看一区二区| 亚洲国产欧美一区| 中文欧美字幕免费| 一区在线播放| aa国产精品| 尤物精品在线| 日韩五码在线| 在线播放视频一区| 艳妇臀荡乳欲伦亚洲一区| 韩国成人理伦片免费播放| 91久久国产精品91久久性色| 国产精品视频久久一区| 免费人成精品欧美精品| 欧美新色视频| 亚洲成人在线网| 国产亚洲精品福利| 日韩视频一区二区三区在线播放| 国产欧美日韩一区二区三区| 亚洲电影在线看| 国产日韩欧美精品综合| 日韩视频二区| 亚洲日本成人网| 欧美中文日韩| 欧美一区永久视频免费观看| 欧美激情黄色片| 免费在线一区二区| 国产日韩在线视频| 亚洲婷婷在线| 9久草视频在线视频精品| 久久久久久久999精品视频| 亚洲欧美激情视频| 欧美连裤袜在线视频| 免费观看30秒视频久久| 国产欧美日韩专区发布| 一区二区三区成人精品| 99国产一区| 欧美成人精品一区| 欧美波霸影院| 在线日韩av片| 久久这里有精品视频| 久久综合久久久久88| 狠狠色狠狠色综合日日五| 欧美一区二区视频免费观看 | 在线成人h网| 久久av免费一区| 久久九九热免费视频| 国产欧美一区二区三区沐欲| 一区二区电影免费观看| 亚洲一区二区三区国产| 欧美日韩免费观看中文| 99视频一区二区| 亚洲影院污污.| 国产精品久久久久影院亚瑟| 一级日韩一区在线观看| 亚洲欧美综合国产精品一区| 国产精品成人v| 一区二区三区欧美成人| 亚洲欧美视频一区| 国产乱理伦片在线观看夜一区| 亚洲一区二区久久| 久久人人九九| 亚洲激情六月丁香| 欧美激情中文字幕一区二区| 亚洲精品免费在线观看| 亚洲天堂网在线观看| 国产精品一香蕉国产线看观看 | 欧美在线视频一区二区| 国产一二三精品| 麻豆成人在线| 99re成人精品视频| 久久精品免费| 亚洲精品欧美日韩专区| 欧美日韩一区二区三区| 亚洲自拍偷拍一区| 免费观看一区| 在线午夜精品自拍| 国内视频一区| 欧美另类极品videosbest最新版本| 99国产精品自拍| 久久久久欧美| 一区二区三区鲁丝不卡| 国产午夜精品一区理论片飘花| 久久久精品国产免费观看同学| 亚洲黄色尤物视频| 欧美亚洲综合另类| 最近中文字幕mv在线一区二区三区四区 | 亚洲一区视频在线| 在线观看日韩| 欧美偷拍另类| 久久蜜臀精品av| 亚洲午夜国产成人av电影男同| 欧美成黄导航| 午夜亚洲精品| 日韩视频永久免费| 国产专区一区| 国产精品久久久爽爽爽麻豆色哟哟| 久久婷婷国产综合尤物精品| 99精品视频免费在线观看| 免费中文字幕日韩欧美| 午夜亚洲一区| 一区二区三区国产精品| 在线观看精品视频| 国产欧美在线观看| 欧美色中文字幕| 欧美国产三级| 久久亚洲国产精品日日av夜夜| 亚洲欧美日韩精品久久| 亚洲美女性视频| 欧美韩国在线| 蜜桃久久av| 久久久国产一区二区三区| 亚洲尤物在线| 亚洲私人影吧| 中文在线资源观看视频网站免费不卡| 在线欧美日韩精品| 伊人伊人伊人久久| 国产在线日韩| 国产综合第一页| 国语自产精品视频在线看抢先版结局| 国产精品久久久久久久电影| 欧美日韩国产精品自在自线| 女同性一区二区三区人了人一 | 中国av一区| 中文一区字幕| 亚洲一区二区三区高清不卡| 99re6这里只有精品| 一区二区免费在线播放| 99国内精品久久| 亚洲色无码播放| 亚洲自拍偷拍麻豆| 欧美一区二区大片| 久久精品五月| 乱码第一页成人| 欧美激情第一页xxx| 欧美日韩国语| 国产精品尤物福利片在线观看| 欧美视频中文一区二区三区在线观看| 欧美日韩在线视频首页| 国产精品美腿一区在线看 | 乱人伦精品视频在线观看| 久久人人爽人人爽爽久久| 美女国产一区| 欧美区一区二区三区| 欧美日韩亚洲一区三区| 国产精品综合| 极品少妇一区二区三区| 亚洲精品在线视频观看| 一区二区三区日韩欧美精品| 午夜久久久久久久久久一区二区| 久久精品国产99国产精品澳门| 久久综合久久综合久久综合| 欧美激情亚洲综合一区| 99这里只有久久精品视频| 午夜视频一区在线观看| 免费美女久久99| 国产精品理论片在线观看| 国产在线一区二区三区四区| 亚洲娇小video精品| 亚洲一区二区三区国产| 狼人天天伊人久久| 日韩午夜激情av| 久久精品色图| 欧美午夜一区二区福利视频| 国产亚洲一区二区三区在线观看 | 亚洲缚视频在线观看| 一区二区高清视频在线观看| 欧美一区二区日韩一区二区| 欧美大色视频| 亚洲影院色无极综合| 欧美成人免费全部观看天天性色| 国产精品萝li| 亚洲精品日日夜夜| 久久久久久久国产| 日韩视频在线免费| 久久久久久网| 国产人妖伪娘一区91| 中文久久精品| 亚洲第一区色| 久久久国产亚洲精品| 国产精品乱码一区二区三区| 亚洲精选国产|