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

清風竹林

ぷ雪飄絳梅映殘紅
   ぷ花舞霜飛映蒼松
     ----- 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>
            噜噜噜91成人网| 亚洲国产va精品久久久不卡综合| 一本久久a久久精品亚洲| 欧美国产一区二区在线观看| 欧美成人综合网站| 亚洲一二三四久久| 午夜久久久久久久久久一区二区| 黄色av一区| 亚洲人精品午夜在线观看| 欧美激情久久久久| 午夜在线观看欧美| 久久综合久久88| 亚洲午夜成aⅴ人片| 午夜视频在线观看一区二区| 亚洲国产精品久久久久婷婷884| 亚洲狠狠婷婷| 国产麻豆精品theporn| 免费中文字幕日韩欧美| 欧美三日本三级少妇三99| 久久久久久久一区二区三区| 欧美国产欧美亚州国产日韩mv天天看完整 | 欧美不卡三区| 欧美性色视频在线| 蜜臀久久99精品久久久画质超高清| 欧美激情在线观看| 久久精品99国产精品| 欧美日本国产一区| 农村妇女精品| 国产欧美另类| 99视频一区| 最新亚洲激情| 久久精品在线播放| 亚洲欧美日韩国产一区二区| 欧美不卡一卡二卡免费版| 香蕉国产精品偷在线观看不卡| 男男成人高潮片免费网站| 久久av资源网| 国产精品狠色婷| 亚洲黄色影片| 亚洲国产裸拍裸体视频在线观看乱了 | 亚洲欧洲精品一区二区三区波多野1战4| 亚洲视频免费看| 99精品久久久| 美国三级日本三级久久99| 久久久九九九九| 国产精品久久久久久亚洲毛片| 亚洲黄色有码视频| 激情国产一区二区| 欧美在线亚洲在线| 久久精品国产欧美激情| 国产欧美日韩亚洲一区二区三区| 夜夜嗨网站十八久久| 一本久道久久久| 欧美国产日本| 亚洲电影自拍| 亚洲精品午夜| 欧美激情综合色综合啪啪| 欧美激情偷拍| 亚洲美女黄色片| 嫩草影视亚洲| 亚洲精品欧美日韩专区| 亚洲免费电影在线| 欧美日韩不卡在线| 一区二区国产日产| 午夜国产精品视频| 国产欧美在线| 久久精品91久久香蕉加勒比| 两个人的视频www国产精品| 在线欧美电影| 欧美激情网友自拍| 日韩亚洲在线| 午夜在线成人av| 国产综合色产在线精品| 久久精品导航| 欧美激情一区二区| 亚洲午夜高清视频| 国产亚洲人成网站在线观看| 久久精品国产精品 | 在线视频精品一| 国产精品久久久久久久久果冻传媒| 亚洲在线观看视频| 美女精品一区| 一区二区三区免费观看| 国产麻豆精品久久一二三| 久久久av水蜜桃| 亚洲老板91色精品久久| 欧美一区二区三区在线播放| 伊人久久av导航| 欧美激情黄色片| 午夜精品av| 亚洲国产人成综合网站| 欧美一区二区啪啪| 亚洲人成网站在线观看播放| 国产精品sss| 另类国产ts人妖高潮视频| 一区二区三区高清不卡| 久久综合久色欧美综合狠狠| 一本色道久久综合精品竹菊| 国产视频一区二区在线观看| 麻豆精品国产91久久久久久| 正在播放亚洲| 亚洲国产高清一区| 久久精品一二三| 一本综合精品| 在线免费观看欧美| 国产女主播一区| 欧美日本高清视频| 麻豆久久婷婷| 欧美一级网站| 在线亚洲成人| 亚洲国产成人精品视频| 久久久久久91香蕉国产| 亚洲一区中文字幕在线观看| 91久久国产综合久久91精品网站 | 久久久精品2019中文字幕神马| 夜夜爽夜夜爽精品视频| 欧美激情精品久久久久久久变态| 性色av一区二区怡红| 一区二区三区福利| 91久久精品国产91性色| 国际精品欧美精品| 国产酒店精品激情| 国产精品家庭影院| 欧美日韩国产精品一区| 欧美a级片一区| 狼人天天伊人久久| 久久一区亚洲| 久久久精品国产99久久精品芒果| 亚洲男人的天堂在线观看| 日韩亚洲欧美成人一区| 亚洲日本视频| 亚洲欧洲一区二区在线播放| 欧美激情视频一区二区三区不卡| 你懂的视频欧美| 欧美不卡在线| 欧美福利在线| 亚洲国产黄色| 亚洲国产日本| 99国产麻豆精品| 一区二区三区www| 国产精品99久久99久久久二8| 夜夜嗨av一区二区三区| 在线性视频日韩欧美| 亚洲欧美日韩国产中文| 小处雏高清一区二区三区 | 亚洲精品美女久久久久| 亚洲精品永久免费| 国产精品99久久久久久久久久久久 | 美女视频网站黄色亚洲| 欧美福利小视频| 亚洲欧洲一区二区天堂久久| 日韩视频免费大全中文字幕| 99精品国产99久久久久久福利| 一区二区欧美激情| 午夜精品一区二区三区四区| 久久精品国产亚洲aⅴ| 久久婷婷国产综合国色天香| 欧美福利一区二区| 欧美视频亚洲视频| 国产午夜精品一区理论片飘花 | 久久久久久亚洲精品中文字幕| 久久露脸国产精品| 亚洲成人在线视频播放| 亚洲国产成人久久综合| 亚洲美女一区| 欧美一级在线播放| 免费一级欧美在线大片| 国产精品福利在线观看网址| 国产一区亚洲一区| 亚洲美女免费精品视频在线观看| 亚洲欧美日韩一区二区在线| 久热这里只精品99re8久| 亚洲精品国久久99热| 性做久久久久久免费观看欧美 | 久久久成人网| 欧美日韩一区二区三区在线看| 国产午夜精品全部视频播放| 亚洲精品中文字| 久久久久se| 亚洲最快最全在线视频| 久久久久国产精品午夜一区| 欧美性猛交xxxx免费看久久久| 影音先锋久久精品| 亚洲欧美日韩中文视频| 欧美激情影音先锋| 欧美亚洲一区在线| 欧美揉bbbbb揉bbbbb| 在线国产精品一区| 欧美在线播放高清精品| 亚洲国产日韩一区二区| 欧美主播一区二区三区| 欧美性猛交视频| 亚洲最新合集| 欧美国产成人精品| 久久久91精品| 国产日韩欧美一区二区三区在线观看 | 美女999久久久精品视频| 亚洲午夜视频在线| 欧美日韩综合在线免费观看| 亚洲精品乱码久久久久久按摩观|