STL 字符串处理

STL使用技巧 - 字符串处理

大小写转换
#include <string>
#include <algorithm>
#include <cctyp>
using namespace std;
 
string s("hello");
transform(s.begin(), s.end(), s.begin(), ::toupper);
transform(s.begin(), s.end(), s.begin(), ::tolower);
去掉首尾多余空格
#include <string>
using namespace std;
 
string s("  hello  ");
 
//去掉行首空格
s.earse(0, s.find_first_not_of(' '));
//去掉行尾空格
s.earse( s.find_last_not_of(' ')+1 );
删除所有同一个字符
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
 
string s("   hello, world. say bye   ");
//删除字符串s中的所有空格
s.erase( remove_if( s.begin(),
      s.end(),
      bind2nd(equal_to<char>(), ' ')
                  ),
         s.end()
       );
//上面代码的单行版
s.erase(remove_if(s.begin(), s.end(), bind2nd(equal_to<char>(), ' ')), s.end());
startwith,endwith
以某一段字符串开始或结束

#include <string>
using namespace std;
 
string s("hello, world");
string head("hello");
string tail("ld");
//判断是否以head开头
bool startwith = (s.compare(0, head.size(), head) == 0);
//判断是否以ld结尾
bool endwith = (s.compare(s.size() - tail.size(), tail.size(), tail) == 0);
转换为整数或浮点数
C方法:

#include <cstdlib>
#include <string>
using namespace std;
 
string s("123");
int i = atoi(s.c_str());
long int l = atol(s.c_str());
double f = atof(s.c_str());
C++方法:

#include <string>
#include <sstream>
using namespace std;
 
string s("123");
int ii;
stringstream(s) >> ii;
 
string sd("12.3");
double dd;
stringstream(sd) >> dd;
 
string sb("true");
bool b;
stringstream(sb) >> boolalpha >> b;
容器合并字符串
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
vector<string> vect;
vect.push_back("hello");
vect.push_back(", ");
vect.push_back("world");
 
string s;
//把vector的所有字符串拼接成一个字符串
s = accumulate(vect.begin(), vect.end(), string(""));
反转字符串
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
string s("hello world!");
//对自身进行反转
reverse(s.begin(), s.end());
string s1;
//自身不变,反转后赋值给s1
s1.assign(s.rbegin(), s.rend());
tuple
pair<T1, T2>只能支持两个类型的返回,所以tuple由此而生,它可以支持多个模版参数的返回,相当于升级版的pair。
很多人都用过boost的tuple,却不知实际C++自身已经带了tuple,且不需要C++0x的支持。


#include <iostream>
#include <tr1/tuple>
int main(int argc, const char *argv[])
{
 int a = 1;
 float b = 1.5;
 double c = 2.5;
 char * d = "abc";
 std::tr1::tuple<int, float, double, char*> t = std::tr1::make_tuple(a, b, c, d);
 //也可以是下面的写法
 //std::tr1::tuple<int, float, double, char*> t(a, b, c, d);
 
 //记得是从0开始
 std::cout<<std::tr1::get<0>(t)<<", "
   <<std::tr1::get<1>(t)<<", "
   <<std::tr1::get<2>(t)<<", "
   <<std::tr1::get<3>(t)<<std::endl;
 return 0;
}
输出结果:


1, 1.5, 2.5, abc
在C++0x标准中是直接把tuple移到了,tr1外的std命名空间当中,编译的时候需要加上-std=c++0x选项,上面那段代码就可以改写为:

#include <iostream>
#include <tuple>
int main(int argc, const char *argv[])
{
 int a = 1;
 float b = 1.5;
 double c = 2.5;
 char * d = "abc";
 std::tuple<int, float, double, char*> t = std::make_tuple(a, b, c, d);
 //也可以是下面的写法
 //std::tuple<int, float, double, char*> t(a, b, c, d);
 
 //记得是从0开始
 std::cout<<std::get<0>(t)<<", "
   <<std::get<1>(t)<<", "
   <<std::get<2>(t)<<", "
   <<std::get<3>(t)<<std::endl;
 return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值