C++string类的使用

本文详细介绍了C++中的String类,包括接口设计、容量操作(如size(),capacity(),clear(),reserve(),resize()),遍历(正序、倒序和链表),以及修改操作(push_back,append,assign,insert,replace,substr)等内容。
摘要由CSDN通过智能技术生成

一.string接口

string不需要考虑数组越界问题,需要多少就开多少

string s1; //无参数

string s2("hello bit"); //带参数

string s3(s2); //拷贝构造

赋值操作

string s1("hello");

string ret2 = s1 + "world";

cout<< ret2 <<endl ;

二.string类对象的容量操作

1.size

返回字符串有效字符的长度s

string s1("hello world");

cout<<s1.size()<<endl;   //具有通用性
cout<<s1.length()<<endl; //不具有通用性

2.capacity

返回空间总大小

string s1("hello");

cout<<s1.capacity()<<endl;

3.clear

清空有效字符

string s1("hello");

s1.clear(); //清理字符串
s1 += "你好"; //赋值
cout<<s1.capacity()<<endl; //capacity变小了说明释放空间,没变小就没有释放空间
cout<<s1.max_size()<<endl; //判断能开多少空间

4.empty:

5.reserve

为字符串预留空间,确定大概要开多少空间,提前开好,减少扩容,提高效率

string s;
s.reserve(100); //提前开好100个空间



6.resize

将有效字符的个数改成n个,多余的空间用字符c填充

resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减小,底层空间总大小不变。

string s1("hello");

s1.resize(13); //不给实际值,会在字符串后面加上'\0'

s1.reseize(13, 'a'); //给实际值,会在后面加上数值

s1.resize(20, 'x');  //大于实际空间,会相对应的扩容

s1.resize(5);  //比现有的数值要小就会对应的删除

三.string类对象的遍历操作

1.string遍历读和写

string s1("hello");


//读
for(size_t i = 0; i < s1.size(); i++)
{
  cout<<s1[i]<<end;
}


//写
for(size_t i = 0; i < s1.size(); i++)
{
  s1[i]++;
}

2.iterator迭代器

   1.访问元素

   2.遍历元素

   3.接口一致

正序遍历:

string::iterator it = s1.begin();
while (it !=s1.end())
{
  cout<< *it <<" ";    使用*运算符解引用迭代器,获取它当前指向的字符
  ++it;
}


it = s1.begin();    将迭代器重新设置为字符串s1的开始位置  
while(it != s1.end())
{
   *it = 'a';
    ++it;
}

3.reverse倒序遍历

string s1("abcdefg");

string::reverse_iterator it = s1.rbegin();

while(it !=s1.rend())
{
   cout<<*it<<" ";
   it++;    //指向上一个字符
}

//简介写法
auto rit = s1.rbegin();

while(rit != s1.rend())
{
  
   cout<<*rit<<" ";
   ++rit;  // 递增迭代器rit,指向下一个字符  
}

简便写法

把*it赋给ch,ch是*it的拷贝不支持修改

for(auto ch : s1)
{
  ch++;
}

加上&符号就可以修改

for(auto& ch : s1)
{
  ch++;
}

4.链表遍历

list<int>lt;
lt.push_back(1);
list<int>::iterator lit = lt.begin();

while(lit !=li.end())
{
  cout<< it* <<endl;
  ++it;
}

5.operator

用于定义类和对象之间的运算符行为,通过重载operator可以改变已有运算符的行为或者为类对象定义新的预算符行为

1.重载赋值运算符:

operator+=

2.重载加法运算符:

operator+

3.重载流插入运算符:

operator<<

四.string类对象的修改操作

1.push_back

在字符串后尾插入字符

string s1("hello");

s1.push_back('a');

2.append

在字符串后面追加一个字符串,append可以一次插入多个字符串

string s1("hello");

s1.append("hahah");
s1.append(s1);

3.base.assign

可以将=" "类型的初始化数据给覆盖

std::string str("xxxxx");

std::string base = "111111111111111111111111"

base.assign(str);
std::cout<<base<<'\n';

4.insert

数据插入

std::string str("hello");
str.insert(2, 4, 'x');  //2代表位置字符位置,下标位置是3, 4代表要插入多少字符,x要插入的字符
cout<<str<<endl;


replace

空格替换数据

std::string s1("hello");

s1.replace(2, 1, "sss");
cout<<s1<<endl;

5.substr

在str中从pos位置开始截取n个字符,然后将其返回

std::string str = "Hello, world!";  
std::string subStr = str.substr(7, 5); // 从位置7开始提取长度为5的子字符串  
std::cout << subStr << std::endl; // 输出 "world"
str +="b"; //在str后面追加一个字符b

6.c._str

返回c格式字符串

int main() {  
    std::string s = "Hello, World!";  
    const char* cstr = s.c_str();  
    std::cout << cstr << std::endl;  
    return 0;  
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值