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

清風竹林

ぷ雪飄絳梅映殘紅
   ぷ花舞霜飛映蒼松
     ----- 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 李現民 閱讀(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>
            亚洲欧美日韩电影| 一本一本久久a久久精品牛牛影视| 9人人澡人人爽人人精品| 亚洲第一综合天堂另类专| 久久久久久久久岛国免费| 好看不卡的中文字幕| 欧美成人亚洲成人日韩成人| 欧美韩国日本综合| 亚洲在线国产日韩欧美| 亚洲欧洲99久久| 亚洲第一精品久久忘忧草社区| 欧美激情视频一区二区三区不卡| 欧美日韩国产综合视频在线| 亚洲欧美视频一区| 久久免费视频网| 一本色道久久88综合亚洲精品ⅰ| 日韩视频在线一区二区三区| 国产精品色一区二区三区| 久久影视精品| 欧美视频1区| 美女精品国产| 国产精品免费网站| 欧美激情亚洲一区| 国产精品亚洲网站| 亚洲国产导航| 国产一区av在线| 亚洲日本激情| 经典三级久久| 亚洲午夜久久久| 亚洲高清在线视频| 香蕉视频成人在线观看| av成人免费在线| 久久精品国产亚洲a| 亚洲一区黄色| 欧美成年人网| 美女久久一区| 国产精品亚洲不卡a| 亚洲国产精品日韩| 黄色影院成人| 亚洲欧美在线另类| 亚洲午夜视频在线观看| 免费亚洲电影| 久久久久久久波多野高潮日日| 欧美日韩一区国产| 亚洲国产婷婷香蕉久久久久久| 国产一区99| 亚洲欧美变态国产另类| 亚洲一区二区av电影| 欧美国产精品va在线观看| 久久人人爽爽爽人久久久| 国产精品一区二区久久久| 夜夜嗨网站十八久久| 亚洲欧洲日本mm| 久久女同互慰一区二区三区| 蜜桃av噜噜一区| 欧美中文日韩| 国产免费成人| 性感少妇一区| 久久精品一二三区| 国产一区二区三区日韩| 欧美一级成年大片在线观看| 性欧美超级视频| 国产婷婷色一区二区三区| 亚洲欧美制服中文字幕| 久久久久国产精品一区| 国产一区二区三区免费观看| 欧美一进一出视频| 久久亚洲国产精品日日av夜夜| 国产伊人精品| 久久久人成影片一区二区三区 | 国产主播一区二区| 久久精品国产一区二区三区免费看 | 国产精品美女久久久久久免费| 中文久久精品| 久久国产精品毛片| 激情校园亚洲| 能在线观看的日韩av| 亚洲免费av网站| 亚洲男人av电影| 国内精品国产成人| 欧美+日本+国产+在线a∨观看| 91久久视频| 亚洲欧美999| 国内一区二区在线视频观看| 蜜臀久久99精品久久久画质超高清| 亚洲国产99精品国自产| 亚洲一区自拍| 狠狠爱综合网| 欧美美女喷水视频| 亚洲欧美在线另类| 欧美激情四色| 性欧美超级视频| 91久久极品少妇xxxxⅹ软件| 欧美视频在线观看视频极品| 欧美在线看片| 亚洲人成在线影院| 欧美一区二区在线看| 最新亚洲一区| 国产欧美婷婷中文| 欧美激情二区三区| 欧美一区二区三区在线看| 亚洲激情女人| 久久影视三级福利片| 亚洲小说欧美另类社区| 原创国产精品91| 国产精品视频99| 欧美黑人在线观看| 久久国产精品99国产精| 一区二区三区|亚洲午夜| 免费一级欧美在线大片| 午夜视频一区在线观看| 亚洲人人精品| 精品成人一区二区三区| 国产精品日韩欧美综合| 欧美国产日韩一二三区| 久久久成人网| 香蕉成人伊视频在线观看| 最新国产乱人伦偷精品免费网站 | 久久精品九九| 亚洲欧美卡通另类91av| 日韩视频一区二区在线观看 | 永久免费精品影视网站| 国产噜噜噜噜噜久久久久久久久 | 欧美成年人网站| 久久夜色撩人精品| 欧美一区二区成人6969| 亚洲色诱最新| 一区二区日本视频| 99成人在线| 夜夜夜精品看看| 日韩亚洲在线| 99re6热只有精品免费观看| 欧美成人一区在线| 免费观看亚洲视频大全| 久久一区二区视频| 久久综合五月天婷婷伊人| 久久免费国产| 久久深夜福利免费观看| 久久久水蜜桃| 噜噜噜久久亚洲精品国产品小说| 欧美在线视频导航| 久久九九99视频| 久久综合狠狠| 欧美风情在线观看| 欧美激情四色| 亚洲美女av在线播放| 中文久久乱码一区二区| 午夜国产精品视频| 欧美一区三区二区在线观看| 久久成人免费| 欧美超级免费视 在线| 欧美精品在欧美一区二区少妇| 欧美日韩成人| 国产欧美精品一区| 好看的亚洲午夜视频在线| 亚洲国产另类 国产精品国产免费| 亚洲激情啪啪| 亚洲在线黄色| 久久综合狠狠综合久久综合88| 欧美激情精品久久久久久大尺度| 91久久精品www人人做人人爽| 日韩视频不卡中文| 新狼窝色av性久久久久久| 久久综合福利| 欧美午夜视频在线观看| 国产手机视频一区二区| 在线欧美电影| 亚洲社区在线观看| 久久久www| 亚洲精品黄网在线观看| 亚洲一区999| 久久中文字幕一区| 欧美亚州一区二区三区| 一区二区视频免费完整版观看| 日韩视频一区二区在线观看| 欧美专区亚洲专区| 亚洲激情综合| 欧美在线视频免费播放| 欧美日韩的一区二区| 国语精品一区| 亚洲一区二区三区777| 乱人伦精品视频在线观看| 夜夜精品视频一区二区| 久久影视精品| 国产三区二区一区久久| 亚洲伦伦在线| 久久综合国产精品| 亚洲欧美日本视频在线观看| 欧美成人情趣视频| 国内一区二区在线视频观看| 亚洲欧美日韩国产一区| 亚洲国产一区二区精品专区| 欧美伊人久久| 国产精品99免费看 | 欧美日韩高清在线| 国模吧视频一区| 欧美一区日韩一区| 日韩天堂在线视频| 欧美成人免费在线视频| 在线视频成人|