C++字符串数组定义
在C++中不仅可以用string定义字符串变量,也可以用string定义字符串数组。
string array[3];
表示定义一个字符串数组,这个数组包含3个字符串元素。
C++字符串数组初始化
string array[3]={{"li"},{"zhang"},{"wang"}}
读者在使用字符串数组时应该注意以下几点:
经典案例:C++实现用字符串数组输出。
#include<iostream>//预处理
#include<string> //引入string
using namespace std;//命名空间
int main()//主函数
{
string array[3]={{"zhangsan"},{"lisi"},{"wangwu"}};//定义字符串数组
for(int i=0;i<3;i++)//for循环
{
cout<<array[i]<<endl;//挨个输出字符串变量的值
}
return 0; //函数返回值为0;
}
执行本程序之后,会输出:
zhangsan
lisi
wangwu
--------------------------------
Process exited after 1.425 seconds with return value 0
请按任意键继续. . .
更多案例可以go公众号:C语言入门到精通
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。