查找:
string中提供了一个find()函数,可以查找特定的字串,用起来非常方便。
- string str=”shadandeajian”;
- str.find(“dan”); return 3
- str.find(“z”); return string::npos
str.find(“dan”)的返回值是 3
str.find(“z”)的返回值是 string::npos
注意:
- str.find()的返回值类型是size_t,尽量不要用int来接收。
- 如果查找不到字串,返回string::npos,在大多数编辑器和操作系统下,npos被宏定义为-1.
字符串与数字互转:
#include <sstream>中的stringstream,可以帮助我们字符串与数字互转
stingstream 相当于一个转换器:看下面例子
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <sstream>
#define debug(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
using namespace std;
int main(void){
string str="520";
int a;
stringstream ss;
ss<<str;
ss>>a;
cout<<a;
return 0;
}
我们先把字符串流入ss中,ss转换一下,等到ss流出到a的时候,就已经变成了int类型。同理double longlong都可以的,反方向数字转字符串也是同理。
注意:
如果多次使用ss,每次需要ss.clear()