ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.3 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.daniweb.com/programming/software-development/threads/474865/multiple-data-in-a-queue |
| Last Crawled | 2026-03-28 17:28:00 (9 days ago) |
| First Indexed | 2015-09-27 13:26:00 (10 years ago) |
| HTTP Status Code | 200 |
| Meta Title | c++ - Multiple data in a queue | DaniWeb |
| Meta Description | I want to create a queue that holds certain information. I want it to hold multiple peices of information ... |
| Meta Canonical | null |
| Boilerpipe Text | 12 Years Ago
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!
Dani AI
Generated
4 Months Ago
Replies from
and
correctly point to the core idea: group related fields into one item and enqueue that item. For a class-based approach, prefer a compact, value-semantic class (constructor + const accessors) or else store pointers to the class when copying is undesirable. The snippet below shows a practical pattern that avoids unnecessary copies by keeping heap-allocated objects in the queue via std::unique_ptr.
#include <queue>
#include <memory>
#include <string>
#include <iostream>
class ProcessInfo {
public:
ProcessInfo(int pid, std::string filename, int memstart, char rw, std::string length)
: pid_(pid), filename_(std::move(filename)), memstart_(memstart), rw_(rw), length_(std::move(length)) {}
int pid() const { return pid_; }
const std::string& filename() const { return filename_; }
private:
int pid_;
std::string filename_;
int memstart_;
char rw_;
std::string length_;
};
int main() {
std::queue<std::unique_ptr<ProcessInfo>> q;
q.push(std::make_unique<ProcessInfo>(42, "data.bin", 0x1000, 'r', "128KB"));
std::unique_ptr<ProcessInfo> item = std::move(q.front());
q.pop();
std::cout << item->pid() << " " << item->filename() << '\n';
}
Notes and practical tips:
std::queue
exposes only
front
/
back
/
push
/
pop
; it does not return the popped value, so move from
front()
before calling
pop()
.
Use
std::unique_ptr
when single ownership is needed; use
std::shared_ptr
for shared ownership or polymorphism.
If iteration or indexed access is required, use
std::deque
(the default underlying container) or another container directly instead of
std::queue
.
std::queue
is not thread-safe; add synchronization or a concurrent queue implementation for multi-threaded producers/consumers.
Implementing a custom queue is only warranted for special behavior (lock-free requirements, custom allocation, strict capacity/ring-buffer semantics); otherwise the STL containers suffice.
This complements the earlier answers and shows a safe, modern way to enqueue class instances while avoiding heavy copies.
Recommended Answers
Use a struct and the C++ STL queue to hold those data structs
—
David W
131
Jump to Post
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 …
—
David W
131
Jump to Post
All 7 Replies
David W
131
Practically a Posting Shark
12 Years Ago
Use a struct and the C++ STL queue to hold those data structs
12 Years Ago
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;
}
David W
131
Practically a Posting Shark
12 Years Ago
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;
}
12 Years Ago
Yeah, David is right - we (I) should use string. const char* is a addict from work tbh ;)
12 Years Ago
Got it guys! Is there a way to do this with a class though rather than a struct?
12 Years Ago
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;
};
David W
131
Practically a Posting Shark
12 Years Ago
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.
Edited
12 Years Ago
by David W because:
fixed typo
Reply to this 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. |
| Markdown | [Menu](https://www.daniweb.com/programming/software-development/threads/474865/multiple-data-in-a-queue#dw-sidebar "Toggle Menu")
Menu
[DaniWeb](https://www.daniweb.com/)
[Log In](https://www.daniweb.com/connect/profile/login) [Sign Up](https://www.daniweb.com/connect/profile/login/signup)
- [Read](https://www.daniweb.com/articles/latest/recommended)
- [Contribute](https://www.daniweb.com/community/contribute/2)
- [Meet](https://www.daniweb.com/connect/)
# Multiple data in a queue
1. [Forums](https://www.daniweb.com/forums)
2. [Programming](https://www.daniweb.com/programming/4)
3. [Software Development](https://www.daniweb.com/programming/software-development/2)
4. Discussion / Question
[](https://www.daniweb.com/members/922966/mystycs)
[mystycs](https://www.daniweb.com/members/922966/mystycs) 0 Newbie Poster
12 Years Ago
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\!
[c++](https://www.daniweb.com/tags/c%2B%2B) [queue](https://www.daniweb.com/tags/queue)
0
0
Reply with Quote
Share
- 3 Contributors
- 7 Replies
- 909 Views
- 1 Day Discussion Span
- Latest Post
[12 Years Ago](https://www.daniweb.com/programming/software-development/threads/474865/multiple-data-in-a-queue#post2073872)
Latest Post by David W
## Dani AI
Generated
4 Months Ago
Replies from @DavidW and @panqnik correctly point to the core idea: group related fields into one item and enqueue that item. For a class-based approach, prefer a compact, value-semantic class (constructor + const accessors) or else store pointers to the class when copying is undesirable. The snippet below shows a practical pattern that avoids unnecessary copies by keeping heap-allocated objects in the queue via std::unique\_ptr.
```
#include <queue>
#include <memory>
#include <string>
#include <iostream>
class ProcessInfo {
public:
ProcessInfo(int pid, std::string filename, int memstart, char rw, std::string length)
: pid_(pid), filename_(std::move(filename)), memstart_(memstart), rw_(rw), length_(std::move(length)) {}
int pid() const { return pid_; }
const std::string& filename() const { return filename_; }
private:
int pid_;
std::string filename_;
int memstart_;
char rw_;
std::string length_;
};
int main() {
std::queue<std::unique_ptr<ProcessInfo>> q;
q.push(std::make_unique<ProcessInfo>(42, "data.bin", 0x1000, 'r', "128KB"));
std::unique_ptr<ProcessInfo> item = std::move(q.front());
q.pop();
std::cout << item->pid() << " " << item->filename() << '\n';
}
```
Notes and practical tips:
- `std::queue` exposes only `front`/`back`/`push`/`pop`; it does not return the popped value, so move from `front()` before calling `pop()`.
- Use `std::unique_ptr` when single ownership is needed; use `std::shared_ptr` for shared ownership or polymorphism.
- If iteration or indexed access is required, use `std::deque` (the default underlying container) or another container directly instead of `std::queue`.
- `std::queue` is not thread-safe; add synchronization or a concurrent queue implementation for multi-threaded producers/consumers.
- Implementing a custom queue is only warranted for special behavior (lock-free requirements, custom allocation, strict capacity/ring-buffer semantics); otherwise the STL containers suffice.
This complements the earlier answers and shows a safe, modern way to enqueue class instances while avoiding heavy copies.
## Recommended Answers Collapse Answers
> Use a struct and the C++ STL queue to hold those data structs
>
> — [David W](https://www.daniweb.com/members/1080783/david-w) 131
[Jump to Post](https://www.daniweb.com/programming/software-development/threads/474865/multiple-data-in-a-queue#post2073536)
> 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 …
> ```
>
> — [David W](https://www.daniweb.com/members/1080783/david-w) 131
[Jump to Post](https://www.daniweb.com/programming/software-development/threads/474865/multiple-data-in-a-queue#post2073718)
## All 7 Replies
[](https://www.daniweb.com/members/1080783/david-w)
[David W](https://www.daniweb.com/members/1080783/david-w) 131 Practically a Posting Shark
12 Years Ago
Use a struct and the C++ STL queue to hold those data structs
0
0
Reply with Quote
Share
[](https://www.daniweb.com/members/1102612/panqnik)
[panqnik](https://www.daniweb.com/members/1102612/panqnik) 0 Newbie Poster
12 Years Ago
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;
}
```
1
0
Reply with Quote
Share
[](https://www.daniweb.com/members/1080783/david-w)
[David W](https://www.daniweb.com/members/1080783/david-w) 131 Practically a Posting Shark
12 Years Ago
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;
}
```
1
0
Reply with Quote
Share
[](https://www.daniweb.com/members/1102612/panqnik)
[panqnik](https://www.daniweb.com/members/1102612/panqnik) 0 Newbie Poster
12 Years Ago
Yeah, David is right - we (I) should use string. const char\* is a addict from work tbh ;)
0
0
Reply with Quote
Share
[](https://www.daniweb.com/members/922966/mystycs)
[mystycs](https://www.daniweb.com/members/922966/mystycs) 0 Newbie Poster
12 Years Ago
Got it guys! Is there a way to do this with a class though rather than a struct?
0
0
Reply with Quote
Share
[](https://www.daniweb.com/members/922966/mystycs)
[mystycs](https://www.daniweb.com/members/922966/mystycs) 0 Newbie Poster
12 Years Ago
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;
};
```
0
0
Reply with Quote
Share
[](https://www.daniweb.com/members/1080783/david-w)
[David W](https://www.daniweb.com/members/1080783/david-w) 131 Practically a Posting Shark
12 Years Ago
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.
0
0
Reply with Quote
Share
Edited
12 Years Ago
by David W because: *fixed typo*
Share
Facebook
X
LinkedIn
[Reply to this topic](https://www.daniweb.com/programming/software-development/threads/474865/multiple-data-in-a-queue#dw-reply-box)
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.
[Sign Up — It's Free\!](https://www.daniweb.com/connect/profile/login/signup)
Recommended Topics
- [ Data Structures - Airport exercise 2](https://www.daniweb.com/programming/software-development/threads/443262/data-structures-airport-exercise "Hello I ma trying to practice on Data structure using java. I solved a lot of problems but this one looks interesting but I need some help understanding what I really have to do ... > write a program that simulates the operation of a busy airport that has only ...")
- [ Generic Resizing Circular Array . Do i need to implement iterable ? 8](https://www.daniweb.com/programming/software-development/threads/462737/generic-resizing-circular-array-do-i-need-to-implement-iterable "Since the underlying data-structure used here is an array , which is itself iterable via `for-each()` , i'm wondering how much benefit implementing `Iterable` will provide here. I would appreciate if someone could review my code and suggest any further improvements. **Code:** import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; class ResizingCircularArray<E> ...")
- [ error 2065 2](https://www.daniweb.com/programming/software-development/threads/474146/error-2065 "Hi guys im a beginner at programming i started a book for beginners im only on lopps now but as uasuall i googled and wondered off into classes i keep getting this error, i initialized the float variables in the class by using the constructor. I'm not sure if this ...")
- [ multiple definition of 'main' in c 3](https://www.daniweb.com/programming/software-development/threads/487307/multiple-definition-of-main-in-c "hello friends i finally started programmation at school, we use ubuntu, at home i installed eclipse with MinGW i tried simple helllo world and it works but when i start mutiple exercices under same same project i get a compiler errormultiple definition of 'main' in c i tried `int main ...")
- [ class template problem 2](https://www.daniweb.com/programming/software-development/threads/473449/class-template-problem "I need a help in coding this program... write a C++ program to read a set of n numbers where n is defined by the user and print the contents of array in the reverse order using class template, numbers should include int and double data types... please reply soon....")
- [ Reading data from text file - C++ 4](https://www.daniweb.com/programming/software-development/threads/241957/reading-data-from-text-file-c "Hi, could anybody please tell me what i am doing wrong in this program. The goal is to read the student name and grade from a text file and print it on the screen. This is what i have so far. by the way this is for a school project ...")
- [ Building a database application 6](https://www.daniweb.com/programming/software-development/threads/19363/building-a-database-application "Hello everyone! I am planning to build a database application for my school to maintain school lockers. However, I have a few questions regarding that application: [list=1] [*]Is it possible to use access databse with C++? [*]Is there anyway to authenicate a user with C++? [*]How shoud I go about ...")
- [ C++ Object Oriented Queue(using double linked list) 2](https://www.daniweb.com/programming/software-development/threads/459537/c-object-oriented-queue-using-double-linked-list " //Author: Frank R. Mendez //Title: Object Oriented Queue //Description: This is program uses double linked list to store data in the queue its functions are enqueu,dequeue and display queue. //Hope it can help you in you future projects. Good vibes to all programmers! #include <iostream> using namespace std; struct Node ...")
- [ HELp Required.... 4](https://www.daniweb.com/programming/software-development/threads/114332/help-required "hi every body.... i need little help regarding project which we have to made in C++..... can someone guide me tht which type of topices can selected...... i am very much confused....:S")
- [ C Program - to accept 'n' strings from user and store it in a char array 10](https://www.daniweb.com/programming/software-development/threads/477820/c-program-to-accept-n-strings-from-user-and-store-it-in-a-char-array "C Program - to accept 'n' strings from user and store it in a char array I am currently using Turbo C++ Version 3.0 (one with blue screen) Can any one help with the C Code of the subject ?")
- [ need help 3](https://www.daniweb.com/programming/software-development/threads/209298/need-help "can anyone tell me how to write a programme to print numbers in following fashion 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1")
- [ C++ program to find sum of marks entered by 30 students 2](https://www.daniweb.com/programming/software-development/threads/467353/c-program-to-find-sum-of-marks-entered-by-30-students "C++ program to find sum of marks entered by 30 students using for loop or while loop/")
- [ Segmentation fault when reading from file 2](https://www.daniweb.com/programming/software-development/threads/304263/segmentation-fault-when-reading-from-file "The aim of the program is to read contents from 2 files and store it in struct variables.(This is actually a part of another program which stimulates pass2 of two pass assembler) [B]This code works fine.[/B] [code=c] #include<stdio.h> #include<stdlib.h> #include<string.h> struct optbl { char opco[10]; int value; }o_tbl[100]; struct symtbl ...")
- [ C Programming Priority queue questions 3](https://www.daniweb.com/programming/software-development/threads/486303/c-programming-priority-queue-questions "I was reading this example of Priority queues and have several questions. http://matrixsust.blogspot.com/2011/11/basic-priority-queue-in-c.html #include<stdio.h> #include<malloc.h> void insert(); void del(); void display(); //How exactly do structs in structs work? Do you need to do anything special with them? Is this a //form of a linked list? Which part of this is ...")
- [ Constructor nullpointerexception 14](https://www.daniweb.com/programming/software-development/threads/399225/constructor-nullpointerexception "Even after I set a for loop in my constructor I receive a null pointer exception any ideas why? here is a bit of the code [CODE] private String[] ingredientName,units = new String [40]; private double[] ingredientAmount = new double [40]; public Recipe() // Recipe Constructor example { for(int setVals ...")
- [ queue implimentation 9](https://www.daniweb.com/programming/software-development/threads/477174/queue-implimentation "please give me a code of multiple linked queue in data structure using c++")
- [ conditional if statements 3](https://www.daniweb.com/programming/software-development/threads/474864/conditional-if-statements "Write a program to get 3 numbers from user for integer variable a, b,and c. if a is not zero, find out whether it is the common divisor of b and c.")
- [ DATA STRUCTURES IN C++ 6](https://www.daniweb.com/programming/software-development/threads/455407/data-structures-in-c "I'm a little confused with an assignment here and i just needed someone to point me in the right direction. Design an inventory class that stores the following members: serialNum : An integer that holds a part’s serial number. manufactDate : A member that holds the date the part was ...")
- [ C++ Coding Question 6](https://www.daniweb.com/programming/software-development/threads/455977/c-coding-question "Im having a problem with the following exercise. I have the whole program working, however, the transaction time is not passing to the customer. The only element that this affects is the time that the customer leaves. Anny assistance would be greatly appreciated. Best, Dani Below are the 5 files ...")
- [ Queue problem using a function append. 2](https://www.daniweb.com/programming/software-development/threads/477948/queue-problem-using-a-function-append "Question:write a function append that takes as input two queues Q1 and Q2, appends the content of Q2 to Q1 and returns Q1. The protoype of the function is: queue<float> append(queue<float> Q1, queue<float> Q2); My problem here im completly stuck i do not understand how to work it. here are ...")
Not what you need?
Reach out to all the awesome people in our software development community by starting your own topic. We equally welcome both specific questions as well as open-ended discussions.
[Start New Topic](https://www.daniweb.com/community/contribute/2)
[Topics Feed](https://www.daniweb.com/rss/2)
[Reply to this Topic](https://www.daniweb.com/programming/software-development/threads/474865/multiple-data-in-a-queue#dw-reply-box)
###
### Share Post
Permanent Link
Facebook
Facebook
X
X
LinkedIn
LinkedIn
### Insert Code Block
- Forums
- [Forum Index](https://www.daniweb.com/forums)
- [Hardware & Software](https://www.daniweb.com/hardware-and-software/1)
- [Programming](https://www.daniweb.com/programming/4)
- [Digital Media](https://www.daniweb.com/digital-media/5)
- [Community Center](https://www.daniweb.com/community-center/3)
- Recent
- [Recommended Topics](https://www.daniweb.com/articles/latest/recommended)
- [Newest Topics](https://www.daniweb.com/articles/latest?sort=started)
- [Latest Topics](https://www.daniweb.com/articles/latest?sort=latest)
- [Latest Posts](https://www.daniweb.com/posts/index/0)
- [Latest Comments](https://www.daniweb.com/stats/comments/0)
- [Top Tags](https://www.daniweb.com/community/tags/0/articles)
- Tools
- [Writing](https://www.daniweb.com/programming/software-development/threads/474865/multiple-data-in-a-queue#dw-sb-writing)
- [Start New Topic](https://www.daniweb.com/community/contribute)
- [Markdown Syntax](https://www.daniweb.com/welcome/syntax)
- [Newsletter Archive](https://www.daniweb.com/email/archive)
- [Social](https://www.daniweb.com/programming/software-development/threads/474865/multiple-data-in-a-queue#dw-sb-social)
- [Top Members](https://www.daniweb.com/stats/toplist)
- [Meet People](https://www.daniweb.com/connect/)
- [APIs](https://www.daniweb.com/programming/software-development/threads/474865/multiple-data-in-a-queue#dw-sb-apis)
- [Connect API](https://www.daniweb.com/connect/developers)
- [Forum API Docs](https://www.daniweb.com/welcome/api)
- [Topics Feed](https://www.daniweb.com/rss)
- Resources
- [Community Rules](https://www.daniweb.com/welcome/rules)
- [DaniWeb Premium](https://www.daniweb.com/donate/index)
- [FAQ](https://www.daniweb.com/welcome/faq)
- [About Us](https://www.daniweb.com/welcome/about)
- [Advertise](https://www.daniweb.com/welcome/advertising)
- [Contact Us](https://www.daniweb.com/welcome/contact)
- Legal
- [Terms of Service](https://www.daniweb.com/connect/welcome/tos)
- [Privacy Policy](https://www.daniweb.com/connect/welcome/privacy)
 Your Privacy Choices
© 2026 DaniWeb® LLC
© 2026 DaniWeb® LLC
- [FAQ](https://www.daniweb.com/welcome/faq)
- [About Us](https://www.daniweb.com/welcome/about)
- [Advertise](https://www.daniweb.com/welcome/advertising)
- [Contact Us](https://www.daniweb.com/welcome/contact)
- [Terms of Service](https://www.daniweb.com/connect/welcome/tos)
- [Privacy Policy](https://www.daniweb.com/connect/welcome/privacy)
 Your Privacy Choices |
| Readable Markdown | [](https://www.daniweb.com/members/922966/mystycs)
12 Years Ago
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\!
## Dani AI
Generated 4 Months Ago
Replies from and correctly point to the core idea: group related fields into one item and enqueue that item. For a class-based approach, prefer a compact, value-semantic class (constructor + const accessors) or else store pointers to the class when copying is undesirable. The snippet below shows a practical pattern that avoids unnecessary copies by keeping heap-allocated objects in the queue via std::unique\_ptr.
```
#include <queue>
#include <memory>
#include <string>
#include <iostream>
class ProcessInfo {
public:
ProcessInfo(int pid, std::string filename, int memstart, char rw, std::string length)
: pid_(pid), filename_(std::move(filename)), memstart_(memstart), rw_(rw), length_(std::move(length)) {}
int pid() const { return pid_; }
const std::string& filename() const { return filename_; }
private:
int pid_;
std::string filename_;
int memstart_;
char rw_;
std::string length_;
};
int main() {
std::queue<std::unique_ptr<ProcessInfo>> q;
q.push(std::make_unique<ProcessInfo>(42, "data.bin", 0x1000, 'r', "128KB"));
std::unique_ptr<ProcessInfo> item = std::move(q.front());
q.pop();
std::cout << item->pid() << " " << item->filename() << '\n';
}
```
Notes and practical tips:
- `std::queue` exposes only `front`/`back`/`push`/`pop`; it does not return the popped value, so move from `front()` before calling `pop()`.
- Use `std::unique_ptr` when single ownership is needed; use `std::shared_ptr` for shared ownership or polymorphism.
- If iteration or indexed access is required, use `std::deque` (the default underlying container) or another container directly instead of `std::queue`.
- `std::queue` is not thread-safe; add synchronization or a concurrent queue implementation for multi-threaded producers/consumers.
- Implementing a custom queue is only warranted for special behavior (lock-free requirements, custom allocation, strict capacity/ring-buffer semantics); otherwise the STL containers suffice.
This complements the earlier answers and shows a safe, modern way to enqueue class instances while avoiding heavy copies.
## Recommended Answers
> Use a struct and the C++ STL queue to hold those data structs
>
> — [David W](https://www.daniweb.com/members/1080783/david-w) 131
[Jump to Post](https://www.daniweb.com/programming/software-development/threads/474865/multiple-data-in-a-queue#post2073536)
> 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 …
> ```
>
> — [David W](https://www.daniweb.com/members/1080783/david-w) 131
[Jump to Post](https://www.daniweb.com/programming/software-development/threads/474865/multiple-data-in-a-queue#post2073718)
## All 7 Replies
[](https://www.daniweb.com/members/1080783/david-w)
[David W](https://www.daniweb.com/members/1080783/david-w) 131 Practically a Posting Shark
12 Years Ago
Use a struct and the C++ STL queue to hold those data structs
[](https://www.daniweb.com/members/1102612/panqnik)
12 Years Ago
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;
}
```
[](https://www.daniweb.com/members/1080783/david-w)
[David W](https://www.daniweb.com/members/1080783/david-w) 131 Practically a Posting Shark
12 Years Ago
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;
}
```
[](https://www.daniweb.com/members/1102612/panqnik)
12 Years Ago
Yeah, David is right - we (I) should use string. const char\* is a addict from work tbh ;)
[](https://www.daniweb.com/members/922966/mystycs)
12 Years Ago
Got it guys! Is there a way to do this with a class though rather than a struct?
[](https://www.daniweb.com/members/922966/mystycs)
12 Years Ago
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;
};
```
[](https://www.daniweb.com/members/1080783/david-w)
[David W](https://www.daniweb.com/members/1080783/david-w) 131 Practically a Posting Shark
12 Years Ago
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.
Edited 12 Years Ago by David W because: *fixed typo*
[Reply to this topic](https://www.daniweb.com/programming/software-development/threads/474865/multiple-data-in-a-queue#dw-reply-box)
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. |
| Shard | 168 (laksa) |
| Root Hash | 18142197517966842768 |
| Unparsed URL | com,daniweb!www,/programming/software-development/threads/474865/multiple-data-in-a-queue s443 |