我不确定我做错了什么,但我不断收到错误 c++

I am not sure what I am doing wrong but I keep getting an error c++

本文关键字:c++ 错误 不确定 错了 什么      更新时间:2023-10-16
for (int i = peekIndex; i < pdp->size(); i++)
{
    string x = pdp->peek();
    if (x.at(0) == 's')
    {
       out << pdp->peek() << endl;
       pdp->moveForward();  
    }
    else
    {
       pdp->moveForward();
    }
}

我得到的错误是

抛出和std::out_of_range
实例后终止调用 what(): basic_string::at()

流产

peek 方法返回位于peekIndex位置的字符串。moveFowrard方法递增peekIndexpdp是大小为 100 的vector。我应该只偷看和打印以"s"开头的单词,这些单词已被推到<vector>.我基本上完成了,但这部分证明有些困难。 谢谢

#include<iostream>
#include<vector>
#include<string>

using namespace std;
class StringDeque {
protected:
vector<string>* elements;
int frontItem;   //CLASS INV: indexes item with least index
int rearSpace;   //CLASS INV: indexes space after item with greatest index
int upperBound;  //For array[0..n-1] this is "n" not "n-1".

public:
StringDeque(int guaranteedCapacity):
elements (new vector<string>( 2*guaranteedCapacity))
frontItem (guaranteedCapacity),
rearSpace ( guaranteedCapacity),
upperBound ( 2*guaranteedCapacity)
{}
proteted:
virtual bool isEmpty() const { return frontItem == rearSpace; }
virtual bool  isFull() const { return rearSpace == upperBound || frontItem == 0; }
virtual int size() const { return rearSpace - frontItem; }
virtual string popRear() {
if (isEmpty()) {
cerr<< "Later we'll define and throw an EmptyQException"<< endl;
return "";
} else {
return elements->at(--rearSpace);
}
}
virtual string popFront() {
if (isEmpty()) {
cerr<<"Later we'll define and throw an EmptyQException"<<endl;
return "";
} else {
return elements->at(frontItem++);
}
}
/** Directions include similarly testing for "full" in the C++ code.
*/
virtual void pushFront(string newItem) {
elements->at(--frontItem)= newItem;
}
virtual void pushRear(string newItem) {
elements->at(rearSpace++) = newItem;
}

virtual string toString() {
string out = "";
for (int i = frontItem; i < rearSpace; i++) {
out += elements->at(i) + " ";
}
  return out;
}
};


  class PeekDeque : public StringDeque {
  private:
  int peekIndex;

  public:
  PeekDeque(int guaranteedCapacity):
  StringDeque(guaranteedCapacity),
  peekIndex(guaranteedCapacity/2)
  {}

  virtual void moveFrontward() {
  if (peekIndex == upperBound) {
  cerr<<"Cannot move past total capacity"<<endl;
  }     else{
      elements->at(peekIndex ++);
           }
   }
  virtual void moveRearward () {
  if (peekIndex == -1) {
    cerr<<"Cannot move below total capacity"<<endl;
  }     else{
     elements ->at( peekIndex--);
           }
    }

    virtual string popFront() {
     cerr<<"Attempt to pop from empty PeekDeque"<<endl;
     }
    virtual string popRear() {
    cerr<<"Attempt to pop from empty PeekDeque"<<endl;
     }
virtual string peek() {
  if (isEmpty()) {
     cerr<<"Cannot peek an Empty index"<<endl;
     return "";
  } else {
     return elements->at(peekIndex + 1);
  }
  }


  virtual string toString() {
  string out = "";
  for (int i = frontItem; i < rearSpace; i++) {
     out += elements->at(i) + " ";
  }
  return out;
  }
  };
  int main(){
  PeekDeque* pdp = new PeekDeque(101);
  pdp->pushFront("oh");
  pdp->pushFront("say");
  pdp->pushFront("can");
  pdp->pushFront("you");
  pdp->pushFront("see");
  pdp->pushRear("any");
  pdp->pushRear("bad bugs");
  pdp->pushRear("on");
  pdp->pushRear("me?");
  for(int i = peekIndex; i<pdp->size(); i++){
    string x =
   if(x.at(0)=='s'){
  cout<<pdp->peek()<<endl;
  pdp->moveForward();  }
     else{
  pdp->moveForward();
}
   }
  }

可能是你的测试应该是:

    if(!x.empty() && x.at(0)=='s')

如果没有看到更多的背景,我无法准确判断,但我很确定x.empty()这是一个可能的情况。

更新:

pdp 是大小为 100 的向量

您确定已使用 pdp.resize(100,std::string()); 方法来确保正确初始化所有仓位吗?

它不是空的,我已经将 8 件事推送到 PDP

此外,std::vector<>::resize()std::vector<>::push_back()可能无法按预期协同工作。使用 std::vector<>::push_back()std::vector<>::resize() 为其提供预分配的大小并按索引操作条目。始终检查索引访问的std::vector<>::size()

更新 2:

但真正的答案显然更简单。你有:

virtual string peek() {
  if (isEmpty()) {
     cerr<<"Cannot peek an Empty index"<<endl;
     return ""; // This will certainly result in a string that is empty!
以下是

初始推送后容器的外观:

0                       202
| ... | ... | ... | ... |
      ^     ^     ^
      |     |     rearSpace
      |     peekIndex
      frontItem

现在,size()返回rearSpace - frontItem,但迭代从peekIndex开始,因此超出了索引rearSpace,窥视未初始化(空)的字符串。

这当然是一个近似的答案,因为你使用索引的工作是一团糟。请非常小心并仔细考虑。