开发者

Vector subscript out of range [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago. 开发者_如何学JAVA

I'm getting the vector subscript out of range error. I've had it before, but it prints 'before' but it doesn't print 'after' so I'm confused at to why one of these lines would be causing it.

cout << "before" << endl;
vector<vector<char>> animals;
vector<vector<char>> food;
vector<char> other;
int lastline = 0;
for(int i=1;i<=(c);i++){
cout << "after" << endl;


If c is the count of elements in any vector, then the mistake is simply that in a vector with N items the item indexes are 0...[N-1] and not 1...N.

Therefore, make this correction:

for(int i=0; i < (c); i++) {

By the way, in C-like languages the archetype of a for loop that iterates N times is, not coincidentally:

for(int i = 0; i < N; ++i)

Stick with this unless you have a very very good reason to make an exception, and you get to avoid this type of bug "for free".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