我正在学习c ++并在执行以下代码时获取了垃圾值。我检查了算法部分,这似乎是正确的,但是如果出错,请告诉我。我已经使用了诸如add,add或insert元素之类的功能,del函数删除,空函数清空队列,最后我有了front函数,其工作是返回front值。
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
class Queue {
private :
vector<char> data_;
int front_, rear_;
public :
Queue(): front_(-1),rear_(-1) { data_.resize(20); }
~Queue() {};
int empty() {
if (front_==rear_) {
front_ = rear_ = -1 ;
return 1;
}
else
return 0;
}
void add(char x) {
if (front_ == -1)
front_ = 0 ;
rear_=rear_+1;
data_[rear_]=x;
}
void del( ) {front_=front_+1;}
char front() { return front_ ; }
} ; // End of the class
int main( ) {
Queue q;
char str[20];
cin >> str;
for (int i = 0; i < strlen(str); ++i)
q.add(str[i]);
while (!q.empty()) {
cout << q.front();
q.del();
}
return 0;
}