I am working with a vector of around 400 float members and for my assignment I need to copy the vector into a queue. I never really understood the purpose of the queue and that has not made it easy. Here is what the function that I currently have....
template<typename T>
void vector_to_queue(std::queue<T> q, std::vector<T> v){
for(vector<T>::iterator it = v.begin(); it != v.end(); it ){
q.push();
}
}
This is as far as I've gotten, I am completely stumped and would appreciate any help that can be provided.
CodePudding user response:
You are pushing nothing to the queue, this should work for you:
template<typename T>
void vector_to_queue(std::queue<T>& q, const std::vector<T>& v){
for(vector<T>::iterator it = v.begin(); it != v.end(); it ){
q.push(*it);
}
}
CodePudding user response:
template<typename T>
void vector_to_queue(std::queue<T> &q, const std::vector<T> &v) {
for (const auto &elem : v) {
q.emplace(elem);
}
}
Or little fancy
#include <algorithm>
template<typename T>
void vector_to_queue(std::queue<T> &q, const std::vector<T> &v) {
std::for_each(v.begin(), v.end(), [&q](const auto &elem){ q.emplace(elem); });
}