string容器

#include
#include
using namespace std;

int main()
{

char  str[100] = "i  love  yanghe"; //c语言中用字符数组表示字符串
//string类型提供了很多字符串的操作方法   所以还是string类型使用比较方便
string  s1;
s1 = "i  love  china";
string s2 = " i love you ";
s2 = s1;
string s3("i  love wagnle dwdw");

//用c语言的字符数值初始化c++string字符类对象
string s4(str);
cout << s4[0]<< endl;

cout << s1<< endl;
cout << sizeof(s1) << endl;
cout << s1.size() << endl;
cout << s1.length() << endl;

cout << s2 << endl;
cout << sizeof(s2) << endl;
cout << s2.size() << endl;
cout << s2.length() << endl;


cout << s3 << endl;
cout << sizeof(s3) << endl;
cout << s3.size() << endl;
cout << s3.length() << endl;


//s[n] 返回字符串中第n个字符  

//动态分配数组长度  这个是c语言所不具备的
int num = 6;
string s5(num, 'h');
//cout << s5 << endl;
const char *p = s5.c_str();
//s.c_str这个就是返回一个字符串s中的内容指针(指向字符串的常量指针)?????   主要是将c++中字符串转化为c语言中的字符串样式
//cout << s5.c_str()<< endl;
//cout << p<< endl;
char strp[100];
strcpy_s(strp, sizeof(strp), p);
cout << strp << endl;

string s6 = "joiajfoi" + string("fjoajoi");
string s7 = "fjefjoaewi" + s6;
cout << s6 << endl;
cout<< s7 << endl;

//范围for语句使用  就是把字符一个一个打出来(中间用回车分割,类似于装置) 
for( char c:s7)
{	
	cout << c << endl;

}
cout << s7 << endl;


for (char &c : s7)
{

	c = toupper(c);
	cout << c << endl;

}
cout << s7 << endl;
system("pause");
return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值