第三方库用习惯了,回到标准c++下各种不适应。
因为软件大小的限制,不能集成这些库。一个Qt库几十MB,boost就更甭说了。
还好常用的就只有那么些。
string& replace_all(string& str,const string& old_value,const string& new_value)
{
while(true) {
string::size_type pos(0);
if( (pos=str.find(old_value))!=string::npos )
str.replace(pos,old_value.length(),new_value);
else break;
}
return str;
}
string& replace_all_distinct(string& str,const string& old_value,const string& new_value)
{
for(string::size_type pos(0); pos!=string::npos; pos+=new_value.length()) {
if( (pos=str.find(old_value,pos))!=string::npos )
str.replace(pos,old_value.length(),new_value);
else break;
}
return str;
}
字符串分隔
//字符串分割函数
std::vector<std::string> split(std::string str, std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
str += pattern;//扩展字符串以方便操作
unsigned size = str.size();
for (unsigned i = 0; i < size; i++)
{
pos = str.find(pattern, i);
if (pos < size)
{
std::string s = str.substr(i, pos - i);
result.push_back(s);
i = pos + pattern.size() - 1;
}
}
return result;
}
使用regex方式。
(需要你了解正则表达式)这种方式功能很强大。
vector<string> find_all_string(const char *buff, const char *pattern)
{
vector<string> outList;
string t_str = buff;
regex t_pattern(pattern);
smatch result;
string::const_iterator iterStart = t_str.begin();
string::const_iterator iterEnd = t_str.end();
string temp;
while (regex_search(iterStart, iterEnd, result, t_pattern))
{
temp = result[0];
outList.push_back(temp);
iterStart = result[0].second;
//cout << "robin:" << temp;
}
return outList;
}
调用方式:
vector<string> t_oneVector = find_all_string("+OK 0c179159-0557-462a-b7ed-1c20549ed27d", "[\\w|\+|\-]+");
vector<string> t_twoVector = find_all_string("-ERR NO_ANSWER", "[\\w|\+|\-]+");
list的排序 来源
static bool rCompare(const std::string &s1, const std::string &s2)
{
std::vector<std::string> t_s1Split = split(s1, ":");
std::vector<std::string> t_s2Split = split(s2, ":");
std::string s1_key = t_s1Split.at(0);
std::string s1_value = t_s1Split.at(1);
std::string s2_key = t_s2Split.at(0);
std::string s2_value = t_s2Split.at(1);
bool ok1 = false;
bool ok2 = false;
//std::string t_s1_value_val = ;
s1_value.erase(remove_if(s1_value.begin(), s1_value.end(),
bind2nd(equal_to<char>(), '\"')),
s1_value.end());
s2_value.erase(remove_if(s2_value.begin(), s2_value.end(),
bind2nd(equal_to<char>(), '\"')),
s2_value.end());
int t_iValue1 = atoi(s1_value.c_str());
int t_iValue2 = atoi(s2_value.c_str());
if (0 != t_iValue1
&& 0 != t_iValue2)
{
return t_iValue1 < t_iValue2;
}
else
{
return s1_value < s2_value;
}
return true;
}
//
std::list<std::string> t_loginInfoList;
for (auto i = t_loginInfoMap.begin(); i != t_loginInfoMap.end(); i++)
{
std::string t_oneInfo;
t_oneInfo += i->first;
t_oneInfo += ":";
t_oneInfo += i->second;
t_loginInfoList.push_back(t_oneInfo);
}
//进行自定义排序
t_loginInfoList.sort(rCompare);
网上有说使用 标准的sort函数,结果并不行,报错。
将十六进制转换成字符串
std::string t_strSha1;
for (int i = 0; i < SHA_DIGEST_LENGTH; i++)
{
char val[3] = { 0 };
sprintf_s(val, 3, "%02x", digest[i]);
t_strSha1.append(val);
}
从string类型转换为bool类型.来源
bool b;
std::string s = "true";
std::istringstream(s) >> std::boolalpha >> b;
当字符串s为“1”时,上面的代码无法正确转换,此时应该用:
···
bool b;
std::string s = “1”;
istringstream(s) >> b;
···
输入数据
std::ostringstream t_strStream;
t_strStream <<"hello "<<"world"<<" dada";
...
string t_sqlstr = t_strStream.str();