Skip to content

[SNIPPETS-C++] Updated "Vector To Queue" #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions snippets/cpp/data-structure-conversion/vector-to-queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ title: Vector to Queue
description: Convert vector into queue quickly
tags: data structures,queue,vector
author: mrityunjay2003
contributors: majvax
---

```cpp
#include<queue>
#include<vector>
#include<deque>

std::queue<int> vectorToQueue(const std::vector<int>& v) {
return std::queue<int>(std::deque<int>(v.begin(), v.end()));
template <typename T>
std::queue<T> vectorToQueue(const std::vector<T>& v) {
return std::queue<T>(std::deque<T>(v.begin(), v.end()));
}



// Usage:
std::vector<int> vec = { 1, 2, 3, 4, 5 };
vectorToQueue(&vec); // Returns: std::queue<int> { 1, 2, 3, 4, 5 }
vectorToQueue(vec); // Returns: std::queue<int> { 1, 2, 3, 4, 5 }
```
Loading