C++ 标准库string
基础操作
#include <string>
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
string s1 = "value_s1";
string s2(s1);
string s3("value_s3");
string s4(4,'c');
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
cout << "s4 = " << s4 << endl;
cout << "size of s1:" << s1.size() << endl;
cout << "s2 is empty?:" << s2.empty() << endl;
cout << "s1 + s2 = :" << s1+s2 << endl;
string a,b;
cout << "input string:" << endl;
cin >> a >> b;
cout << "a = " << a << "b = " << b << endl;
getline从流中读取: 读取一整行
string c;
while (getline(cin, c)){
cout << "c = " << c << endl;
}
string str="ab!!cd,,efg";
for(auto c: str){
cout << c <<endl;
}
cout << "使用范围for语句遍历string:"<<endl;
decltype(str.size()) count = 0;
for(auto c:str){
if(ispunct(c))
count++;
}
for(auto &c:str){
c = toupper(c);
}
cout <<"number of puncts:"<< count << endl;
cout << "str = "<<str<<endl;
cout << "下标访问" <<endl;
for(int i = 0; i!=str.size();i++){
cout << str[i] <<endl;
}
cout << "迭代器访问" <<endl;
for(auto it=str.begin(); it!=str.end(); ++it){
cout<<*it<<endl;
}
return 0;
}
进阶操作
构造、初始化string
string s(cp,n)
从数组中拷贝前n个字符
string s(s2,pos2)
从字符串s2的pos2位置开始拷贝
string s(s2,pos2,len2)
从字符串s2的pos2位置拷贝len2个字符
const char *cp = "hello world!!";
char p[]={'h','w'};
string s1(cp);
string s2(p);
string s3(p,2);
string s4(cp+6,5);
string s5(s1,6,5);
string s6(s1,6);
string s7(s1,6,20);
string s8(s1,16);
s.substr(pos,n)
从string的pos位置开始拷贝n个字符
string s("hello world");
string s2=s.substr(0,5);
string s3=s.substr(6);
string s4=s.substr(6,11);
string s5=s.substr(12);
更改string
- string的
insert()
和erase()
可以接受迭代器,也可以接受下标;
s.insert(s.size(),5,'!');
s.insert(0,s2);
s.insert(0,s2,0,s2.size());
s.erase(s.size()-5,5);
const char *cp="Stately,plump Buck";
s.assign(cp,7);
s.insert(s.size(),cp+7);
string s1("abcd"),s2=s1;
s1.insert(s1.size(),"efg");
s2.append("efg");
s1.replace(4,3,"EFG");
s2.replace(4,3,"EFGH");
搜索操作
s.find(args)
在s中寻找args第一次出现的位置
s.rfind(args)
在s中寻找args最后一次出现的位置
s.find_first_of(args)
在s中寻找args中任意元素第一次出现的位置
s.find_first_not_of(args)
在s中寻找args中任意元素第一次没出现的位置
s.find_last_of(args)
在s中寻找args中任意元素最后一次出现的位置
s.find_last_not_of(args)
在s中寻找args中任意元素最后一次没出现的位置
string s("Abcdefgsdf");
auto pos=s.find("Abcd");
auto pos1=s.rfind("f");
cout<<pos<<" "<<pos1<<endl;
string numbers("0123456789"),name("rbd35k4");
auto pos2=name.find_first_of(numbers);
auto pos3=name.find_last_of(numbers);
auto pos4=name.find_first_not_of(numbers);
auto pos5=name.find_last_not_of(numbers);
cout<<pos2<<" "<<pos3<<" "<<pos4<<" "<<pos5<<" "<<endl;
练习:查找string中的所有数字的位置
string temp("al46ien7g234lhbo4534ohln65oh4oh7j653dhg");
string::size_type pos=0;
while(temp.find_first_of(numbers,pos)!=string::npos){
pos=temp.find_first_of(numbers,pos);
cout<<"position:"<<pos<<endl;
pos++;
}
compare函数:
- 大于返回正数,小于返回负数,等于返回0
s.compare(s2)
s与整个s2比较
s.compare(pos1,n1,s2)
s从pos1开始的n1个字符与整个s2比较
s.compare(pos1,n1,s2,pos2,n2)
s从pos1开始的n1个字符与s2从pos2开始的n2个字符比较
s.compare(cp)
s与cp指向的字符数组比较
s.compare(pos1,n1,cp)
s从pos1开始的n1个字符与cp指向的字符数组比较
s.compare(pos1,n1,cp,n2)
s从pos1开始的n1个字符与cp指向的地址开始的n2个字符比较
数值转换
- 非成员函数
to_string(val)
:将任何算数类型数值val转换成string
stoi(s)
:string转换成int整型
stol(s)
:string转换成long整型
stoul(s)
:string转换成unsigned long整型
stoll(s)
:string转换成long long整型
stoull(s)
:string转换成unsigned long long整型
stof(s)
:string转换成float浮点型
stod(s)
:string转换成double浮点型
stold(s)
:string转换成long double浮点型
练习:提取字符串中的数值
string a("pi=3.14");
double d=stod(a.substr(a.find_first_of("+-.0123456789")))