std::string用法总结

本文详细介绍了C++标准库中的string类,包括构造函数、字符操作、输入输出、赋值、连接、比较等功能,以及迭代器使用方法和字符串流处理技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在平常工作中经常用到了string类,本人记忆了不好用到了的时候经常要去查询。在网上摘抄一下总结一下,为以后的查询方便:

string类的构造函数:
string(const char *s);    //用c字符串s初始化string(int n,char c);     //用n个字符c初始化


string类的字符操作:
const char &operator[](int n)const;
const char &at(int n)const;
char &operator[](int n);
char &at(int n);
operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。
const char *data()const;//返回一个非null终止的c字符数组
const char *c_str()const;//返回一个以null终止的c字符串
int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目

注:对于string中对象字符的处理,有很多已有的函数在CCtype头文件中,可以很方便的应用


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填充不足的部分

string类的输入输出操作:string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。
函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'\n'分开。

string的赋值:
string &operator=(const string &s);//把字符串s赋给当前字符串
string &assign(const char *s);//用c类型字符串s赋值
string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值
string &assign(const string &s);//把字符串s赋给当前字符串
string &assign(int n,char c);//用n个字符c赋值给当前字符串
string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串

string的连接:
string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾 
string &append(const char *s);            //把c类型字符串s连接到当前字符串结尾
string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾
string &append(const string &s);    //同operator+=()
string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾
string &append(int n,char c);        //在当前字符串结尾添加n个字符c
string &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾


string的比较:
bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
int compare(const string &s) const;//比较当前字符串和s的大小
int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const;
compare函数在>时返回1,<时返回-1,==时返回0   


string的子串:
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串


string的交换:
void swap(string &s2);    //交换当前字符串与s2的值


string类的查找函数:
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值 
int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值 
int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos 
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos 
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const; 
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找


string类的替换函数:
string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符
string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串


string类的插入函数: string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n);
//前4个函数在p0位置插入字符串s中pos开始的前n个字符
string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符
void insert(iterator it, int n, char c);//在it处插入n个字符c


string类的删除函数 iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串


string类的迭代器处理:
string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。
用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:
const_iterator begin()const;
iterator begin();                //返回string的起始位置
const_iterator end()const;
iterator end();                    //返回string的最后一个字符后面的位置
const_iterator rbegin()const;
iterator rbegin();                //返回string的最后一个字符的位置
const_iterator rend()const;
iterator rend();                    //返回string第一个字符位置的前面
rbegin和rend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator,string::const_reverse_iterator实现


字符串流处理:
通过定义ostringstream和istringstream变量实现,<sstream>头文件中
例如:
    string input("hello,this is a test");
    istringstream is(input);
    string s1,s2,s3,s4;
    is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
    ostringstream os;
    os<<s1<<s2<<s3<<s4;
    cout<<os.str();

标准C++库字符串类std::string的用法

