C++字符串数组的赋值

心刖留痕 2008-12-05 09:02:03
怎样定义一个字符串数组,然后动态的给每个赋值,并出去每一个字符串,
希望大家能帮忙解决一下,我刚学C++,最好是能写个能够实现的小程序 多谢了
...全文
3436 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
hexcdd 2010-11-12
  • 打赏
  • 举报
回复
看懂了点...
shengxiwgzly 2010-04-18
  • 打赏
  • 举报
回复
打发干啥的
星羽 2008-12-06
  • 打赏
  • 举报
回复
vector<string>
nullah 2008-12-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lc19890326 的回复:]
既然是学C++ 就少有C风格的字符串吧

C/C++ code
#include <iostream>
#include <string>
using namespace std;

int main()
{
string a[2];
for(int i=0;i<2;++i)
{
cin>>a[i];
cout<<a[i]<<endl;
}
return 0;
}
[/Quote]
up
心刖留痕 2008-12-06
  • 打赏
  • 举报
回复
想不到同一个问题有这么多种是实现方法啊,学习了,多谢各位了
cba_v 2008-12-06
  • 打赏
  • 举报
回复
up
VCRWX 2008-12-06
  • 打赏
  • 举报
回复
string a[10];
a[0]="abcde";
a[2]="abced"
````````````
注意string和C中的字符数组的区别
不过还是建议你用vector<string>比较好
luojc714 2008-12-06
  • 打赏
  • 举报
回复
用标准模板库(STL)中的vector加string实现是一种好的办法...
kaidiligent0 2008-12-06
  • 打赏
  • 举报
回复
学习了.
hongzao 2008-12-06
  • 打赏
  • 举报
回复
用vector<T>加string,呵呵也就是楼上的vector<string>
winingsky 2008-12-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lc19890326 的回复:]
既然是学C++ 就少有C风格的字符串吧

C/C++ code
#include <iostream>
#include <string>
using namespace std;

int main()
{
string a[2];
for(int i=0;i<2;++i)
{
cin>>a[i];
cout<<a[i]<<endl;
}
return 0;
}
顶!
[/Quote]
qq675927952 2008-12-05
  • 打赏
  • 举报
回复
4L 的代码好像要问题吧,
lizhaohu 2008-12-05
  • 打赏
  • 举报
回复
string a[2];
for(int i=0;i<2;++i)
{
cin>>a[i];
// cout<<a[i]<<endl;
}
cout<<a<<endl;
smdszgzh 2008-12-05
  • 打赏
  • 举报
回复
用指针数组啊!

#include <iostream>
using namespace std;
void main()
{
char *a[5];
for(int i(0);i<sizeof(a)/sizeof(char *);i++)
cin>>a[i];
cout<<a[i]<<endl;
}
Crazying111 2008-12-05
  • 打赏
  • 举报
回复
我觉得C很基础



很多时候用C更能体现算法设计思想

#include <iostream>
#include <string>
using namespace std;

int main()
{
int i;
char ** a=new []char *; //动态申请
for(int i=0;i<2;++i)
{
a[i]=new char *;
cin>>a[i];
cout<<a[i]<<endl;
}
return 0;
}

lq651659889 2008-12-05
  • 打赏
  • 举报
回复
难道还有别的方法.关注.....
lc19890326 2008-12-05
  • 打赏
  • 举报
回复
少用~
lc19890326 2008-12-05
  • 打赏
  • 举报
回复
既然是学C++ 就少有C风格的字符串吧

#include <iostream>
#include <string>
using namespace std;

int main()
{
string a[2];
for(int i=0;i<2;++i)
{
cin>>a[i];
cout<<a[i]<<endl;
}
return 0;
}

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