std:less

M

Mike Copeland

How does one use std:less? Specifically, I have a declaration as
follows:
struct ChipRecord // Chip Times record
{
int bibNum; // Bib #
long lowStartTime; // Start Time
long hiStartTime; // Start Time
long gunTime; // Gun Time, if needed
long lowFinishTime; // Start Time
long hiFinishTime; // Start Time
long netTime; // computed "net" Finish Time
string entName; // Entrant Name
string chipId;
string ctsKey; // City/State abbreviation key
short entAge; // Age
char entSex; // Sex code (M/F)
char entRCode;
} timeWork; // entrant info record
typedef map<string, ChipRecord> CHIPINFO;
CHIPINFO chipInfo;
map<int, ChipRecord>::iterator cIter;

In the above, the "index" to the objects is numeric (bibNum), and I
wish to assure that (1) no duplicates are stored and (2) I could produce
an ordered listing of all stored data. Since sorting an STL map seems
overly difficult, is there some way to use the std::less option somehow
to get the data stored sequentially?
If so, how do I use this option? If not, what might I do? TIA
 
J

Juha Nieminen

Mike said:
typedef map<string, ChipRecord> CHIPINFO;

Regardless of what ChipRecord is, that map will have an unambiguous
ordering because the operator<() for std::string will be used.
map<int, ChipRecord>::iterator cIter;

This is an iterator to a completely different map, but that one also
has a completely unambiguous ordering because the < operator for ints
will be used.

Let me guess: What you *really* want is this:

typedef std::set<ChipRecord> CHIPINFO;

If that's the case, then simply define an operator<() for your
ChipRecord struct. Do whatever comparisons you need inside it.
 
R

Rolf Magnus

Mike said:
How does one use std:less? Specifically, I have a declaration as
follows:
struct ChipRecord // Chip Times record
{
int bibNum; // Bib #
long lowStartTime; // Start Time
long hiStartTime; // Start Time
long gunTime; // Gun Time, if needed
long lowFinishTime; // Start Time
long hiFinishTime; // Start Time
long netTime; // computed "net" Finish Time
string entName; // Entrant Name
string chipId;
string ctsKey; // City/State abbreviation key
short entAge; // Age
char entSex; // Sex code (M/F)
char entRCode;
} timeWork; // entrant info record
typedef map<string, ChipRecord> CHIPINFO;
CHIPINFO chipInfo;
map<int, ChipRecord>::iterator cIter;

In the above, the "index" to the objects is numeric (bibNum), and I
wish to assure that (1) no duplicates are stored and (2) I could produce
an ordered listing of all stored data. Since sorting an STL map seems
overly difficult,

std::map works by sorting the elements by key internally. That's what makes
the map find its elements quickly. It's not 'overly difficult' to sort it
in a different way, but rather not possible at all.
is there some way to use the std::less option somehow
to get the data stored sequentially?

The map uses std::less on the key only.
If so, how do I use this option? If not, what might I do? TIA

If you want to change the sort order, a map isn't the right container. You
could use another container like std::vector. If it's sorted, you can use
std::binary_search to find elements quickly.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top