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

时间:2014-02-21 22:24:49

标签: c++ algorithm deque

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();
}
   }
  }

2 个答案:

答案 0 :(得分:1)

可能是你的考试应该是:

    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!

答案 1 :(得分:0)

以下是初次推送后容器的样子:

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

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

这肯定是一个近似的答案,因为你对指数的工作是一团糟。请小心谨慎,仔细考虑。

相关问题