Creating a vector of queues

Joined
Jan 19, 2025
Messages
1
Reaction score
0
Hi everyone, Im working right now on an object which has a vector with queues in it. And the data which is stored within this queues is diverse. So I work with templates:
C++:
class Node {
    
    public:
    
    Node(T myType, int anz = 5) {

        this->elementCnt = 5;
        std::queue<T> q;
        q.push(myType);
        Base.push_back(q);
        //Base.push_back(std::queue<T> q.push(myType));
        
    }


    private:
    
    int         elementCnt;
    std::vector<std::queue<T>> Base;

};

within the main program I instantiated 2 different objects of that class:
Code:
int main(int argc, char *argv[])
{
    int n = 5;

    Node<const char*> v("hallo",n);
    Node<int> d(7,n);

    return 0;
}

Now this example is highly simplified to point out what Im talking about but it works. The datatype is getting excepted and the objects and methods are getting created for int and cstring. But I ask: could that can be made simpler. I commented out the line with my idea of don't create a temp std::queue and inserting it direct into the std::vector?
Base.push_back(std::queue<T> q.push(myType)); but this is not accepted by the compiler. Is it possible anyway, and if it is, how?

Thank you

BR Christian
 
Joined
Jan 14, 2025
Messages
25
Reaction score
6
The correct approach would be to first create an instance of std::queue and then use the push method to add elements.
 
Joined
Sep 4, 2022
Messages
150
Reaction score
16
Hi

you're at it , C++ purposes bring 'Node' and 'Linked List' around.

As you prepare your Node objects to handle 'all Types', why don't you use 'pointers' <Long> to be parts of your queue ?
no more Types to instanciate , just Pointers.
C++:
// sample from GFGeeks
// https://www.geeksforgeeks.org/cpp-linked-list/
struct Node {   
          int data;   
          Node* next;
};

// pointers as 'value:data'
struct Node {   
          long &data;   
          Node* next;
};

As you use Class, for Nodes, rebuild your Class !!,
'Nodes' and 'Nodes List' are two mates working together.

But queueing from Node Object is a wrong design step.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,249
Messages
2,571,244
Members
47,876
Latest member
Kiptechie

Latest Threads

Top