C++基础 string类型

string 字符型 可以保存字符串

C++使用string 需要引用

#include “<string>”

举栗子

实现俩个输出字符串 比大小 小的去前面

// ConsoleApplication12.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include "stdlib.h"
#include <string>

using namespace std;

 
 

int _tmain(int argc, _TCHAR* argv[])
{
	
	string str1,str2,temp;
	cout<<"input strings:";
	cin>>str1>>str2;
	//int j = str1.compare(str2);
	if( str1>str2 )
	{
		temp = str1;
		str1=str2;
		str2 = temp;
	}
	cout<< "now,they are:";
	cout<<str1<<","<<str2<<endl;
 
	system("pause");
	return 0;
}
 

在string里 也是数组形式保存  

 	 string str1;
	 cout<<"input strings:";
	 cin>>str1;
	 cout<<str1[0]<<" "<<str1[1]<<" "<<str1[2]<<endl;

输出代码 当然 想修改 直接 str1[2] ='a';即可

string 还有很多方便的函数

发布了279 篇原创文章 · 获赞 43 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/q465162770/article/details/103459317