C++ 字符串 10-- 18.28~30.string型字符串的查空empty、交换swap、类型转换c_str

80 篇文章 0 订阅
#include <iostream>
#include <string>
using namespace std;
/*---------------------------------
     18-28 18.28.判断string型字符串是否为空
	 18.29.swap_交换两个字符串的内容
	 18.30.将string型字符串转换为char型字符串
---------------------------------*/
int main()
{
	cout<<"-------C++类 empty()-----------:"<<endl;
	string str("");
	if(str.empty()) //字符串查空
	{  cout<<"字符串为空!"<<endl;}
	else
		cout<<str<<endl;


	cout<<"-------C类 swab()-----------:"<<endl;
	char ch1[15]="ofur";
	char ch2[15]="";
	swab(ch1,ch2,4); //以ch1的长度交换奇偶位字符后,拷贝到ch2
	cout<<ch1<<endl<<ch2<<endl;


	cout<<"-------C++类 swap()-----------:"<<endl;
	string str1="ofur";
	string str2="";
	str1.swap(str2); //交换两个字符串
	cout<<str1<<endl<<str2<<endl;


	cout<<"-------C++类 c_str()-----------:"<<endl;
	str1="hello world";
	const char *ch;
	ch=str1.c_str(); //将string型字符串转换为char型字符串
	cout<<ch<<endl;


	return 0;
}

运行结果:

-------C++类 empty()-----------:
字符串为空!
-------C类 swab()-----------:
ofur
foru
-------C++类 swap()-----------:


ofur
-------C++类 c_str()-----------:
hello world
Press any key to continue

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值