hello, how can i get the first five elements form an queue with out using for loop ....
A:
Why would you want to do that? Maybe you have to hold 5 elements in hand at a time fr reasons that are not clear here.
Anyway - if you means STL <queue>:
std::queue<Element> container;
Element e1, e2, e3, e3, e5;
e1 = container.front();
container.pop();
e2 = container.front();
container.pop();
e3 = container.front();
container.pop();
e4 = container.front();
container.pop();
e5 = container.front();
container.pop();
Behaviour is undefined if < 5 elements on the queue.
You can replace this with the below if you want to avoid the Element copy, however in this case be aware that the reference becomes invalid after the referenced element is popped.
std::queue<Element> container;
Element& e1 = container.front(); // or const Element&
container.pop();
Element& e2 = container.front();
container.pop();
Element& e3 = container.front();
container.pop();
Element& e4 = container.front();
container.pop();
Element& e5 = container.front();
container.pop();
Steve Townsend
2010-09-29 11:01:22
That code doesn’t work. Declaring `e1` as a reference means you need to initialize it right away.
Konrad Rudolph
2010-09-29 11:03:15
@Konrad, thx, deleting for now
Steve Townsend
2010-09-29 11:04:43
edit and reinstated
Steve Townsend
2010-09-29 11:25:51
I would think you would want to copy. What are you referring to after the pop()?
stefaanv
2010-09-29 11:54:47
@stefaanv - agreed, but the requirement in the q appears to be artificial anyway...
Steve Townsend
2010-09-29 13:05:47
+1
A:
#include <iostream>
#include <queue>
int main(){
std::queue<std::string> q;
for (int i = 0; i<5; ++i){
std::string s;
std::cin >> s;
q.push(s);
}
int itemsToFetch = 5;
do{
std::string curString = q.front();
q.pop();
std::cout << curString << std::endl;
--itemsToFetch;
} while (itemsToFetch > 0 && !q.empty());
return 0;
}
fschmitt
2010-09-29 11:12:08
+4
A:
No explicit loops at all:
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
template <typename T>
class QueuePopper
{
public:
QueuePopper(std::queue<T> &q) : q(q) {}
T operator() (const T &) { T t = q.front(); q.pop(); return t; }
private:
std::queue<T> &q;
};
int main()
{
typedef std::string T;
std::queue<T> q;
...
std::vector<T> v(5);
std::transform(v.begin(), v.end(), v.begin(), QueuePopper<T>(q));
}
Oli Charlesworth
2010-09-29 11:15:27
in this solution is hidden loop exist in `transform`, then where is border of your loop hiding? I can export function with loop to other file and this is a same.
Svisstack
2010-09-29 11:51:16
@Svisstack: Of course. This is an artificial scenario! But I haven't had to actually *write* a loop.
Oli Charlesworth
2010-09-29 12:00:14
+3
A:
One more way:
template<int n> void GetElements(queue<string>& qu)
{
if(!qu.empty())
{
cout << qu.front() << '\n';
qu.pop();
}
GetElements<n-1>(qu);
}
template<> void GetElements<0>(queue<string>& qu) {}
// and call:
GetElements<5>(qu);
Or:
switch(qu.size())
{
default: cout << qu.front() << '\n'; qu.pop();
case 4: cout << qu.front() << '\n'; qu.pop();
case 3: cout << qu.front() << '\n'; qu.pop();
case 2: cout << qu.front() << '\n'; qu.pop();
case 1: cout << qu.front() << '\n'; qu.pop();
case 0: ;
}
ybungalobill
2010-09-29 11:20:33
+1, because each time I see it, i find that the second construct is really clever.
ereOn
2010-09-29 12:04:26