关于out of range的问题(帮我看一下代码)

LBWANDWC 2016-10-11 09:17:44
以下代码运行时显示超出界限,不知道为什么?请问该如何修改?
#include<iostream>
#include<string>
#include<cstring>
using namespace std;

void copy(string & n);

int main()
{
cout<<"Enter a string(q to quit):"<<endl;
string str;
getline(cin,str);
while(str!="q")
{
copy(str);
cout<<str<<endl<<"Enter a string(q to quit):"<<endl;
getline(cin,str);
}
return 0;
}
void copy(string & n)
{
int i=0;
while(n[i])
n[i]=toupper(n[i++]);
}

...全文
1161 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
LBWANDWC 2016-10-11
  • 打赏
  • 举报
回复
可是我getline输入了一个换行符啊?
paschen 版主 2016-10-11
  • 打赏
  • 举报
回复
string字符串不以\0结尾 改成:

	for (i = 0; i < n.size(); ++i)
		n[i]=toupper(n[i]);
赵4老师 2016-10-11
  • 打赏
  • 举报
回复
string字符串不以0结尾也不以'\n'结尾。
LBWANDWC 2016-10-11
  • 打赏
  • 举报
回复
那么我如果修改为 void copy(string & n) { int i=0; while (n[i]!='\n') { n[i]=toupper(n[i]); ++i; } } 为什么行不通呢?
HymanLiuTS 2016-10-11
  • 打赏
  • 举报
回复
string字符串不是已'\0'结尾的,他本质是一个类对象,不同于c中的字符串,要遍历它可以用它的length作为判断条件
LBWANDWC 2016-10-11
  • 打赏
  • 举报
回复
谢谢你们的回答,但是string字符串不是以空字符结尾吗?当while读取到空字符的时候难道不会跳出循环?
AlbertS 2016-10-11
  • 打赏
  • 举报
回复
赞同2楼的做法,貌似string类型的字符串结尾没有'\0'
Sniper_Pan 2016-10-11
  • 打赏
  • 举报
回复
同一楼,另外,这代码要稍微修改下,尽量避免在同一行代码中同时进行修改和调用i,因为无法预期编译器会先做哪一步(等式左右,如先做右边,会影响左边下标位置)
void copy(string & n)
{
	int i = 0;
	while (i < n.length())
	{
		n[i] = toupper(n[i]);
		++i;
	}
}
begodliker 2016-10-11
  • 打赏
  • 举报
回复
当i超过string的长度时,while(n[i])会越界
reskai 2016-10-11
  • 打赏
  • 举报
回复
引用 9 楼 LBWANDWC 的回复:
可是我getline输入了一个换行符啊?

getline自动读出一行内容,但是不包含换行
赵4老师 2016-10-11
  • 打赏
  • 举报
回复
Reference <string> basic_string getline function template <string> std::getline (basic_string)(1) template <class charT, class traits, class Alloc> basic_istream<charT,traits>& getline (basic_istream<charT,traits>& is, basic_string<charT,traits,Alloc>& str, charT delim); (2) template <class charT, class traits, class Alloc> basic_istream<charT,traits>& getline (basic_istream<charT,traits>& is, basic_string<charT,traits,Alloc>& str); (1) template <class charT, class traits, class Alloc> basic_istream<charT,traits>& getline (basic_istream<charT,traits>& is, basic_string<charT,traits,Alloc>& str, charT delim); template <class charT, class traits, class Alloc> basic_istream<charT,traits>& getline (basic_istream<charT,traits>&& is, basic_string<charT,traits,Alloc>& str, charT delim); (2) template <class charT, class traits, class Alloc> basic_istream<charT,traits>& getline (basic_istream<charT,traits>& is, basic_string<charT,traits,Alloc>& str); template <class charT, class traits, class Alloc> basic_istream<charT,traits>& getline (basic_istream<charT,traits>&& is, basic_string<charT,traits,Alloc>& str); Get line from stream into string Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, for (2)). The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation. If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it. Each extracted character is appended to the basic_string as if its member push_back was called. Parameters is basic_istream object from which characters are extracted. str basic_string object where the extracted line is stored. Return Value The same as parameter is. A call to this function may set any of the internal state flags of is if: flag error eofbit The end of the source of characters is reached during its operations. failbit The input obtained could not be interpreted as a valid textual representation of an object of this type. In this case, distr preserves the parameters and internal data it had before the call. Notice that some eofbit cases will also set failbit. badbit An error other than the above happened. (see ios_base::iostate for more info on these) Additionally, in any of these cases, if the appropriate flag has been set with is's member function basic_ios::exceptions, an exception of type ios_base::failure is thrown. Example 1234567891011121314 // extract to string #include <iostream> #include <string> main () { std::string name; std::cout << "Please, enter your full name: "; std::getline (std::cin,name); std::cout << "Hello, " << name << "!\n"; return 0; } Complexity Unspecified, but generally linear in the resulting length of str. Iterator validity Any iterators, pointers and references related to str may be invalidated. Data races Both objects, is and str, are modified. Exception safety Basic guarantee: if an exception is thrown, both is and str end up in a valid state. See also basic_istream::getlineGet line (public member function )operator>> (basic_string)Extract string from stream (function template )

64,637

社区成员

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

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