STL中的string

string常用函数

1、构造函数

string(const char *s); //用c字符串s初始化

string(int n,char c); //用n个字符c初始化

string类还支持默认构造函数和复制构造函数,如string s1;string s2="hello"

2、删除 erase
有三种用法
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)

string str ="This is an example phrase.";
string::iterator it;

// 第(1)种用法
str.erase (10,8);
cout << str << endl; // "This is an phrase."

// 第(2)种用法
it=str.begin()+9;
str.erase (it);
cout << str << endl; // "This is a phrase."

// 第(3)种用法
str.erase (str.begin()+5, str.end()-7);
cout << str << endl; // "This phrase."


3、查找
https://blog.csdn.net/MakerCloud/article/details/88929516

1)find_first_not_of() 查找当前string与指定的字符串中任意一个字符都不相符的字符,并返回该字符在字符串中第一次出现的位置。

size_t find_first_not_of ( const string& str, size_t pos = 0 ) const;
size_t find_first_not_of ( const char* str, size_t pos, size_t n ) const;
size_t find_first_not_of ( const char* str, size_t pos = 0 ) const;
size_t find_first_not_of ( char ch, size_t pos = 0 ) const;

2) find_first_of() 搜索字符串中属于任意一个str、s或c的字符,并返回字符串中第一个出现的位置。

size_t find_first_of ( const string& str, size_t pos = 0 ) const;
size_t find_first_of ( const char* s, size_t pos, size_t n ) const;
size_t find_first_of ( const char* s, size_t pos = 0 ) const;
size_t find_first_of ( char ch, size_t pos = 0 ) const;

3) find()
1)、查找字母或子串在母串中出现的位置,找不到返回特殊标记npos
if(str.find(ch)!=string::npos){ //查找单个字符
cout<<str.find(ch)<<endl;
}
注意:返回的结果是匹配到的字符的第一个位置,如果有多个字符都可以匹配,那么只会返回第一个

若要找出所有出现的位置请使用
vector<string>& words
for (string word : words) {
int pos = s.find(word);
while ((pos = s.find(word, pos)) != string::npos) {
for (int i = pos; i < pos + word.size(); i++) {
isBold[i] = true;
}
pos++;
}
}

2)、返回子串首次和最后出现的位置
find_first_of()
find_last_of()

3)、查找某一给定位置后的子串位置
int position=s.find("ab", 5); //从字符串s 下标5的位置开始找子串“ab”


4、字符串拼接
用+=
string funOfEmail(string s){
int idnex=s.find('@');
string res;
//res=(isupper(s[0])? (s[0]+32):s[0]) + "*****" +(isupper(s[idnex-1])? (s[idnex-1]+32):s[idnex-1]) + s[idnex]; //这种写法是错误的
res+=isupper(s[0])? (s[0]+32):s[0];
res+="*****";
res+=isupper(s[idnex-1])? (s[idnex-1]+32): s[idnex-1];
res+=s[idnex];
for(int i=idnex+1;i< s.length();i++) {
if(isupper(s[i]))
res+= s[i]+32;
else
res+=s[i];
}
return res;
}

注:大小写转换 :大写转小写 直接加32

5、返回子串
s.substr(pos, n) 返回一个string,包含s中从pos开始的n个字符(pos的默认值是0,n的默认值是s.size() - pos,即不加参数会默认拷贝整个s)

6、逆序(反转)
使用reverse 需包含头文件#include <algorithm>
string str="hello world , hi";
reverse(str.begin(),str.end());

7、返回子串 substr

假设:string s = “0123456789”;
string sub1 = s.substr(5);     //只有一个数字5表示从下标为5开始一直到结尾:sub1 = “56789”
string sub2 = s.substr(5, 3);  //从下标为5开始截取长度为3位:sub2 = “567”

8、输入输出

#include<iostream>
using namespace std;
int n;
cin>>n;
cout<< n<<endl;

字符串
注意:cin提取始终将空格(空格,制表符,换行符...)视为终止要提取的值,因此提取字符串意味着始终提取单个单词,而不是短语或整个句子。
cin>>s

整行字符串输入使用getline()
string s;
while(getline(cin, s)) {

}

字符串流
#include<sstream>
stringstream

格式化输出
包含头文件 : #include<iomanip>
cout<<setiosflags(ios::fixed)<<setprecision(2);合在一起的意思就是输出一个右对齐的小数点后两位的浮点数。

1) 使用setprecision(n)可控制输出流显示浮点数的数字个数 C++默认的流输出数值有效位是6。setprecision(n)就是输出n个数,会有四舍五入。
2) setiosflags(ios::fixed)
setprecision(n)与setiosflags(ios::fixed)合用,可以控制小数点右边的数字个数。

eg:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main() {
double s=20.7843000;
cout << s << endl;
cout << setiosflags( ios::fixed );
cout << "setprecision( 1 )"<< setprecision( 1 )<< s << endl;
cout << "setprecision( 2 )"<< setprecision( 2 )<< s << endl;
cout << "setprecision( 3 )"<< setprecision( 3 )<< s << endl;
cout << "setprecision( 4 )"<< setprecision( 4 )<< s << endl;
cout << "setprecision( 5 )"<< setprecision( 5 )<< s << endl;
cout << "setprecision( 6 )"<< setprecision( 6 )<< s << endl;
cout << "setprecision( 7 )"<< setprecision( 7 )<< s << endl;
cout << "setprecision( 8 )"<< setprecision( 8 )<< s << endl;
return 0;

}

输出:

20.7843:
setprecision( 1 )20.8
setprecision( 2 )20.78
setprecision( 3 )20.784

9、整数与字符串之间的转换

1)整数转换为字符串 to_string()

int n;

string s = to_string(n);  //将整数转换为字符串

2)字符串转整数

string s="1234";

int val=stoi(s);

注意:

stoi函数默认要求输入的参数字符串是符合int范围的[-2147483648, 2147483647],否则会runtime error。
atoi函数则不做范围检查,若超过int范围,则显示-2147483648(溢出下界)或者2147483647(溢出上界)。
stoi头文件:<string>,c++函数
atoi头文件:<cstdlib>,c函数

int main()
{
string s1 = "21474839", s2 = "-214748";
char *s3 = "214748666666663", *s4 = "-21474836488";
cout << stoi(s1) << endl;
cout << stoi(s2) << endl;
cout << atoi(s3) << endl;
cout << atoi(s4) << endl;
return 0;
}

原文地址:https://www.cnblogs.com/sunshine1218/p/12088669.html