学习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

...全文
215 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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
  • 打赏
  • 举报
回复
呵呵 ...我也来学习

65,189

社区成员

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

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