我在main中声明了一个优先级队列,并且在通过if语句满足某些条件时,我使用emplace将一个对象添加到队列中。不幸的是,在if语句退出时,队列似乎是空的。我检查了我的队列是否根本没有添加对象,看来在范围内,对象在队列中。我将粘贴一些我在这里使用的代码。对不起,如果这是一个愚蠢的问题,或者我使用这个网站是错误的,我知道这里的规则有多严格,但我在网站和其他地方搜索了答案。
int main()
{
int control= 0;
while (control!= -1)
{
int menue_control = 0;
priority_queue<object> neg;
priority_queue<object> pos;
priority_queue<double> test;
cout << "\n pos Empty? " << pos.empty();//1
cout << "\n negEmpty? " << neg.empty();//1
cout << "\n testEmpty? " << test.empty();//1
//after I run though the if statements once, these return to be empty
std::cout << "\nmenu stuff: Quit\n\n";
std::cin >> menu;
if (menu == 1)
{
std::cout << "You have selected first Menu option:\n ";
std::cout << "\nSubmenu:Enter 1 to do OT1 or enter 2 for OT2 ";
int OT;
std::cin >> OT;
string name;
int num;
double val;
if (OT== 1)
{
std::cout << "name: ";
std::cin >> name;
std::cout << "Enter number : ";
std::cin >> num;
std::cout << "Enter 1 for thing, enter 2 for other thing: ";
int aaa;
std::cin >> aaa;
if (aaa== 1)
{
std::cout << "Enter val:";
std::cin >> val;
pos.emplace(name, num, val);
test.emplace(val);
cout << "\n posEmpty? " << pos.empty();//0
cout << "\n neg Empty? " << neg.empty();//1
cout << "\n test Empty? " << test.empty();//0
int difference;
if (!pos.empty() && !neg.empty()) {
diff= neg.top() - pos.top();
if (diff> 0)
{
neg.emplace(name, diff, val);
}
if (diff< 0)
{
pos.emplace(name, -1 * diff, val);
}
else
{
std::cout << "done:\n";
}
}
}
if (aaa== 2)
{
}
}
if (OT== 2)
{
std::cout << "Enter name: ";
std::cin >> name;
std::cout << "Enter number ";
std::cin >> num;
std::cout << "Enter 1 for thing, enter 2 for other thing: ";
int aaa;
std::cin >> aaa;
if (aaa== 1)
{
std::cout << "Etner selling price: ";
std::cin >> val;
neg.emplace(name, num, val);
test.emplace(val);
cout << "\n pos Empty? " << pos.empty();//1
cout << "\n neg Empty? " << neg.empty();//0
cout << "\n tes Empty? " << tes.empty();//0
if (!pos.empty() && !neg.empty()) {
int diff = pos.top() - neg.top();
std::cout << "GOT HERE NOW!";
if (difference > 0)
{
neg.emplace(name, diff, val);
}
if (diff< 0)
{
pos.emplace(name, -1 * diff, val);
}
else
{
std::cout << "order completely filled:\n";
}
}
}
if (aaa== 2)
{
//do stuff
}
}
}
//A bunch of if menu statements here that are empty
if (menu == 6) { control= -1; }
}
return 0;
}
当while循环结束时,优先级队列超出范围。在他看来,你无法获得它的价值。
我很惊讶它为你编译,它不会在这里。你应该用-Wall编译它,并摆脱所有的错误和警告。这会显示您的错误。