https://www.cnblogs.com/X-Do-Better/p/8628492.html
https://blog.csdn.net/weixin_38686780/article/details/79636290
头文件
#include <string>
初始化
string s1 //默认初始化,s1是一个空串
string s2(s1); // s2是s1的副本 等价于 string s2=s1;
string str3( s2, 11, 4 );//从s2的第11位开始复制4个字符
string s3(“value”); //s3是字面值“value”的副本,除了字面值最后的空字符除外
string s3=“value” //同上
string s4(n,‘c’); //把s4初始化为由连续n个字符c组成的串
操作符(Operators)
你可以用 ==, >, <, >=, <=, and !=比较字符串. 可以用 + 或者 += 操作符连接两个字符串, 并且可以用[]获取特定的字符.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cout << "Please input your name:"<<endl;
cin >> str;
if( str == "Li" ) // 字符串相等比较
cout << "you are Li!"<<endl;
else if( str != "Wang" ) // 字符串不等比较
cout << "you are not Wang!"<<endl;
else if( str < "Li") // 字符串小于比较,>、>=、<=类似
cout << "your name should be ahead of Li"<<endl;
else
cout << "your name should be after of Li"<<endl;
str += ", Welcome!"; // 字符串+=
cout << str<<endl;
for(int i = 0 ; i < str.size(); i ++)
cout<<str[i]; // 类似数组,通过[]获取特定的字符
return 0;
}
程序执行结果为:
Please input your name:
Zhang↙
you are not Wang!
Zhang, Welcome!
Zhang, Welcome!
string特性描述
int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)
int max_size()const; //返回string对象中可存放的最大字符串的长度
int size()const; //返回当前字符串的大小
int length()const; //返回当前字符串的长度
bool empty()const; //当前字符串是否为空
void resize(int len,char c); //把字符串当前大小置为len,多去少补,多出的字符c填充不足的部分
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
if (str.empty())
cout<<"str is NULL."<<endl;
else
cout<<"str is not NULL."<<endl;
str = str + "abcdefg";
cout<<"str is "<<str<<endl;
cout<<"str's size is "<<str.size()<<endl;
cout<<"str's capacity is "<<str.capacity()<<endl;
cout<<"str's max size is "<<str.max_size()<<endl;
cout<<"str's length is "<<str.length()<<endl;
str.resize(20,'c');
cout<<"str is "<<str<<endl;
str.resize(5);
cout<<"str is "<<str<<endl;
return 0;
}
程序执行结果为:
str is NULL.
str is abcdefg
str's size is 7
str's capacity is 15
str's max size is 4294967294
str's length is 7
str is abcdefgccc
str is abcde
string的查找
由于查找是使用最为频繁的功能之一,string提供了非常丰富的查找函数:(注:string::npos)
size_type find( const basic_string &str, size_type index );
//返回str在字符串中第一次出现的位置(从index开始查找),如果没找到则返回string::npos
size_type find( const char *str, size_type index ); // 同上
size_type find( const char *str, size_type index, size_type length );
//返回str在字符串中第一次出现的位置(从index开始查找,长度为length),如果没找到就返回string::npos
size_type find( char ch, size_type index );
// 返回字符ch在字符串中第一次出现的位置(从index开始查找),如果没找到就返回string::npos
#include<iostream>
#include<string>
using namespace std;
int main(){
int loc;
string s="study hard and make progress everyday! every day!!";
loc=s.rfind("make",10) ; //从10开始查找
cout<<"the word make is at index"<<loc<<endl;//-1表示没找到
loc=s.rfind("make");//缺省状态下,从最后一个往前找
cout<<"the word make is at index"<<loc<<endl;
loc=s.find_first_of("day");
cout<<"the word day(first) is at index "<<loc<<endl;
loc=s.find_first_not_of("study");
cout<<"the first word not of study is at index"<<loc<<endl;
loc=s.find_last_of("day");
cout<<"the last word of day is at index"<<loc<<endl;
loc=s.find("day");//缺陷状态下从第一个往后找
cout<<loc;
return 0;
}
其他常用函数
string &insert(int p,const string &s); //在p位置插入字符串s
string &replace(int p, int n,const char *s); //删除从p开始的n个字符,然后在p处插入串s
string &erase(int p, int n); //删除p开始的n个字符,返回修改后的字符串
string substr(int pos = 0,int n = npos) const; //返回pos开始的n个字符组成的字符串
void swap(string &s2); //交换当前字符串与s2的值
string &append(const char *s); //把字符串s连接到当前字符串结尾
void push_back(char c) //当前字符串尾部加一个字符c
const char *data()const; //返回一个非null终止的c字符数组,data():与c_str()类似,用于string转const char*其中它返回的数组是不以空字符终止,
const char *c_str()const; //返回一个以null终止的c字符串,即c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同,用于string转const char*
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "abc123defg";
string str2 = "swap!";
cout<<str1<<endl;
cout<<str1.erase(3,3)<<endl; //从索引3开始的3个字符,即删除掉了"123"
cout<<str1.insert(0,"123")<<endl; //在头部插入
cout<<str1.append("123")<<endl; //append()方法可以添加字符串
str1.push_back('A'); //push_back()方法只能添加一个字符
cout<<str1<<endl;
cout<<str1.replace(0,3,"hello")<<endl; //即将索引0开始的3个字符替换成"hello"
cout<<str1.substr(5,7)<<endl; //从索引5开始7个字节
str1.swap(str2);
cout<<str1<<endl;
const char* p = str.c_str();
printf("%s\n",p);
return 0;
}
运行结果:
abc123defg
abcdefg
123abcdefg
123abcdefg123
123abcdefg123A
helloabcdefg123A
abcdefg
swap!
swap!
去重
#include <algorithm>
int n = unique(ss.begin(), ss.begin() + ss.length()) - ss.begin();