使用 std::copy 插入 STL 队列
我想使用 std::copy 将元素插入到队列中,如下所示:
vector<int> v;
v.push_back( 1 );
v.push_back( 2 );
queue<int> q;
copy( v.begin(), v.end(), insert_iterator< queue<int> >( q, q.front() ) );
但这无法编译,抱怨 begin
不是 的成员std::queue
。
注意:我也用 std::inserter
尝试过 - 这也失败了,这次说“reference”不是“std::queue”的成员。 std::back_inserter
和 std::back_insert_iterator
也因相同的错误而失败。
我是否遗漏了一些明显的东西,或者 insert_iterator
是否不适用于队列?
I'd like to use std::copy
to insert elements into a queue like this:
vector<int> v;
v.push_back( 1 );
v.push_back( 2 );
queue<int> q;
copy( v.begin(), v.end(), insert_iterator< queue<int> >( q, q.front() ) );
But this fails to compile, complaining that begin
is not a member of std::queue
.
Note: I tried it with std::inserter
too - this also failed, this time saying that 'reference' is not a member of 'std::queue'. std::back_inserter
and std::back_insert_iterator
also fail with the same error.
Am I missing something obvious, or do insert_iterator
s just not work with queues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
不幸的是,
std::queue
将名为push_back
的函数“调整”为push
,这意味着标准back_insert_iterator
不起作用。可能最简单的方法(尽管概念上很难看)是使用一个短暂的容器适配器适配器[原文如此](呃!)来调整容器适配器,该适配器的寿命与后插入迭代器的寿命一样长。
像这样使用:
Unfortunately
std::queue
'adapts' the function known aspush_back
to justpush
which means that the standardback_insert_iterator
doesn't work.Probably the simplest way (albeit conceptually ugly) is to adapt the container adapter with a short lived container adapter adapter[sic] (eugh!) that lives as long as the back insert iterator.
Used like this:
队列不允许迭代其元素。
来自 SGI STL 文档:
您可以完成这项工作,但您不能使用
insert_iterator
。您必须编写类似queue_inserter
的内容来呈现迭代器接口。更新 我无法控制自己,决定尝试实现您需要的迭代器。结果如下:
这对于这样的函数非常有用:
但它不适用于 STL 副本,因为 STL 很愚蠢。
Queue does not allow iteration through its elements.
From the SGI STL Docs:
You can make this work, but you can't use
insert_iterator
. You'll have to write something likequeue_inserter
that presents an iterator interface.Update I couldn't help myself and deicded to try to implement the iterator you need. Here are the results:
This works great for functions like this:
But it doesn't work with the STL copy because the STL is stupid.
std::queue
不是 STL 意义上的容器,它是一个功能非常有限的容器适配器。对于您似乎需要的std::vector
或std::deque
(“双端队列,这是一个“真正的容器”),似乎是正确的选择。std::queue
isn't a container in the STL sense, it's a container adapter with very limited functionality. For what you seem to need eitherstd::vector
orstd::deque
("double-ended queue, which is a "real container"), seems the right choice.我很确定它不会工作 - 队列提供
push
,但插入迭代器期望使用push_front
或push_back
。没有真正的理由你不能编写自己的push_insert_iterator
(或任何你喜欢的名称),但这有点痛苦......I'm pretty sure it just won't work -- a queue provides
push
, but an insert iterator expects to usepush_front
orpush_back
. There's no real reason you couldn't write your ownpush_insert_iterator
(or whatever name you prefer) but it is a bit of a pain...insert_iterator
和back_insert_iterator
仅适用于具有(分别)insert
和push_back
方法的容器(或适配器) -queue
没有这些。您可以以这些为模型编写自己的迭代器,如下所示:除非这样的东西已经存在,但我很确定它不存在。
insert_iterator
andback_insert_iterator
only work on containers (or adaptors) with (respectively)insert
andpush_back
methods -queue
doesn't have these. You could write your own iterator modelled on these, something like this:Unless such a thing already exists, but I'm pretty sure it doesn't.
std::queue
不是 STL 中的基本容器之一。它是一个容器适配器,使用基本 STL 容器之一构建(在本例中是顺序容器之一std::vector
std::deque
或std::list
)。它是专为 FIFO 行为而设计的,并且不会在您希望insert_iterator
工作的给定迭代器处提供随机插入。因此不可能像这样使用队列。我能想到的最简单的方法是:
并像这样使用它:
std::queue
is not one of the basic containers in STL. It is a container adaptor which is built using one of the basic STL containers ( in this case one of the sequential container eitherstd::vector
std::deque
orstd::list
). It is designed specifically for FIFO behaviour and does not provide random insertion at the given iterator which you want for theinsert_iterator
to work. Hence it will not be possible to use queue like this.The easiest way I could think of to do this is to:
And use it like:
您需要的是一个
push_inserter
(即执行push
到队列中的插入器)。据我所知,STL中没有这样的迭代器。我通常所做的就是遗憾地回到旧的 for 循环。如果你有勇气,你可以推出你自己的迭代器,大致如下:
这只是一个草稿,但你已经明白了。使用
push
方法(例如queue
、stack
)与任何容器(或者容器适配器)一起使用。What you need is a
push_inserter
(i.e. an inserter that performspush
es into the queue). As far as I know, there is no such iterator in the STL. What I usually do is sadly fall back to the good old for loop.If you have the courage, you can roll your own iterator, something along these lines:
This is just a draft but you got the idea. Works with any container (or, well, container adapters) with a
push
method (e.g.queue
,stack
).对于 c++11
和 c++14
for c++11
and c++14
在这个简单的例子中,您可以这样写:
这将复制
向量
并将其用作队列
的底层容器。当然,如果您需要在构建队列后将事物放入队列,则这种方法将不起作用。
In this simple case, you can write:
This will make a copy of the
vector
and use it as the underlying container of thequeue
.Of course, this approach won't work if you need to enqueue things after the queue has been constructed.