使用 std::forward_list 返回错误的队列实现
Queue implementation using std::forward_list returning an error
返回此运行时错误
$/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_back
或std::vector::emplace_back
即可。要获取第一个和最后一个元素,请分别使用std::vector::front
和std::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);
}
如何从类成员函数返回迭代器?
与在标准容器中一样,您需要通过提供合适的成员来返回begin
和end
迭代器。例如
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;
一起练习
相关文章:
- boost::进程间消息队列引发错误
- 尝试将lambda函数放在队列中时出现一般分配器错误(可能是与unique_ptr有关的错误)
- 在C++中创建队列 - 什么是 malloc 错误?
- C++数组队列实现方法错误
- 使用MPI_Gather会导致无法附加到引导队列错误
- 使用 <list> (错误 C2760) 打印队列的元素
- 使用 std::forward_list 返回错误的队列实现
- 更改队列指针成员的值需要在 C++ 中出现奇怪的错误
- 获取错误:之前在此处声明的布尔队列的编辑<T>
- C++:自定义数据类型向量错误的队列
- 为什么STL优先级队列错误地分配了我的类对象
- 前缀表示法 C++,分段错误(堆栈和队列)
- 线程安全队列出现分段错误
- 队列验证层错误:队列族索引在 pCreateInfo->pQueueCreateInfos 数组中不是唯一的
- 交换优先级队列时尝试引用已删除的函数错误
- 为什么我的简单C++程序使用队列和 fstream 错误到无效指针
- 尝试编译队列时static_initialization_and_destruction错误
- 队列类的取消排队方法"Aborted (core dumped)"错误
- 在.c文件接收函数中使用Linux中的MSGGET创建消息队列未实现错误
- 尝试使用堆栈创建队列.为什么我会遇到int转换错误的空白