#include<string>
std::string s1;
std::string s3(s2);
std::string s2("this is a string");
begin       得到指向字符串开头的Iterator
end       得到指向字符串结尾的Iterator
rbegin       得到指向反向字符串开头的Iterator
rend       得到指向反向字符串结尾的Iterator
size       得到字符串的大小
length()       和size函数功能相同
max_size       字符串可能的最大大小
capacity       在不重新分配内存的情况下,字符串可能的大小
empty       判断是否为空
operator[]       取第几个元素,相当于数组
c_str       取得C风格的const char* 字符串
data       取得字符串内容地址
operator=       赋值操作符
reserve       预留空间
swap       交换函数
insert       插入字符
append       追加字符
push_back       追加字符
erase       删除字符串
clear       清空字符容器中所有内容
resize       重新分配空间
assign       和赋值操作符一样
replace       替代
copy       字符串到空间
find       查找,返回基于0的索引号
rfind       反向查找
find_first_of       查找包含子串中的任何字符,返回第一个位置
find_first_not_of       查找不包含子串中的任何字符,返回第一个位置
find_last_of       查找包含子串中的任何字符,返回最后一个位置
find_last_not_of       查找不包含子串中的任何字符,返回最后一个位置
substr(n1,len)       得到字符串从n1开始的长度为len的子串
比较字符串(支持所有的关系运算符)
compare       比较字符串
operator+       字符串链接
operator+=       += 操作符
operator==       判断是否相等
operator!=       判断是否不等于
operator<       判断是否小于
operator>>       从输入流中读入字符串
operator<<       字符串写入输出流
getline       从输入流中读入一行
<think>好的,我现在需要解决用户遇到的C++编译错误:“default argumentmissing forparameter5 of‘boolBrooksAWCHelper::moveArm(std::string,std::string,std::string,std::string,std::string)”。用户之前提到过类成员初始化的错误,现在又遇到了函数默认参数的问题。首先,我需要理解这个错误的原因。根据错误信息,函数声明中第五个参数缺少默认参数。默认参数的规则是,一旦某个参数被赋予默认值,其后所有的参数都必须有默认值。也就是说,如果第五个参数有默认值,那么前面的四个参数可能没有正确设置默认值,或者第五个参数本身确实没有默认值,但前面的参数有默认值,导致编译器期望第五个参数也必须要有。接下来,我需要检查用户提供的代码示例。假设用户有一个函数声明如下:```cppclassBrooksAWCHelper{public:boolmoveArm(std::stringa1,std::stringa2,std::stringa3,std::stringa4,std::stringa5);};```如果用户尝试为某个参数设置默认值,比如:```cppboolmoveArm(std::stringa1,std::stringa2="",std::stringa3,std::stringa4,std::stringa5);```这里第二个参数有默认值,但第三个、第四个、第五个没有,这会导致错误,因为后面的参数必须都有默认值。正确的做法应该是从第一个有默认值的参数之后的所有参数都必须有默认值。另一种可能是用户在函数声明中某个参数有默认值,但在定义时没有保持一致。例如,声明中有默认参数,而定义中没有,或者顺序不一致。我需要给出解决方案,可能需要调整参数的默认值设置,确保从第一个有默认值的参数开始,后面的所有参数都有默认值。例如:```cppboolmoveArm(std::string a1,std::string a2 ="",std::string a3 ="", std::stringa4= "",std::string a5 =""); ```或者,如果用户希望第五个参数有默认值,而前面的没有,那需要将第五个参数放在最后,并确保前面的参数在调用时不会遗漏。此外,用户可能在函数声明和定义中默认参数不一致,导致编译器报错。需要提醒用户,默认参数只能在声明或定义中指定一次,不能在两者同时出现。最后,我需要提供代码示例,说明正确的声明方式,并解释每个步骤,确保用户理解默认参数的使用规则。同时,需要指出常见的错误,比如参数顺序错误或重复定义默认参数。可能的相关问题包括默认参数的顺序规则、在类内外声明默认参数的区别、C++11之后的默认参数新特性,以及如何解决默认参数与函数重载的冲突。这些问题可以帮助用户更深入地理解默认参数的使用和潜在问题。</think>这个编译错误源于C++默认参数的顺序规则,以下是解决方案和代码示例: ```cpp // 错误声明示例(参数5缺少默认值) class BrooksAWCHelper { public: bool moveArm(std::string a1, std::string a2 = "default2", std::string a3 = "default3", std::string a4, // 错误:后续参数缺少默认值 std::string a5 = "default5"); }; // 正确解决方案 class BrooksAWCHelper { public: // 正确顺序:默认参数从右向左连续设置 bool moveArm(std::string a1, std::string a2 = "default2", std::string a3 = "default3", std::string a4 = "default4", std::string a5 = "default5") { // 方法实现 return true; } }; ``` **错误原理分析**: 1. **默认参数连续性规则**: - 默认参数必须从参数列表的最右边开始连续设置 - 不能出现`void func(int a=1, int b);`这样的声明 2. **声明与定义一致性**: ```cpp // 头文件声明 class BrooksAWCHelper { public: bool moveArm(std::string a1, std::string a2 = "default"); }; // 源文件定义时不能重复默认参数 bool BrooksAWCHelper::moveArm(std::string a1, std::string a2) { // 实现代码 } ``` **多参数场景处理**: ```cpp // 当需要跳过某些参数时,使用重载函数 class BrooksAWCHelper { public: // 基础版本 bool moveArm(std::string a1, std::string a2 = "default2", std::string a3 = "default3", std::string a4 = "default4", std::string a5 = "default5"); // 简化版本(跳过a2) bool moveArm(std::string a1, std::string a3, std::string a4 = "default4", std::string a5 = "default5") { return moveArm(a1, "default2", a3, a4, a5); } }; ``` **现代C++改进方案**(C++20起): ```cpp // 使用指定初始化(需要C++20) auto moveArm(auto a1, auto... args) -> bool { struct Params { std::string a2 = "default2"; std::string a3 = "default3"; std::string a4 = "default4"; std::string a5 = "default5"; } p; // 使用结构化绑定处理参数 // 处理参数包... return true; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值