使用 std::forward_list 返回错误的队列实现

Queue implementation using std::forward_list returning an error

本文关键字:错误 队列 返回 实现 list std forward 使用      更新时间:2023-10-16

返回此运行时错误

$/usr/bin/ld: QueueusingSLL: _ZSt4cout: invalid version 2 (max 0)
$/usr/bin/ld: QueueusingSLL: error adding symbols: bad value 
collect2: error: ld returned 1 exit status

另外,如何从类成员函数返回迭代器? 请参阅代码中的auto iterator()函数。

源代码

#include<iostream>     // std::cout
#include<forward_list> // std::singly linked list
#include<iterator>     //// std::iterator,next(is a random iterator)
//#include <algorithm> // std::for_each loop
using namespace std;
class Queue
{
private:
forward_list<int> list;
public:
Queue()
{}
Queue(int firstElem) {
enqueue(firstElem);
}
//Return the size of Queue
int size()
{
forward_list<int>::iterator first = list.begin();
forward_list<int>::iterator last = list.end();
return std::distance(first, last);
}
//Return whether or not the queue us empty
void isEmpty()
{
std::cout << "Queue " << (list.empty() ? "is empty" : "is not empty") << std::endl;
}
// Peek the top of the stack without removing an element
// Returns 0 if queue is empty.
int peek()
{
if (list.empty() == 1)
return 0;
return list.front();
}
//Add an element at back(end or tail) of queue:enqueue
void enqueue(int Elem)
{
auto pos = list.begin();
while (std::next(pos) != list.end()) ++pos;
list.insert_after(pos, Elem);
}
//Remove an element from front of queue:dequeue
int dequeue()
{
if (list.empty() == 1) return 0;
list.pop_front();
return 1; //1 means success
}
void display()
{
cout << endl << "Elements in Queue x";
for (forward_list<int>::iterator i = list.begin(); i != list.end(); i++)
{
cout << endl << "t" << *i;
}
cout << endl << endl;
}
/*auto iterator()
{
return list.iterator();
}*/
};

int main()
{
Queue Q1, Q2(5);
Q1.isEmpty();
Q2.isEmpty();
cout << endl << "sizeofQ1   " << Q1.size() << endl << "sizeofQ2     " << Q2.size() << endl << endl;
Q1.enqueue(10);
Q1.enqueue(20);
Q1.enqueue(30);
Q1.enqueue(40);
Q1.enqueue(50);
Q1.enqueue(60);
Q1.display();
Q2.display();
cout << endl << "sizeofQ1   " << Q1.size() << endl << endl;
Q1.dequeue();
Q1.dequeue();
Q1.display();
cout << endl << "sizeofQ1   " << Q1.size() << endl << endl;
return 0;
}

首先,如果你想在容器端插入,最好使用std::vector,而不是std::forward_list。然后只需按照要求使用std::vector::push_backstd::vector::emplace_back即可。要获取第一个和最后一个元素,请分别使用std::vector::frontstd::vector::back


对于您的问题:您的enqueue函数无法处理list空案例。你需要

void enqueue(int Elem)
{
if (list.empty())  // for the empty list case
{
list.push_front(Elem);
return; // done here
}
auto pos = list.begin();
while (std::next(pos) != list.end())
++pos;
list.insert_after(pos, Elem);
}

如何从类成员函数返回迭代器?

与在标准容器中一样,您需要通过提供合适的成员来返回beginend迭代器。例如

auto begin() const noexcept {
return list.begin();
}
auto end() const noexcept {
return list.end();
}

但是,只有从 c++14 开始才能返回auto。在 c++11 中,需要显式提及返回类型。感谢尾随回报,您可以通过它

auto begin() const noexcept -> decltype(list.begin()) // trailing return
{
return list.begin();
}
auto end() const noexcept -> decltype(list.end()) // trailing return
{
return list.end();
}

作为旁注,请不要与using namespace std;一起练习