queue assistance needed

Is there any possible way to create an STL queue of arrays?
I tried
queue<int[]>q;
It didnt work any ideas?
You could do

queue<int *> q;

A queue of pointers. Because array are more or less pointers.

But since you're using STL, why not continue that way a create a queue of vector:

queue<vector<int > > q;
If your compiler supports C++11 (or TR1), you could also use std::array, if a fixed sized array is OK.

1
2
3
const size_t array_size  = 8;

queue<array<int, array_size>> q; // C++11 doesn't need the spaces between the >s 


And while you can't create a queue of arrays, you can create a queue of structs which contain a fixed sized array. Of course, you then have to work with the struct member so the code is less tidy (unless you sort of reinvent std::array.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <iomanip>
#include <queue>
#include <algorithm>
using namespace std;

template<typename TElem>
void write(const char* prefix, const TElem* begin, const TElem* end) {
    cout << prefix << " =";
    for( ; begin != end; ++begin) {
        cout << " " << setw(2) << *begin;
    }
    cout << endl;
}

int main() {
    const size_t array_size  = 8;
    const size_t array_count = 5;

    struct Demo {
        int data[array_size];
    };

    queue<Demo> q;

    Demo temp;

    for(size_t i = 0; array_count > i; ++i) {
        int j = i;
        generate(temp.data, temp.data + array_size, [&]() {
            return ++j;
        });
        q.push(temp);
    }

     while(!q.empty()) {
        const Demo& d = q.front();
        write("a", d.data, d.data + array_size);
        q.pop();
    }

    return 0;
}


Andy
Last edited on
Topic archived. No new replies allowed.