菜鸟问题,求助!

laokaizzz 2013-11-05 11:57:15
本人菜鸟,遇到一个问题,请高手指教,感谢。

下面是c++ primer的习题的一段代码,其中有一段不是很理解,或者说理解错误。见红色字体。
int main()
{
vector<string> svec;
string str;
// 输入vector 元素
cout << "Enter strings:(Ctrl+Z to end)" << endl;
while (cin >> str)
svec.push_back(str);
// 创建字符指针数组
char **parr = new char*[svec.size()];
// 处理vector 元素
size_t ix = 0;
for (vector<string>::iterator iter = svec.begin();
iter != svec.end(); ++iter, ++ix) {
// 创建字符数组
char *p = new char[(*iter).size()+1]; 个人理解是创建了一个指针,指向一个内存块。// 复制vector 元素的数据到字符数组
strcpy(p, (*iter).c_str()); 个人理解是把(*iter).c_str() 的内容复制到 了p所指向的内存块中。而不是复制给p所在的内存地址中。// 将指向该字符数组的指针插入到字符指针数组
parr[ix] = p;
}
// 输出vector 对象的内容
cout << "Content of vector:" << endl;
for (vector<string>::iterator iter2 = svec.begin();iter2 != svec.end(); ++iter2)
cout << *iter2 << endl;
// 输出字符数组的内容
cout << "Content of character arrays:" << endl;
for (ix =0; ix != svec.size(); ++ix)
cout << parr[ix] << endl; 疑问,按照我的理解,这里parr[ix]应该为指针,要取里面的值,应该是*parr[ix],但是实际却不是,而是直接parr[ix],所以很费解,大神指导下
// 释放各个字符数组
for (ix =0; ix != svec.size(); ++ix)
delete [] parr[ix];
// 释放字符指针数组
delete [] parr;
return 0;
}
...全文
483 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lunat 2013-11-06
  • 打赏
  • 举报
回复
有人希望手下人不需要思考,完全靠他自己事无巨细的指挥,他选择用C; 有人希望手下都是帅才,自己统治全局,明白细节,却无需关注细节,他选择了C++。
赵4老师 2013-11-06
  • 打赏
  • 举报
回复
引用 10 楼 laokaizzz 的回复:
[quote=引用 9 楼 zhao4zhong1 的回复:] 摒弃cout,使用printf
针对这个,我去百度了一下,了解了他们的不同点。一输出的话,c++里用cout貌似影响不大。 不过我的问题不是这个。[/quote] cout很聪明或自作聪明,输出你预料中或无法预料的结果; printf很笨,让它干啥就干啥,总是输出你预料中的结果。
laokaizzz 2013-11-06
  • 打赏
  • 举报
回复
引用 9 楼 zhao4zhong1 的回复:
摒弃cout,使用printf
针对这个,我去百度了一下,了解了他们的不同点。一输出的话,c++里用cout貌似影响不大。 不过我的问题不是这个。
赵4老师 2013-11-06
  • 打赏
  • 举报
回复
摒弃cout,使用printf
laokaizzz 2013-11-06
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试是程序员必须掌握的技能之一。
感谢你的回复,一般问题,我自己都会调试的,有的问题,确实不理解,或者百度上搜不到,才会上来问的。 就算再牛逼的人不可能也没有请教过别人的时候吧。 我不是要求别人帮我看代码功能,我说的是其中有几句我比较疑惑,说的是看红字部分。你估计误解了我的意思,你可以看一下五楼的回复,我只是提个疑问,并不是伸手要东西。谢谢。
SKATE11 2013-11-05
  • 打赏
  • 举报
回复
楼主理解正确 可能是翻译有误
max_min_ 2013-11-05
  • 打赏
  • 举报
