LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 07-30-2011, 04:41 AM   #1
nkoplm
Member
 
Registered: May 2005
Distribution: Fedora
Posts: 92

Rep: Reputation: 15
a map of queues? c++ STL


Hello, I am trying to store the data in my program in a map of priority_queues an I am having a bit of trouble doing it.


basically I am storing activities (ie watching tv, eating dinner, playing ping-pong, etc) and I want these event grouped by the day that they happened on.


my first thought was to use a multimap where the key is the date, and the value is the activity, however I would like to keep the activities in a specific order.


I have tried to find the proper syntax for this but have frustratingly come up empty. Even google provided little assistance.

could anyone here point me in the right direction?

thanks!
 
Old 07-30-2011, 05:05 AM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Why not just:

1. Create a class that has your queue and your activity name
2. Create a map containing instances of this class?
 
Old 07-30-2011, 05:23 AM   #3
nkoplm
Member
 
Registered: May 2005
Distribution: Fedora
Posts: 92

Original Poster
Rep: Reputation: 15
Although that would work, I don't believe it has any advantage to my current design. The idea is to group activities together by which day thy were performed on, not by the type of activity that was performed. and in that respect, a "day" object would server no purpose except to encapsulate a queue. This would be extra complexity that is unneeded.


Though I could technically just have one big chrolological list of all the activites that have ever been performed, however, because the list could potentially get quite large, I need to consider running time and efficiency constraints. being able to break the list up into days, or months, or whatever, could greatly improve performance in some use cases.

If i have misunderstood you, or if i could clarify my problem better, let me know.
thanks.
 
Old 07-30-2011, 05:40 AM   #4
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by nkoplm View Post
Hello, I am trying to store the data in my program in a map of priority_queues an I am having a bit of trouble doing it.


basically I am storing activities (ie watching tv, eating dinner, playing ping-pong, etc) and I want these event grouped by the day that they happened on.


my first thought was to use a multimap where the key is the date, and the value is the activity, however I would like to keep the activities in a specific order.


I have tried to find the proper syntax for this but have frustratingly come up empty. Even google provided little assistance.

could anyone here point me in the right direction?

thanks!
Code:
std::map<Date, std::queue<Activity>>
or (recommended)
Code:
typedef std::queue<Activity> ActivityList;
typedef std::map<Date, ActivityList> ActivityMap;
Where Date and Actviity are your types. Keep in mind, that you should only be using references with element stored within map,
Code:
ActivityMap activityMap;
//initialization
Date currentDate = ...;
ActivityList &currentActivity = activityMap[currentDate];
otherwise you'll invoke copy constructor and won't be operating on element stored within map.
 
Old 07-30-2011, 12:08 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by SigTerm View Post
Code:
std::map<Date, std::queue<Activity>>
Does this compile for you? It shouldn't; it should be > >. I've seen this in other code and I'm wondering if some compilers allow it. Aside from that, I agree with your recommendation.
Kevin Barry
 
Old 07-30-2011, 12:59 PM   #6
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by ta0kira View Post
Does this compile for you?
It does:
Code:
#include <map>
#include <queue>

int main(int argc, char** argv){
	std::map<int, std::queue<int>> a;
	return 0;
}
Quote:
Originally Posted by ta0kira View Post
It shouldn't;
Can you quote C++ standard saying so? (I understand that ">>" is technically a shift operator token).

Quote:
Originally Posted by ta0kira View Post
I've seen this in other code and I'm wondering if some compilers allow it.
Microsoft Compiler allows it. g++ doesn't.
 
Old 07-30-2011, 04:44 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by SigTerm View Post
Can you quote C++ standard saying so? (I understand that ">>" is technically a shift operator token).
More importantly, can you quote the standard saying that >> is ever other-than an operator? Does this compile for you in MS?
Code:
#include <vector>

template <int Value>
struct constant
{ static const int value = Value; };

const int index = 1;

int main()
{
  std::vector <constant <1024 >> index> > instance;
}
The code in red would be a complete and valid definition statement if >> denoted the end of nested template instantiations, making the code in blue an error.

Note that a 2011 draft of the standard (14.1.1, 14.2.3) allows the syntax you used; however, it's not a part of the 2003 standard. That means you shouldn't expect it to compile with compilers that don't yet implement the 2011 standard (C++0x). Not to mention it will cause anyone using g++ problems.
Kevin Barry

edit: Also note that my example is invalid for C++0x but valid for C++03 (see 14.2.3).

Last edited by ta0kira; 07-30-2011 at 05:04 PM.
 
Old 07-31-2011, 05:50 AM   #8
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by ta0kira View Post
More importantly, can you quote the standard saying that >> is ever other-than an operator? Does this compile for you in MS?
It doesn't unless put "1024 >> index" into () brackets.

Quote:
Originally Posted by ta0kira View Post
That means you shouldn't expect it to compile with compilers that don't yet implement the 2011 standard (C++0x). Not to mention it will cause anyone using g++ problems.
I have not asked for assistance, so please refrain from telling me what to do or expect. Errors like this are quickly found during the first attempt to compile on different compiler, and are easy to deal with. Another thing is that I prefer to use typedefs for readability so it is unlikely that I'll run into this situation.
 
Old 07-31-2011, 11:57 AM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by SigTerm View Post
I have not asked for assistance, so please refrain from telling me what to do or expect. Errors like this are quickly found during the first attempt to compile on different compiler, and are easy to deal with. Another thing is that I prefer to use typedefs for readability so it is unlikely that I'll run into this situation.
Noted, but I wasn't providing you with assistance. I was more concerned with you providing a solution that won't compile on g++; such an error might not be obvious to a beginner, especially if it comes recommended by someone with experience.
Kevin Barry
 
Old 07-31-2011, 08:23 PM   #10
nkoplm
Member
 
Registered: May 2005
Distribution: Fedora
Posts: 92

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ta0kira View Post
I was more concerned with you providing a solution that won't compile on g++; such an error might not be obvious to a beginner, especially if it comes recommended by someone with experience.
Kevin Barry
Agreed. Thank you both for your help. I have it working now.
 
  


Reply

Tags
c++, map, queue, stl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to insert data in a nested STL map ? TheIndependentAquarius Programming 4 03-02-2011 05:15 AM
C++: Extracting data from a NESTED STL map TheIndependentAquarius Programming 5 02-09-2011 01:46 PM
c++, linux, Do STL map iterator always return in same order when iterate from begin( linux_linux Linux - Newbie 7 03-15-2008 09:56 PM
stl map reverse iterator r.stiltskin Programming 0 03-05-2007 12:06 PM
STL Map problem krishnacins Programming 4 04-20-2006 01:23 AM


All times are GMT -5. The time now is 04:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration