C++ string容器常用函数大全

目录

string的本质

与c相比

构造函数

赋值操作

         拼接

         查找和替换

         比较

         存取

         插入和删除

         子串截取

 

string的本质

string的本质为一个类  其内部维护了一个char* 指针

与c相比

最后没有/0标志
不能使用printf("%s",str);
可以printf("%s",str.data());

string函数会自动变化内存大小

构造函数

函数原型:

        string();      如 string str;
‌        string (const char*s);   如 初始化字符串
        ‌string(const string& str);   如用string初始化string
        ‌string(int n,char c);

 

#include <iostream>
using namespace std;

int main()
{
	//一
	string s1;
	//二
	const char* s = "hello world";
	string s2(s);
	string s3("hello world");

	//三
	string s4(s2);

	//四
	string s5(3, 'a');


	return 0;
}

 

赋值操作

函数原型:

        string& operator =(const char* s);
‌        string& operator =(const strinng& s);
‌        string& operator =(char c);
        ‌string& assign(const char* s , int n);

                        // n可写可不写,写的话表示把字符串前n个字符赋给string
        ‌string& assign(const string& s);
        ‌string& assign(int n , char c);

 

#include <iostream>
using namespace std;

int main()
{
	//一
	string s1;
	s1 = "hello world";

	//二
	string s2 = s1;

	//三
	string s3;
	s3 = 'a';


	//四
	string s4;
	s4.assign("hello C++");
	
	string s5;
	s5.assign("hello C++", 5);
	
	//五
	string s6;
	s6.assign(s4);

	//六
	string s7;
	s7.assign(4, 'a');
	cout << s7;

	return 0;
}

拼接

函数原型:

        string& operator+=(const char* str);
‌        string& operator+=(const char c);
        ‌string& operator+=(const string& str);

        ‌string& append(const char* s);
        ‌string& append(const char* s , int n);
        ‌string& append(const string& s);
        ‌string& append(const string& s , int pos , int n);
        //从pos位置下标开始的n个字符拼接到尾部

 

#include <iostream>
using namespace std;

int main()
{

	//一
	const char* a = "hello world";
	string s1 = "hello C++ ";
	s1 += a;

	string s2 = "hello C++ ";
	s2 += "hello world";

	//二
	const char b = 'a';
	string s3 = "hello C++ ";
	s3 += b;

	//三
	string s1_ = "hello C++ ";
	string s4 = "hello world";
	s1 += s4;

	//四
	string s5 = "hello C++";
	s5.append("hello world");

	const char* c = "hello world";
	string s5_ = "hello world";
	s5_.append(c);

	//五
	const char* d = "hello world";
	string s6 = "hello C++ ";
	s6.append(d, 5);

	//六
	string s7 = "hello C++";
	string s7_ = "hello world";
	s7.append(s7_);

	//七
	string s8 = "hello C++";
	string s8_ = "hello world";

	s8.append(s8_, 0, 5);

	return 0;
}

 

查找和替换

函数原型:

        查找第一次出现位置
        ‌Int find(const string& str , int pos = 0) const;
          //返回第一个字符下标,pos(下标)代表从哪开始(包括这个位置)

‌        查找最后一次出现的位置

Int

rfind(const string& str , int pos = 0) const;

        ‌替换
        ‌string& replace (int pos , int n , const string& str);

#include <iostream>
using namespace std;

int main()
{
	//一
	string s1 = "hello world world";
	int ret = s1.find("world");
//	ret=6
	//二
	ret = s1.rfind("world");
//ret=12;
	//三
	string s2 = "hello C++";
	s2.replace(6, 3, "world");
//s2="hello world";

	return 0;
}

比较

函数原型:

        int compare (const string& s) const;
‌        int compare(const char* s) const;

也可以用比较运算符 返回类型为bool

 

比较方式:按照ASC码比较
                  若遇到不等字符 则停止比较
= 返回 0    //最大用途   判断二者是否相等
> 返回 1
< 返回 -1
 

#include <iostream>
using namespace std;

int main()
{
	string s1 = "hello world";
	string s2 = "hello worke";
	string s3 = "hello world";

	if (s1.compare(s3) == 0)//条件成立
	{
		cout << "s1等于s3" << endl;
	}
	cout << s2.compare(s1);//结果为-1 说明s2大
    //这里可以看出s2-s1 若s2大于s1则返回1 若二者相等则返回0 否则返回-1

	return 0;
}

 

字符串的存取

函数原型:

        char& operator[] (int n);
        ‌char& at(int n);

#include <iostream>
using namespace std;

int main()
{
	string s1 = "hello world";
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << endl;
	}
	
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1.at(i);
	}
	return 0;
}

 

子串的截取

函数原型:

string substr(int pos = 0 , int n = npos) const;
从pos开始截取,截取npos个

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zmzzz666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值