1:数组中存储的数据也可以是string类型的。代码如下:
// 6.22.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <string> using namespace std; int main(int argc, _TCHAR* argv[]) { string sArrary[5] = {"明日","科技","为","您","服务!!"}; string s=""; //空的string for(int i = 0;i<5;i++) { s+=sArrary[i]; } cout<<s<<endl; return 0; }
运行结果:
上面的程序中,数组中存储了5个string对象,程序的目的是将它们连接起来。string与字符串数组都可以表示一段字符串,但他们有很大的区别:
(1)类别不同
(2)字符串数组需要防范越界、结束符等问题,而string不需要
(3)字符串可以通过地址的形式使用“=”赋值给string,但string不能直接赋值给字符串数组