ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

标准库类型string

2021-12-11 09:58:21  阅读:136  来源: 互联网

标签:const string int char 标准 字符串 类型 include


        标准库类型string表示可变长的字符序列,string本身是类,与容器操作相似,使用string类型必须首先包含#include<string>头文件。

       1、string 构造函数

                string s1;                                           string默认构造

                const char* str="hello world";        c风格字符串

                string s2(str);                                    将char*字符串拷贝给s2

                string s3(s2);                                   拷贝构造函数

                string s4(n,'value');                          将n个value赋s4

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1;
	const char* str = "hello world";
	string s2(str);
	string s3(s2);
	string s4(10, 'a');
	cout << "str : " << str << endl;
	cout << "s2 : " << s2 << endl;
	cout << "s3 : " << s3 << endl;
	cout << "s4 : " << s4 << endl;
	system("pause");
	return 0;
}

 

        2、string 容器赋值操作

                string& operator=(const char*s);                 char*类型字符串 赋值给当前的字符串

                string& operator=(const string &s);             把字符串s赋给当前的字符串

                string& operator=(char c);                            把字符赋给当前字符串

                string& assign(const char*s);                       把字符串s赋给当前的字符串

                string& assign(const char*s,int n);               把字符串s前n个字符串赋给当前的字符串

                string& assign(const string&s);                    把字符串s赋给当前字符串

                string& assign(int n ,char c);                        用n个字符串c赋给当前字符串

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1;
	s1 = "hello world";
	string s2;
	s2 = s1;
	string s3;
	s3 = 'c';
	string s4;
	s4.assign("hello world");
	string s5;
	s5.assign("hello world", 5);
	string s6;
	s6.assign(s2);
	string s7;
	s7.assign(5, 'c');
	cout << "s1 : " << s1 << endl;
	cout << "s2 : " << s2 << endl;
	cout << "s3 : " << s3 << endl;
	cout << "s4 : " << s4 << endl;
	cout << "s5 : " << s5 << endl;
	cout << "s6 : " << s6 << endl;
	cout << "s7 : " << s7 << endl;
	system("pause");
	return 0;
}

 

4、string 容器插入和删除

        insert(pos,"value");                                       在pos位置插入value值     

        erase(beg,end);                                              清除[beg,end]区间的字符,若只给beg一个值默认从beg开始全部清除;

        clear();                                                             清空容器

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1= "hello world";
	cout << "s1 : " << s1 << endl;
	s1.insert(3, "value");
	cout << "插入后s1 : " << s1 << endl;
	s1.erase(2, 4);
	cout << "指定区间s1 : " << s1 << endl;
	s1.erase(s1.begin()+2,s1.end()-4);
	cout << "利用迭代器指定区间s1 : " << s1 << endl;
	s1.erase(2);
	cout << "只给beg值s1 : " << s1 << endl;
	s1.clear();
	cout << "请空s1 : " << s1 << endl;
	system("pause");
	return 0;
}

 

  5、string容器拼接

        string& operator+=(const char*str);                重载+=操作符

        string& operator+=(const char c); 

        string& operator+=(const string& str); 

        append(const char* s);                             把字符串s连接到当前字符串尾部

        append(const char*s,int n);                      把字符串前n个字符连接到当前字符串尾部

        append(const string &s);                          把字符串s连接到当前字符串尾部

        append(const string &s,int pos,int n);     把字符串s从pos开始n个字符连接到当前字符串尾部

        

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1= "hello ";
	s1 += "world";
	cout << "s1 : " << s1 << endl;
	s1 += '!';
	cout << "s1 : " << s1 << endl;
	string s2="!6!6!6!";
	s1 += s2;
	cout << "s1 : " << s1 << endl;
	s1.append("yes");
	cout << "s1 : " << s1 << endl;
	s1.append("hhhhhhhhh", 2);
	cout << "s1 : " << s1 << endl;
	s1.append(s2);
	cout << "s1 : " << s1 << endl;
	s1.append(s2, 2, 2);
	system("pause");
	return 0;
}

 

  

6、字符串查找和替换

        find();                        从左至右

        rfind();                       从右至左

        replace(n,m,"字符串“);        从n号位置起m个字符替换成字符串

        

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1= "hello world";
	char c = s1.find(2);
	cout << "c : " << c << endl;
	char b = s1.rfind(2);
	cout << "b : " << b << endl;
	s1.replace(2, 2, "value");
	cout << "s1 : " << s1 << endl;
	system("pause");
	return 0;
}

 

7、字符串比较

        str1.compare(str2);                返回值    0相等,-1小于  ,1大于

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1= "hello world";
	string s2 = "helle w";
	int a;
	a = s1.compare(s2);
	cout << "a = " << a << endl;
	system("pause");
	return 0;
}

 

 

标签:const,string,int,char,标准,字符串,类型,include
来源: https://blog.csdn.net/weixin_61198922/article/details/121868529

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有