[英]Why this code keeps crashing
Why this code gives error: 为什么此代码会出错:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
char x='\0',i=0;
vector<char> text;
do
{
cout<< "Enter a char" << endl;
cin>>x;
if(x<65||(x>90&&x<97)||x>123)
{
cout<<"Out of Range-Please Enter Letters"<<endl;
continue;
}
text.push_back(x);
if(x=='n'||x=='N'){
text.clear();
cout<<"Vector Cleared"<<endl;
continue;
}
cout<<text.size()<<endl;
cout<<"Vector now holds\n";
for(i=0;i< signed (text.size() ) ;i++)
{
cout<< text[i] << endl;
}
}while(x!='y');
cout << "Vector size is " << text.size() << " Vector Capacity is " << text.capacity() << endl;
vector<char>::iterator it = text.begin();
for (; it != text.end() ; it++)
cout << *it << endl;
cout << "Enter Position to delete: " << endl;
cin >> i;
text.erase(text.begin() + i - 1);
it = text.begin() ;
for (; it != text.end() ; it++)
cout << *it << endl;
}
Debug assertion failure, expression vector iterator + offset out of range. 调试断言失败,表达式向量迭代器+偏移超出范围。
It will crash because i
is a character, therefore cin
will read a character. 它会崩溃因为i
是一个角色,所以cin
会读一个角色。 For example if someone enters a value 8
, the value of i
will be 38 (if you platform is using ASCII encoding). 例如,如果有人输入值8
,则i
的值将为38(如果您的平台使用ASCII编码)。
Plus testing characters for their values is very bad practice use isalpha()
from cctype
header instead. 加上测试字符的值是非常糟糕的做法使用isalpha()
来自cctype
头。
Part of your problem is you are using char
as an array index: 部分问题是您使用char
作为数组索引:
cout<< text[i] << endl;
You also should consider prefix (not postfix) operators for non-primitive types to improve your performance at these lines: 您还应该考虑非基本类型的前缀(而不是后缀)运算符,以提高您在以下行中的性能:
for (; it != text.end() ; it++)
and 和
for (; it != text.end() ; it++)
Suggestions: 建议:
std::string
instead of std::vector<char>
. 使用std::string
而不是std::vector<char>
。 isprint, isdigit, isalpha,
etc.) 首选库函数而不是比较字符范围(参见isprint, isdigit, isalpha,
等) tolower
or toupper
. 使用tolower
或toupper
。 I converted your example to use std::string
, character functions and added space around operators. 我将您的示例转换为使用std::string
,字符函数以及在运算符周围添加了空格。 Compare to your style for simplicity and ease of reading: 与您的风格相比,简洁易读:
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char x='\0';
unsigned int i=0;
string text;
do
{
cout<< "Enter a char" << endl;
cin>>x;
// if(x<65||(x>90&&x<97)||x>123)
if (! isprint(x))
{
cout<<"Out of Range-Please Enter Letters"<<endl;
continue;
}
text += x;
if(toupper(x) == 'N')
{
text.clear();
cout<<"String Cleared"<<endl;
continue;
}
cout << text.length() << endl;
cout<<"String now holds\n";
cout << text << endl;
}while(tolower(x) != 'y');
cout << "String length is: " << text.length() << ", capacity: " << text.capacity() << endl;
cout << text << endl;
cout << "Enter Position to delete: " << endl;
cin >> i;
text.erase(text.begin() + i - 1);
cout << "After erase: " << text << endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.