Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我不确定我做错了什么,但我不断得到一个错误c++;_C++_Algorithm_Deque - Fatal编程技术网

C++ 我不确定我做错了什么,但我不断得到一个错误c++;

C++ 我不确定我做错了什么,但我不断得到一个错误c++;,c++,algorithm,deque,C++,Algorithm,Deque,可能您的测试应该是: #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; //CL

可能您的测试应该是:

#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();
}
   }
  }
在没有看到更多上下文的情况下,我无法准确地说出来,但我非常确定
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:

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

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

cerr以下是初始推送后容器的外观:

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


这当然是一个近似的答案,因为您在索引方面的工作非常混乱。请务必小心并仔细考虑。

错误消息说明了一切……您试图从空字符串(x)中获取第一个字符,这是不可能的。但我将其值初始化为pdp->peek(),它怎么是空的?这只意味着peek()首先返回一个空字符串。@user3339152'但我初始化了…'然后澄清,什么
pdp->peek()
应该是屈服的!它是我创建的PeekDeque类的指针实例我不想发布整个类定义,以后我这样做了,它编译了,但没有返回任何东西这是一个好的开始感谢感谢感谢感谢感谢我将进行此更改我的for循环可能会有问题,因为我的大小工作正常
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