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

清風竹林

ぷ雪飄絳梅映殘紅
   ぷ花舞霜飛映蒼松
     ----- 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>
            亚洲激情校园春色| 国产一区二区无遮挡| 亚洲精品免费观看| 免费人成精品欧美精品| 美日韩精品免费观看视频| 亚洲福利在线观看| 亚洲另类春色国产| 欧美系列亚洲系列| 久久精品视频亚洲| 久久三级视频| 一本到高清视频免费精品| 亚洲午夜精品久久| 国产综合精品一区| 亚洲国产婷婷综合在线精品| 欧美好吊妞视频| 亚洲深夜福利网站| 欧美一级精品大片| 亚洲精选视频免费看| 亚洲一二三级电影| 亚洲高清精品中出| 亚洲一区二区三区在线视频| 在线观看的日韩av| 99一区二区| 亚洲电影免费观看高清完整版在线| 亚洲人成在线影院| 国语自产精品视频在线看| 亚洲精品美女免费| 在线视频国内自拍亚洲视频| 99精品免费| 在线日韩中文| 亚洲欧美在线一区二区| 日韩视频中文字幕| 久久精品欧洲| 先锋影音国产一区| 欧美日本亚洲视频| 欧美1区3d| 国产亚洲福利一区| 一区二区高清在线观看| 亚洲福利视频网| 欧美一区二区三区精品电影| 夜夜嗨av一区二区三区| 久久五月天婷婷| 久久精品理论片| 国产精品久久综合| 亚洲美女视频| 日韩视频一区二区| 免费日韩av片| 美女尤物久久精品| 国产视频久久久久| 亚洲视频精选| 在线亚洲一区观看| 欧美护士18xxxxhd| 欧美黑人在线观看| 在线国产欧美| 久久一区免费| 免费黄网站欧美| 一区二区三区中文在线观看| 欧美一区二区在线免费播放| 亚洲欧美中文另类| 国产精品久久久久7777婷婷| 日韩小视频在线观看专区| 亚洲精品午夜| 欧美巨乳在线| 日韩一区二区久久| 亚洲午夜羞羞片| 国产精品久久久久久户外露出| av成人国产| 亚洲欧美成人综合| 国产精品永久免费观看| 亚洲欧美日韩精品久久| 欧美影院午夜播放| 一区二区视频免费在线观看| 久久久久久噜噜噜久久久精品 | 国产日韩欧美91| 香蕉国产精品偷在线观看不卡| 久久精品欧美日韩| 一区在线观看| 欧美电影打屁股sp| 一本大道av伊人久久综合| 亚洲欧美在线观看| 国产一区二区欧美| 麻豆成人综合网| 日韩视频在线播放| 欧美在线视频二区| 亚洲高清一二三区| 欧美日韩国产123| 亚洲永久免费| 欧美国产在线观看| 亚洲永久视频| 一区二区在线不卡| 欧美日韩成人在线| 欧美一区二区高清| 亚洲大片在线| 亚洲欧美日韩一区二区| 国产自产高清不卡| 欧美久久成人| 久久国产精品久久国产精品| 亚洲国产欧美精品| 欧美亚洲一区在线| 亚洲欧洲在线播放| 国产欧美一区视频| 欧美国产精品中文字幕| 午夜激情一区| 日韩性生活视频| 久久中文字幕导航| 亚洲在线视频观看| 亚洲黄页一区| 国产欧美亚洲视频| 欧美日韩亚洲一区二区| 久久久久在线观看| 亚洲主播在线| 99精品国产高清一区二区| 麻豆精品视频在线| 欧美亚洲视频在线观看| 99re6热在线精品视频播放速度| 国产欧美一区二区精品仙草咪| 免费看成人av| 久久久精品日韩欧美| 亚洲综合国产激情另类一区| 亚洲欧洲午夜| 欧美好吊妞视频| 久久亚洲春色中文字幕| 午夜精品在线看| 亚洲午夜女主播在线直播| 亚洲欧洲日本国产| 在线日韩av永久免费观看| 国产日韩专区| 国产精品主播| 国产精品一区二区久久久| 欧美日本高清视频| 欧美激情亚洲自拍| 免费成人高清视频| 免费在线观看精品| 欧美 日韩 国产一区二区在线视频| 久久精品国产一区二区三区| 欧美一级视频免费在线观看| 中文欧美日韩| 亚洲夜晚福利在线观看| 亚洲一区二区在线看| 亚洲一区视频| 亚洲欧美综合国产精品一区| 亚洲视频999| 亚洲专区一区| 欧美一区二区视频在线| 久久精品91久久香蕉加勒比 | 亚洲高清av在线| 欧美激情1区| 亚洲激情视频在线观看| 亚洲三级免费电影| 亚洲精品美女久久7777777| 99xxxx成人网| 亚洲欧美成aⅴ人在线观看| 亚洲免费视频网站| 久久成人这里只有精品| 久久久久一区二区| 欧美成人精品一区二区| 欧美日韩福利| 国产精品一区在线观看你懂的| 国产农村妇女精品| 激情五月婷婷综合| 亚洲精品日韩一| 亚洲一区二区三区777| 欧美在线视频a| 欧美h视频在线| 亚洲美女福利视频网站| 亚洲女人小视频在线观看| 久久精品五月| 欧美另类99xxxxx| 国产精品自拍视频| 亚洲高清资源| 亚洲欧美成aⅴ人在线观看| 久久精品30| 亚洲国产精品久久久久久女王| 在线亚洲欧美视频| 久久久久久久欧美精品| 欧美日韩国产999| 国产亚洲在线观看| 艳妇臀荡乳欲伦亚洲一区| 欧美一区二区高清在线观看| 欧美电影免费观看高清| 亚洲免费小视频| 牛夜精品久久久久久久99黑人| 国产精品二区二区三区| 亚洲黄色影院| 久久国产乱子精品免费女| 欧美激情亚洲国产| 午夜精品久久久久久久久久久久| 欧美成人午夜激情视频| 国产日韩视频一区二区三区| 亚洲精品影院在线观看| 久久精品成人一区二区三区蜜臀| 亚洲欧洲日韩女同| 久久一区二区三区av| 国产精品尤物| 亚洲天堂黄色| 亚洲国产裸拍裸体视频在线观看乱了| 午夜精品视频| 国产精品国产福利国产秒拍| 亚洲精品视频免费观看| 老司机精品视频一区二区三区|