4.4 使用String

Posted: March 25, 2011 in C++

/*使用string来储存字符与数组相比有以下优点:
1.不用估算分配数组的大小
2.快捷复制字符
3.内存会自动被std::string类分配和处理

需要string头文件
语法; string 变量名 (“输入字符”)*/

#include <iostream>
#include <string>

using namespace std;

void main()
{
              string str1 (“Hello “);
              string str2;

              cout<<“string1:”<<str1<<endl;
              cout<<“string1–>copy–>string2”<<endl;
              str2=str1;//复制
              cout<<str2<<endl;

              cout<<endl<<“Enter a new string2:”;
              cin>>str2;

              cout<<“string1 + string2:”<<endl<<str1+str2<<endl;//合并

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s