C++——string容器

//构造函数
#include<iostream>
using namespace std;

//string();
//string(const char* s);
//string(const string& str);
//string(int n,char c);



void test01()
{
	string s1;

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

	string s3(s2);
	cout << "s3 = " << s3 << endl;

	string s4(10, 'a');
	cout << "s4 = " << s4 << endl;


}

int main()
{
	test01();

	system("pause");

	return 0;
}
// 字符串幅值
#include<iostream>
using namespace std;
#include<string>

//string赋值操作
/*
- string& operator=(const char* s) ;   str1   //char*类型字符串赋值给当前的字符串 定义赋值
一string& operator= (const string &s) ; str2  //把字符串s赋给当前的字符串  参数赋值
- string& operator=(char c);            str3      //字符赋值给当前的字符串
- string& assign(const char *s);        str4      //把字 符串s赋给当前的字符串
- string& assign(const char *s, int n);   str5 //把字符串s的前n个字符赋给当前的字符串.
- string& assign(const string &s) ;   str6            //把字符串s赋给当前字符串.
- string& assign(int n, char c) ;    str7              //用n个字符c赋给当前字符串
*/
void test01()
{
	string str1;
	str1 = "hello world";
	cout << "str1 = " << str1 << endl;

	string str2;
	str2 = str1;
	cout <<  "str2 = " << str2 << endl;
		
	string str3;
	str3 = 'a';
	cout << "str3 =" << str3 << endl;

	string str4;
	str4.assign("hello C++");
	cout << "str4 =" << str4 << endl;

	string str5;
	str5.assign("hello C++",4);
	cout << "str5 =" << str5 << endl;

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

	string str7;
	str7.assign(10,'a');                //单引号是字符型'a',双引号是字符串型"aaa"
	cout << "str7 =" << str7 << endl;

}


int main()
{
	test01();

	system("pause");

	return 0;
}```


```cpp
#include<iostream>
using namespace std;
#include<string>
//string字符串拼接

void test01()
{
	string str1 = "我";
	str1 += "爱玩游戏";
	cout << "str1 = " << str1 << endl;

	str1 += ":";
	cout << "str1 = " << str1 << endl;

	string str2;
	str2 = "LOL";
	str1 += str2;
	cout << "str1 = " << str1 << endl;

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

	str3.append(" wangcong ",9);
	cout << "str3 = " << str3 << endl;
	
	str3.append(str1);
	cout << "str3 = " << str3 << endl;

	str3.append(str2,1,2);    //str2的第一个起始位置,截取2个字符
	cout << "str3 = " << str3 << endl;
}

int main()
{
	test01();

	system("pause");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值