LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 05-03-2016, 11:34 PM   #1
kikilinux
Member
 
Registered: Sep 2012
Posts: 125

Rep: Reputation: Disabled
is it correct to use a tuple as a key for a map data structure in c++?


I use the bellow code to store IP packets stream in a map data structure.

Code:
typedef std::tuple<uint32, uint32, uint16, uint16, uint8> five_tuple;

std::map<five_tuple, packet> my_map;
// some other code

my_map[five_tuple] = packet;
.
.
.
// ...

auto search = my_map.find (five_tuple variable);

if(search != my_map.end())
     my_map[variable] = new_packet;
Does the above code correct ?
 
Old 05-05-2016, 08:26 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Well, what does it do when you try it?
 
Old 05-08-2016, 11:07 PM   #3
kikilinux
Member
 
Registered: Sep 2012
Posts: 125

Original Poster
Rep: Reputation: Disabled
It realy work well.
I think the problem of my code is related to this data structure, because I didn't use it before, but it work well.
Another section of my code had bug.
 
Old 05-10-2016, 01:15 AM   #4
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by kikilinux View Post
It realy work well.
I think the problem of my code is related to this data structure, because I didn't use it before, but it work well.
Another section of my code had bug.
of course it does work well
since std::tuple implements the required operators
http://en.cppreference.com/w/cpp/uti...e/operator_cmp
to fulfil the compare concept,
http://en.cppreference.com/w/cpp/concept/Compare
 
1 members found this post helpful.
Old 05-10-2016, 10:33 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941


Incidentally, I normally define a distinct class, in all cases, which descends from Tuple (or whatever). In this way, any logic needed to manipulate or understand the stored items is contained within that class definition itself.
 
  


Reply

Tags
c++, map, 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
what is the structure of LILO map file? mhdjavanmardi Linux - General 2 05-17-2012 03:34 AM
Map the directory structure beginning with root Domskis Linux - Newbie 11 09-22-2011 03:33 PM
C++ graph / map data structure Galib Programming 3 10-17-2010 08:50 PM
How can I map the control key to the caps lock key in Linux? tate1615 Linux - General 4 07-14-2008 01:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 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
Open Source Consulting | Domain Registration