回复
引用 2 楼 laokaizzz 的回复:
[quote=引用 1 楼 lunat 的回复:] 像strcpy这种库函数的疑问,可以问谷歌: http://www.cplusplus.com/reference/cstring/strcpy/ function <cstring> strcpy char * strcpy ( char * destination, const char * source ); Copy string Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point). To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source. Parameters destination Pointer to the destination array where the content is to be copied. source C string to be copied. Return Value destination is returned. 第二个cout直接输出char *,即输出NULL结尾的字符串;*parr[ix]的类型是char,即单个字符;题目中要输出的是字符串不是字符。
感谢,我主要的疑问就是 char *p = new char[(*iter).size()+1]; strcpy(p, (*iter).c_str()); 这里面的p指向的是 一个字符,为什么直接输出 p就可以得到全部字符串呢?你看iterater 也算指针,他就只能通过*p 来输出。 [/quote] p能取到字符串是因为p指向了首地址,通过这个首地址来查找到内容的! iterater是迭代器,不能简单的说它是指针!不要相提并论
laokaizzz 2013-11-05
  • 打赏
  • 举报
回复
引用 1 楼 lunat 的回复:
像strcpy这种库函数的疑问,可以问谷歌: http://www.cplusplus.com/reference/cstring/strcpy/ function <cstring> strcpy char * strcpy ( char * destination, const char * source ); Copy string Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point). To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source. Parameters destination Pointer to the destination array where the content is to be copied. source C string to be copied. Return Value destination is returned. 第二个cout直接输出char *,即输出NULL结尾的字符串;*parr[ix]的类型是char,即单个字符;题目中要输出的是字符串不是字符。
感谢,我主要的疑问就是 char *p = new char[(*iter).size()+1]; strcpy(p, (*iter).c_str()); 这里面的p指向的是 一个字符,为什么直接输出 p就可以得到全部字符串呢?你看iterater 也算指针,他就只能通过*p 来输出。
lunat 2013-11-05
  • 打赏
  • 举报
回复
像strcpy这种库函数的疑问,可以问谷歌: http://www.cplusplus.com/reference/cstring/strcpy/ function <cstring> strcpy char * strcpy ( char * destination, const char * source ); Copy string Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point). To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source. Parameters destination Pointer to the destination array where the content is to be copied. source C string to be copied. Return Value destination is returned. 第二个cout直接输出char *,即输出NULL结尾的字符串;*parr[ix]的类型是char,即单个字符;题目中要输出的是字符串不是字符。
赵4老师 2013-11-05
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试是程序员必须掌握的技能之一。
laokaizzz 2013-11-05
  • 打赏
  • 举报
回复
引用 4 楼 SKATE11 的回复:
楼主理解正确 可能是翻译有误
谢谢,可是给出的源码 是正确的。没错
laokaizzz 2013-11-05
  • 打赏
  • 举报
回复
引用 3 楼 max_min_ 的回复:
[quote=引用 2 楼 laokaizzz 的回复:] [quote=引用 1 楼 lunat 的回复:] 第二个cout直接输出char *,即输出NULL结尾的字符串;*parr[ix]的类型是char,即单个字符;题目中要输出的是字符串不是字符。
感谢,我主要的疑问就是 char *p = new char[(*iter).size()+1]; strcpy(p, (*iter).c_str()); 这里面的p指向的是 一个字符,为什么直接输出 p就可以得到全部字符串呢?你看iterater 也算指针,他就只能通过*p 来输出。 [/quote] p能取到字符串是因为p指向了首地址,通过这个首地址来查找到内容的! iterater是迭代器,不能简单的说它是指针!不要相提并论[/quote] 感谢版主,我测试了一下果然是这样的,还是有点疑问。 string s("test"); char *p=&(s[0]); cout<<p<<endl; 如果指针指的是某个字符串的首地址,则直接取该指针则是取字符串所有内容。这是什么原理呢? 一般指针是这样的: string s("test"); string *p=&s; cout<<p<<endl; 指针只是保存某个对象的地址,输出的时候也是输出该地址的值。 为什么上面那个存的是字符串的首字符的地址,为什么直接取不是输出那个字符的地址,而是输出的整个字符串呢,是有相关的定义或者规定么,我这边c++primer书上我找不到。

64,643

社区成员

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

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