I want to create a queue that holds certain information.

I want it to hold multiple peices of information like below.

Name, Age, Race, Sex.

How can i create a queue FIFO. that holds multiple pieces of information like this that i can access or add into?

Thanks!

Recommended Answers

All 7 Replies

Use a struct and the C++ STL queue to hold those data structs

Here you go:

#include <iostream>
#include <queue>

struct Person
{
    const char* name;
    int age;
};


int main() {

    std::queue<Person> pq;
    pq.push( Person { "John", 20} );
    pq.push( Person { "Alice", 10} );

    Person popped = pq.front();

    std::cout << "I am " << popped.name;

    return 0;
}

Nice :)

In C++, why not rather use C++ (safe) strings?

// in C++ ... why not use (safe) C++ string   ?

#include <iostream>
#include <string>
#include <queue>

struct Person
{
    const std::string name;
    int age;

    Person( const std::string& n="", int a=0 ): name(n), age(a) {}
    friend std::ostream& operator << ( std::ostream& os, const Person& p )
    {
        return os << p.name << ", age " << p.age;
    }
} ;


int main() 
{

    std::queue < Person > pq;
    pq.push( Person( "David", 66 ) );
    pq.push( Person( "John", 20 ) );

    Person front = pq.front();

    std::cout << "I am " << front << std::endl;

    return 0;
}

Yeah, David is right - we (I) should use string. const char* is a addict from work tbh ;)

Got it guys! Is there a way to do this with a class though rather than a struct?

I want to create a queue that can insert information into this class.

class Processes
{
    public:
        void setPID (int a)
        {
            PID = a;
        }
        int retrievePID()
        {
            return PID;
        }
        void setFilename (string input)
        {
            Filename = input;
        }
        string retrieveFilename()
        {
            return Filename;
        }
        void setMemstart (int a)
        {
            Memstart = a;
        }
        int retrieveMemstart()
        {
            return Memstart;
        }
        void setRW (char a)
        {
            rw = a;
        }
        int retrieveRW()
        {
            return rw;
        }
        void setFilelength (string input)
        {
            Filelength = input;
        }
        string retrieveFilelength()
        {
            return Filelength;
        }

    private:
        int PID;
        string Filename;
        int Memstart;
        char rw;
        string Filelength;
};

How does the above question, relate to your original thead here:

I want to create a queue that holds certain information.

I want it to hold multiple peices of information like below.

Name, Age, Race, Sex.

How can i create a queue FIFO. that holds multiple pieces of information like this that i can access or add into?

Are you asking about coding, bottom up, your own class Queue?

If not, please start a new thread for a new topic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.