C++:STL(补充:string容器)

目录

string构造函数

string赋值

string字符串拼接

string查找和替换

string字符存取

string插入和删除

string求字串

string字符串的反转


string构造函数

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

//string的构造函数
void test01()
{
	string s1;//默认构造  string();

	const char* str = "hello world";
	string s2(str);//string(const char* s);
	cout << "s2 = " << s2 << endl;

	string s3(s2);//拷贝构造
	cout << "s3 = " << s3 << endl;

	string s4(10, 'a');//string(int n,char c);
	cout << "s4 = " << endl;
}
int main()
{
	test01();
	return 0;
}


string赋值

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

//string的赋值操作
void test01()
{
	string str1;
	str1 = "hello world";//可以直接=字符串
	cout << "str1 = " << str1 << endl;

	string str2;
	str2 = str1;
	cout << str2 << endl;

	string str3;
	str3 = 'a';//可以直接等于字符

	string str4;
	str4.assign("hello C++");//把字符串赋给当前字符串
	cout << "str4 = " << str4 << endl;

	string str5;
	str5.assign("hello C++", 5);//将字符串的前5个字符赋给str5
	cout << "str5 = " << str5 << endl;

	string str6;
	str6.assign(str5);
	cout << "str6 = " << str6 << endl;

	string str7;
	str7.assign("w", 7);
	cout << "str7 = " << str7 << endl;
}
int main()
{
	test01();
	return 0;
}

string字符串拼接

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

//string的拼接操作
void test01()
{
	string str = "我";
	string str1 = str + "爱玩游戏";
	cout <<str1 << endl;

	string str2 = str1 + ':';//可以是字符
	cout << str2  << endl;

	string str3 = str2+"LOL DNF CF";//可以是字符串
	cout << str3 << endl;

	string str4 = "I ";
	str4.append("love ");
	cout << str4 << endl;

	str4.append("game abcde", 4);//将字符串的前4个字符连接到字符串的结尾
	cout << str4 << endl;

	str4.append(str2);//可以直接追加字符
	cout << str4 << endl;

	str4.append("CFEDFE", 0, 2);//第二个参数是从那个位置开始,第三个参数是截取几个字符
	cout << str4 << endl;


}
int main()
{
	test01();
	return 0;
}

string查找和替换

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

//string的查找和替换
void test01()
{
	
	string str1 = "abcdefasdde";
	int pos = str1.find("de");//能找到输出找的第一个元素在原字符串的下标
	if (pos == -1)//找不到输出-1
	{
		cout << "未找到字符串" << endl;
	}
	else {
		cout << "找到字符串,pos = " << pos << endl;
	}
	//rfind
	pos = str1.rfind("de");
	if (pos == -1)//找不到输出-1
	{
		cout << "未找到字符串" << endl;
	}
	else {
		cout << "找到字符串,pos = " << pos << endl;
	}
	//rfind 和find 的区别
	//find从左往右查找,rfind是从右往左查找

}
void test02()
{
	string str1 = "abcdefassde";
	str1.replace(1, 3, "11111");//从第一个位置起的3个字符替换为"11111"
	cout << str1 << endl;


}
int main()
{
	test02();
	return 0;
}

string字符串比较

字符串比较是按字符串的ASCII码来进行比较,比较的时候先比较第一个,如果第一个字符一样,那就比较第二个字符.........

=返回0

>返回1

<返回-1

#include<string>
#include<iostream>
using namespace std;
//string的字符串比较//
void test01()
{
	string str1 = "xello";
	string str2 = "hello";

	if (str1.compare(str2) == 0)
	{
		cout << "str1 = str2" << endl;
	}
	else if (str1.compare(str2) > 0)
	{
		cout << "str1 > str2" << endl;
	}
	else {
		cout << "str1 < str2" << endl;
	}
}
int main()
{
	test01();
	return 0;
}

string字符存取

1.用[]方式来取字符

2.用at方式来获取字符

#include<string>
#include<iostream>
using namespace std;
//string的字符串比较//
void test01()
{
	string str1 = "hello";
	cout << "str1" << endl;
	
	//1.通过[]来访问单个字符
	for (int i = 0; i < str1.size(); i++)
	{
		cout << str1[i] <<" ";
	}
	cout << endl;

	//2.通过at来访问
	for (int i = 0; i < str1.size(); i++)
	{
		cout << str1.at(i) << " ";
	}
	cout << endl;

	//修改单个字符
	str1[0] = 'x';
	cout << str1 << endl;
}
int main()
{
	test01();
	return 0;
}

string插入和删除

#include<string>
#include<iostream>
using namespace std;
//string的字符串比较//
void test01()
{
	string str1 = "hello";
	cout << "str1" << endl;
	
	str1.insert(1, "111");
	cout << str1<<endl;

	//删除
	str1.erase(1, 3);//第一个参数是第几个位置,第二个参数是删除几个字符
	cout << "str1 = " << str1 << endl;


}
int main()
{
	test01();
	return 0;
}

string求字串

#include<string>
#include<iostream>
using namespace std;
//string的字符串比较//
void test01()
{
	string str1 = "hello";
	cout << "str1="<<str1 << endl;
	
	string subStr = str1.substr(1, 3);//1表示位置,3表示截取几个字符
	cout << "subStr = " << subStr << endl;


}
int main()
{
	test01();
	return 0;
}

string字符串的反转


#include <iostream>
#include <algorithm>
#include <string>
 
using namespace std;
int main()
{
    string str = "hello world";
    reverse(str.begin(), str.end());
 
    cout << str;       //"dlrow olleh"
    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值