关于“遍历动态数组”的问题。

return0x0 2008-04-19 02:40:13
编写程序从标准输入设备读入字符串,并把该数组存放在字符数组中。


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

int main()
{
string word;
cout<<endl<<"请输入字符串: ";
cin>>word;

const int len=strlen(word.c_str());
char *ic=new char[len]; //这里是否应该为[len+1]?
char *pic=ic; //创建指向指针的指针
for (string::iterator i=word.begin();i!=word.end();++i,++pic)
*pic=*i; //将字符串中的元素置入动态数组

for(char *p=ic; p!= ??? ;++p) //遍历动态数组
cout<<*p<<endl; //输出数组中的元素
delete [] ic;

system("pause");
}
...全文
149 10 打赏 收藏 举报
写回复
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
forckgcs 2008-04-19
  • 打赏
  • 举报
回复

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

int main()
{
string word;
cout<<endl<<"请输入字符串: ";
cin>>word;

const int len=strlen(word.c_str());
char *ic=new char[len];
char *pic=ic;
for (string::iterator i=word.begin();i!=word.end();++i,++pic)
*pic=*i;
for(char *p=ic;p!=ic+len;++p) //这样处理 lz满意吗?
cout<<*p<<endl;
delete [] ic;

system("pause");
return 0;
}
nuaacj8888 2008-04-19
  • 打赏
  • 举报
回复
对于以下两句,我查了下msdn
const int len=strlen(word.c_str());
char *ic=new char[len]; //这里是否应该为[len+1]?
msdn对于c_str()解释如下

const E *c_str() const;
The member function returns a pointer to a nonmodifiable C string constructed by adding a terminating null element (E(0)) to the controlled sequence. Calling any non-const member function for *this can invalidate the pointer.

也就是len已经是包含结束符了,所以第二句不用写成char[len+1].
不知道我的说法对不对?
waxl0118 2008-04-19
  • 打赏
  • 举报
回复
路过 学习
xjy1204 2008-04-19
  • 打赏
  • 举报
回复
才看明白楼主原来出的是一道填空题
5楼已经给出答案了

PS:char *ic=new char[len]; //这里不需要是len+1,因为他根本只是存储了所有的字符,并将其打印出来,不需要增加字符串的结尾
xjy1204 2008-04-19
  • 打赏
  • 举报
回复
如果你的字符串漏了结尾的话
就很可能导致内存访问越界了

*pic='\0'; //加上这一条也就是给字符串加个结束符,使之成为一个完整的字符串
effective_person 2008-04-19
  • 打赏
  • 举报
回复

for(char *p=ic; p!= pic ;++p) //遍历动态数组 pic可以实现啊 这么简单啦!pic到字符数组的末尾了。
cout <<*p <<endl;
return0x0 2008-04-19
  • 打赏
  • 举报
回复
2楼的意思看明白了

for(char *p=ic; p!= ??? ;++p)
不知道这里的 ???,还有没有其他的办法?
return0x0 2008-04-19
  • 打赏
  • 举报
回复
*pic='\0'; //字符串结尾。

for(char *p=ic; *p!='\0';++p)

这两个 "\0" 好像没有什么关系啊?

而且循环的时候找到的不一定是动态数组中的‘\0’啊,也可能是数组之外的 第一个'\0'。对吗?
Leejun527 2008-04-19
  • 打赏
  • 举报
回复

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

int main()
{
string word;
cout<<endl<<"请输入字符串: ";
cin>>word;

const int len=strlen(word.c_str());
char *ic=new char[len+1]; //长度[len+1]
char *pic=ic;
for (string::iterator i=word.begin();i!=word.end();++i,++pic)
*pic=*i;
*pic='\0'; //字符串结尾。
for(char *p=ic; *p!='\0';++p)
cout<<*p<<endl;
delete [] ic;

system("pause");
return 0;
}
return0x0 2008-04-19
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main()
{
string word;
cout<<endl<<"请输入字符串: ";
cin>>word;

const int len=strlen(word.c_str());
char *ic=new char[len]; //这里是否应该为[len+1]?
char *pic=ic;
for (string::iterator i=word.begin();i!=word.end();++i,++pic)
*pic=*i;

for(char *p=ic; p!= ??? ;++p) //遍历动态数组
cout<<*p<<endl;
delete [] ic;

system("pause");
}
相关推荐
发帖
C++ 语言

6.3w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
帖子事件
创建了帖子
2008-04-19 02:40
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下