学习C++primer 第24天 之 小问题

jinjunweiruan 2008-07-26 06:54:37
1.
char *cp="Stately plump Buck";
string s;
s.insert(s.size(),cp+7);//s=="Stately plump Buck"
这里的insert从s.size()的地方插入cp+7个元素???怎么会是s=="Stately plump Buck"..


2. string name("AnnaBelle");
string::size_type posl=name.find("Anna");//posl==0
晕。怎么返回0???不是找到了Anna吗??。。。。。。
XX

...全文
212 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
独孤过儿 2008-07-27
  • 打赏
  • 举报
回复
cp指向串首,+7表示向后偏移七位...
jinjunweiruan 2008-07-27
  • 打赏
  • 举报
回复
CP+7
到底指向了什么?????????????????????????????
晕了。
Gary@Tokyo 2008-07-27
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 jinjunweiruan 的回复:]
CP+7
到底指向了什么?????????????????????????????
晕了。
[/Quote]
这个。。。。 可以看手册,自己多思考,做实验
jinjunweiruan 2008-07-26
  • 打赏
  • 举报
回复
今早看了下。。。
呵呵
wangzelong07 2008-07-26
  • 打赏
  • 举报
回复
学习了
K行天下 2008-07-26
  • 打赏
  • 举报
回复
s.insert(s.size(),cp+7);
是在s.size()的位置插入cp+7指向的字符串

后面的是返回下标

楼主还是没有仔细看书
ndsl3334 2008-07-26
  • 打赏
  • 举报
回复
p+7所指向的以空字符结束的字符串副本是" plump Buck"
独孤过儿 2008-07-26
  • 打赏
  • 举报
回复

1、
string& assign ( const string& str );
string& assign ( const string&, size_t pos, size_t n );
string& assign ( const char* s, size_t n );
string& assign ( const char* s );
string& assign ( size_t n, char c );
template <class InputIterator>
string& assign ( InputIterator first, InputIterator last );

Assign content to string

Assigns new content to the string replacing its current content.

The arguments passed to the function determine the new content:


string& assign ( const string& str );
Sets a copy of str as the new content.
string& assign ( const string& str, size_t pos, size_t n );
Sets a copy of a substring of str as the new content. The substring is the portion of str that begins at the character position pos and takes up to n characters (it takes less than n if the end of str is reached before).
string& assign ( const char * s, size_t n );
Sets as the new content a copy of the string formed by the first n characters of the array pointed by s.
string& assign ( const char * s );
Sets a copy of the string formed by the null-terminated character sequence (C string) pointed by s as the new content. The length of the caracter sequence is determined by the first ocurrence of a null character (as determined by traits.length(s)).
string& assign ( size_t n, char c );
Sets a string formed by a repetition of character c, n times, as the new content.
template<class InputIterator> string& assign (InputIterator first, InputIterator last);
If InputIterator is an integral type, behaves as the previous member function version, effectively setting as the new content
a string formed by the repetition first times of the character equivalent to last.
In any other case, the content is set to the values of the elements that go from element referred to by iterator first to the
element right before the one referred to by iterator last.


