2014-02-21 2 views
-1
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(); 
    } 
} 

가 계속 내가 오류가나는 내가 뭘 잘못 확실하지 않다 그러나 나는 오류 C++


what(): basic_string::at()

중단

std::out_of_range의 던지고 예를 다음에 호출 종료입니다

peek 메서드는 01 위치에 문자열을 반환합니다.. moveFowrard 메서드는 peekIndex을 증가시킵니다. pdp은 크기가 100 인 vector입니다. <vector>에 푸시 된 's'로 시작하는 단어 만 엿보고 인쇄합니다. 나는 기본적으로 끝났지 만이 부분은 다소 어려움을 증명합니다. 좀 더 맥락을 보지 않고, 정확히 말할 수 없다

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

,하지만 난 x.empty()가 가능한 경우가 꽤 확신 : 감사

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

오류 메시지에 모두 표시됨 ...불가능한 빈 문자열 (x)에서 첫 번째 문자를 얻으려고합니다. –

+0

하지만 그 값으로 pdp-> peek() 초기화 된 어떻게 비어 있습니까 ?? –

+0

pdp는 무엇입니까? –

답변

0

여기에 컨테이너 초기 푸시 후처럼 보이는 방법은 다음과 같습니다

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

이제 size() 반환 rearSpace - frontItem하지만 peekIndex에서 반복 시작되고, 따라서 rearSpace 인덱스를 넘어이 초기화되지 않은 엿 (비어 있음) 문자열.

색인을 사용한 작업이 엉망이므로 대략적인 대답입니다. 정말로 조심스럽게 생각해보십시오.

+0

덕분에 내 크기가 잘 작동 이후 내 루프에 대한 문제가 될 수 있도록 변경됩니다 –

1

는 테스트가해야 할 수 있습니다.

UPDATE :

pdp 크기의 벡터 (100)

가 제대로 초기화되는 모든 위치를 확인하기 위해 pdp.resize(100,std::string()); 방법을 사용했다 하시겠습니까입니까?

내가 예상대로

또한

std::vector<>::resize()std::vector<>::push_back() 함께 작동하지 않을 수 있습니다 PDP 8 일을 추진해 왔습니다 비어 있지 않습니다. 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

나는 이것을 컴파일했지만 아무것도 좋은 시작 감사를 반환하지 않았다 –

+0

@ user3339152 내 업데이 트를 참조하십시오 ... –

+0

!!!! 그것은 그럴 수 있습니다 크기를 확인해 드리겠습니다 –

관련 문제