admin管理员组文章数量:1193971
Using container parameter for queue
I'm just curious about how the container part of the queue class works in C++. Like in this function
vector<int> foo(){
queue<int, vector<int>> myQ;
for(int i =0; i < 10; i++){
myQ.push(i);
}
return myQ;
}
this wont compile but I don't see why not. From my understanding the vector is thing holding the underlying values of the queue so why cant it be casted or something? /
Using container parameter for queue
I'm just curious about how the container part of the queue class works in C++. Like in this function
vector<int> foo(){
queue<int, vector<int>> myQ;
for(int i =0; i < 10; i++){
myQ.push(i);
}
return myQ;
}
this wont compile but I don't see why not. From my understanding the vector is thing holding the underlying values of the queue so why cant it be casted or something? https://cplusplus.com/reference/queue/queue/
Share Improve this question edited Jan 24 at 5:35 3CxEZiVlQ 38.3k10 gold badges73 silver badges89 bronze badges asked Jan 24 at 5:35 Aidan AhramAidan Ahram 511 silver badge4 bronze badges 4- What is the problem you need to solve using queues and vectors? Why can't you use a vector to begin with? – Some programmer dude Commented Jan 24 at 6:05
- 3 There is no inheritance, a queue is NOT a vector (it uses a vector internally) and there is no accessor to get to that vector (by design). You either have a queue or a vector, which is good because strong typing will safeguard your designs in code. – Pepijn Kramer Commented Jan 24 at 6:52
- 4 While it is sometimes perceived as judgmental, I suspect there is an XY problem at work here. Why do you want to do this? – Chris Commented Jan 24 at 6:53
- why do you use a Queue, When you want to return a Vecot?R – Raildex Commented Jan 24 at 8:39
3 Answers
Reset to default 4A std::queue<int, std::vector<int>>
has a protected
member variable, c
, of type std::vector<int>
, but typically, the classes in the standard library do not provide conversion operators to the type(s) of their member variables. If you need such conversions, you've probably selected the wrong container class to begin with.
Since the container, c
, is protected
, you could however derive your own class from std::queue<int, std::vector<int>>
to be able to extract the member container when needed:
template <class T, class Container = std::deque<T>>
class myqueue : public std::queue<T, Container> {
public:
using std::queue<T, Container>::queue;
using std::queue<T, Container>::operator=;
explicit operator const Container&() const& { return this->c; }
explicit operator Container() && { return std::move(this->c); }
};
std::vector<int> foo() {
myqueue<int, std::vector<int>> myQ;
for(int i =0; i < 10; i++){
myQ.push(i);
}
// display the values in the queue using the first conversion operator:
for(int v : static_cast<const std::vector<int>&>(myQ)) {
std::cout << ' ' << v;
}
std::cout << '\n';
// return a vector<int> using the second conversion operator:
return static_cast<std::vector<int>>(std::move(myQ));
}
Note: MSVC selects the wrong ref-qualified operator
overload in the second call, so you'll get a copy instead of a move in VS2022. I've reported the issue in this ticket.
queue#defn - No. Container c
is a protected member and is not accessible from outside.
A queue
has specific behaviors that the user depends upon. Exposing the underlying container would allow behaviors that are not part of the definition of a queue.
For example, you can't insert something in the middle of a queue. Exposing the vector
in your question would allow insertion anywhere. The behavior of the queue is no longer predictable.
Consider if you are debugging a problem because some value is introduced in the middle of the queue. Finding where this is done could be a significant headache.
本文标签: cIf I create a Queue using a vector as a container can I return it as a vectorStack Overflow
版权声明:本文标题:c++ - If I create a Queue using a vector as a container can I return it as a vector? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738445886a2087210.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论