2、
size_t find ( const string& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;

Find content in string

Searches the string for the content specified in either str, s or c, and returns the position of the first occurrence in the string.

When pos is specified the search only includes characters on or after position pos, ignoring any possible occurrences in previous locations.

Notice that unlike member find_first_of, whenever more than one character is being searched for, it is not enough that only one of these characters match, but the entire sequence of characters to find must be matched.


Parameters
str
string to be searched for in the object. The entire content of str must be matched in some part of the string to be considered a match.
s
Array with a sequence of characters.
In the second member function version, the size of the content to be matched is only determined by parameter n.
In the third version, a null-terminated sequence is expected, and its end is determined by the first occurrence of a null character in it.
n
Length of sequence of characters to search for.
c
Individual character to be searched for.
pos
Position of the first character in the string to be taken into consideration for possible matches. A value of 0 means that the entire string is considered.

Return Value
The position of the first occurrence in the string of the searched content.
If the content is not found, the member value npos is returned.

楼主,我不是给你贴过一个函数手册的链接吗,你倒是下载下来看一下啊。

遇到问题就上来问,我并不觉得这是勤奋的表现,你应该培养自己去解决问题的能力,像这种自己能找到答案的问题,没必要问别人的啊。
ndsl3334 2008-07-26
  • 打赏
  • 举报
回复
s.assign(cp,7)是用cp所指向数组的前7个字符副本替换s
s.assign(cp,7)完了以后s=="Stately"
此时s.size()是7
s.insert(s.size(),cp+7)是在下标为s.size(),也就是7处前("Stately"后面)插入cp+7所指向的以空字符结束的字符串副本
s.insert(s.size(),cp+7)完了以后s=="Stately plump Buck"
ndsl3334 2008-07-26
  • 打赏
  • 举报
回复
s.assign(cp,7)是用cp所指向数组的前7个字符副本替换s
s.assign(cp,7)完了以后s=="Stately"
此时s.size()是7
s.insert(s.size(),cp+7)是在下标为s.size(),也就是7处("Stately"后面)前插入cp+7所指向的以空字符结束的字符串副本
s.insert(s.size(),cp+7)完了以后s=="Stately plump Buck"
chenzhp 2008-07-26
  • 打赏
  • 举报
回复
2. string name("AnnaBelle");
string::size_type posl=name.find("Anna");//posl==0
晕。怎么返回0???不是找到了Anna吗??。。。。。。 //Anna位于整个字符串的起始位置,A处于0号字符啊。所以,返回下标就是0.
XX
jinjunweiruan 2008-07-26
  • 打赏
  • 举报
回复
加上去了,还是看不明白啊。。。
大侠讲清楚点啊 。
ndsl3334 2008-07-26
  • 打赏
  • 举报
回复
1.char *cp="Stately plump Buck";
string s;
s.assign(cp,7) //这里漏了一个
s.insert(s.size(),cp+7);//s=="Stately plump Buck"
ndsl3334 2008-07-26
  • 打赏
  • 举报
回复
2. string name("AnnaBelle");
string::size_type posl=name.find("Anna");//posl==0
返回下标,也就是下标是0,string的第一个元素下标为0
herman~~ 2008-07-26
  • 打赏
  • 举报
回复
呵呵 ...我也来学习
1.下载和安装VC++2008 2.下载和安装VC++2010 3.第1章 快速入门 4.第2章 基本内置类型 5.第2章 重点习题解答 6.第2章 字面值常量 7.第2章 变量 8.第2章 变量名 9.第2章 定义对象 10.第2章 声明和定义 11.第2章 名字的作用域 12.第2章 const 限定符 13.第2章 引用 14.第2章 typedef 15.第2章 枚举 16.第2章 类类型 17.第2章 编写自己的头文件 18.第3章 命名空间的using声明 19.第3章 标准库 string 类型 (1) 20.第3章 标准库 string 类型 (2) 21.第3章重点习题解答 22.第3章标准库string类型(3) 23.第3章标准库vector类型 24.第3章重点习题解答 25.第3章迭代器简介 26.二进制和十六进制 27.第3章标准库bitset类型 28.标准库bitset应用实例 29.第4章数组 30.第4章指针的引入 31.第4章使用指针操作数组元素 32.第4章指针和const限定符 33.第4章C风格字符串 34.第4章创建动态数组 35.第4章新旧代码的兼容 36.第4章多维数 37.第5章算术操作符 38.第5章关系操作符和逻辑操作符 39.第5章位操作符 40.第5章赋值操作符 41.第5章自增和自减操作符 42.第5章箭头操作符 43.第5章条件操作符和逗号操作符 44.第5章new和delete表达式 45.第5章显式转换 46.第6章简单语句 47.第6章if语句 48.第6章switch语句 49.第6章while语 50.第6章for语句 51.第6章dowhile语句 52.第6章break,continue,goto语句 53.第6章6.13try块和异常处理 54.第6章6.13.3标准异常 55.第6章6.14使用预处理器进行调试 56.第7章函数的定义 57.第7章参数传递-非引用形参 58.第7章参数传递-引用参数 59.第7章参数传递-vector和其他容器类型的形参 60.第7章参数传递-数组形参 61.第7章main处理命令行选项 62.第7章return语句 63.第7章递归 64.第7章函数声明 65.第7章局部对象 66.第7章内联函数 67.第7章类的成员函数 68.第7章类的构造函数 69.第7章重载函数 70.第7章重载与作用域 71.第7章重载函数函数匹配 72.第7章重载函数实参转换 73.第7章指向函数的指针 74.第8章面向对象的标准IO库 75.第8章条件状态 76.第8章文件流对象的使用 77.第8章重点习题解答 78.第8章文件模式 79.第8章一个打开并检查输入文件的程序 80.第8章字符串流
1、《The C Programming language》 (Keinighan & Dennis Ritchie 1988) 《 C Primer Pius 》(第五版) Stephen Prata 一本既好又全的C基础讲义 2、《The C++ Programming Languague》特别版(Bjarne Stroustrup) 3、《C++ primer》第三版(Stanley Lippmans) 4、《Inside The C++ Object Model》 (同上) 5、《Effective C++》 (Scott Meyers) 6、《More Effective C++》 (同上) 7、《Exceptional C++》(Herb Sutter) 8、《 C++标准程序库》(Nicolai M.Josuttis)) 9、《Exceptional C++ Style》 (Herb Sutter) 10、《C++面向对象高效编程》(C++ Effective Object-Oriented Software Construction) 11、《面向对象软件构造(Object-Oriented Software Construction)》 12、《设计模式》(Design Patterns) 13、看《Thinking In C++》不要看《C++变成死相》; 14、一定要看《Thinking In C++》第二卷; 15、《 C++ FAQs》 (Marshall Cline, Greg Lomow) 16、《 C++ Templates》 (David Vandevoorde , Nicolai M.Josuttis) 17、读《The Standard C++ Bible》(中文版:标准C++宝典),掌握C++标准; 21、《深入浅出MFC》 (侯捷) 22、《STL 源码剖析》 (侯捷) 23、《高质量程序设计指南——C++/C 》2nd (林锐 韩永泉) 24、《C++ 程序设计教程》2nd(钱能) 25、《The Design and Evolution of C++》(Bjarne Stroustrup) 26、《面向对象编程C++和Java比较教程》(Aviansh C Kak) 27、《Essential COM 》 28、《COM 技术内幕》 29、《软件需求》 30、《Object-Oriented Programming in C++》(Nicolai M.Josuttis) 31、《泛型编程与STL》 Matthew H.Austern 著 侯捷 译 34、看《程序设计实践》,并严格的按照其要求去做;

64,633

社区成员

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

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