1

I have vector

 std::vector<OrderInfo *> vec

and a queue

queue<OrderInfo *> *myQueue = new queue<OrderInfo *>;

I want to copy the vector into the queue. I tried using How can I copy an entire vector into a queue? this answer and also this Insert into an STL queue using std::copy

but it's not working, how do I make it work?

this is what I tried: myQueue = new queue(vec.begin(), vec.end()); i got

error: no matching function for call to ‘std::queue::queue(std::vector::iterator, std::vector::iterator)’ myQueue = new queue(vec.begin(), vec.end());

and when I tried this:

std::copy(vec.begin(),vec.end(),std::back_inserter(myQueue));

i got:

required from ‘BacStrategy::BacStrategy(EZXConnectionHandler&, const string&, bool, const double&, int) [with Event_Type = EZXOrderEventHandler; std::__cxx11::string = std::__cxx11::basic_string]’ /home/yaodav/Desktop/git_repo/test/main.cpp:324:51: required from here /usr/local/include/c++/7.4.0/bits/stl_iterator.h:490:7: error: ‘std::queue*’ is not a class, struct, or union type operator=(const typename _Container::value_type& __value)

yaodav
  • 1,126
  • 12
  • 34

1 Answers1

5

myQueue is a pointer, not a queue, and can’t be passed to std::back_inserter. To fix this, don’t declare it as a pointer.

Furthermore, std::back_inserter can’t be used with a std::queue, as the second link you posted explains.

Instead, simply write

std::queue<OrderInfo*> myQueue{
    std::deque<OrderInfo*>(vec.begin(), vec.end())
};

If you really need a pointer, adapt the code as follows:

std::queue<OrderInfo*>* myQueue = new std::queue<OrderInfo*>{
    std::deque<OrderInfo*>(vec.begin(), vec.end())
};

Lastly, if you need to fill an already initialised queue, proceed as follows: create a temporary queue using the above and assign it to your pointer:

*myQueue = std::queue<OrderInfo*>{std::deque<OrderInfo*>(vec.begin(), vec.end())};

If this looks too messy you can also create a temporary variable for that queue — but in that case you need to use std::move to ensure that the queue gets move-assigned, not expensively copied:

auto tmp = std::queue<OrderInfo*>{std::deque<OrderInfo*>(vec.begin(), vec.end())};
*myQueue = std::move(tmp);

In the same vein, consider carefully whether you want to store OrderInfos rather than pointers to OrderInfos.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • and now Im :getting /usr/local/include/c++/7.4.0/bits/stl_iterator.h:492:13: error: ‘class std::queue’ has no member named ‘push_back’ container->push_back(__value); ~~~~~~~~~~~^~~~~~~~~ – yaodav Jan 23 '20 at 13:02
  • @yaodav Yes, as is explained in the link you’ve posted. My bad. The updated code works now. – Konrad Rudolph Jan 23 '20 at 13:08
  • thank you very much but I need it to be a ptr because the queue is a data member and the vector comes later on. so il just do it in the old fashion way (loop) – yaodav Jan 23 '20 at 13:47
  • @yaodav I’m not sure I understand this: data members don’t need to be pointers! That said, I’ll update my answer. No loops needed. – Konrad Rudolph Jan 23 '20 at 13:49
  • what I mean is - from what I know is that if I have a none ptr data member then its init in the constructor, then I cant use your method. but if I use ptr (and I'm not considering the other aspect of ptr vs value) I can create the queue when the vector coming – yaodav Jan 23 '20 at 13:56
  • You don't need to name `std::deque`, just [use more `{}`](http://coliru.stacked-crooked.com/a/1f28cf0acaa104de) – Caleth Jan 23 '20 at 14:39
  • @Caleth I’m not sure whether I like this. I think I’d rather be explicit in this case, especially since brace initialisers for containers usually invoke the collection initialiser (which accepts `std::initializer_list`), but we want to call a different constructor. Too easy to accidentally call the wrong constructor here. – Konrad Rudolph Jan 23 '20 at 16:20
  • @KonradRudolph so why not `std::queue::containter_type(vec.begin(), vec.end())`? A `std::queue` isn't a container, it has one as a member. – Caleth Jan 23 '20 at 16:22
  • @Caleth Because there’s obviously a balance to be struck between explicitness and conciseness, and I believe that my code strikes this balance. That said, I don’t hate this conceptually, it’s just too verbose (but that could be avoided with a typedef). – Konrad Rudolph Jan 23 '20 at 16:26