473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Map to store 3 items

New to C++. Just ran into a need to store three things in a map. Before
trying it, I thought I'd post here and ask for advice. Here is an
example of what I need to store:

map<string name, string prefix, int length>

Both name and prefix would be unique for each map entry. I was wondering
if the the first entry could be an array that holds the two items (name
and prefix), but then I wondered how an iterator would behave when
it->first is called.

Any suggestions or pointers on using a map with three items would be
much appreciated.

Brad
Jun 27 '08 #1
4 1910
In article <fv**********@solaris.cc.vt.edu>, br**@16systems.com says...
New to C++. Just ran into a need to store three things in a map. Before
trying it, I thought I'd post here and ask for advice. Here is an
example of what I need to store:

map<string name, string prefix, int length>

Both name and prefix would be unique for each map entry. I was wondering
if the the first entry could be an array that holds the two items (name
and prefix), but then I wondered how an iterator would behave when
it->first is called.
It can't be an array -- it has to be copyable and assignable. It can be
a pair<string, stringthough, in which case you'd do something like
it->first.first or it->first.second, to access the name and prefix
respectively.

Of course, you could also create a class of your own to hold the two
strings, something like:

class key {
string name;
string prefix;
public:
key(string n, string p) : name(n), prefix(p) {}
key(key const &other) : name(other.name), prefix(other.prefix) {}
key &operator=(key const &other) {
name = other.name;
prefix = other.prefix;
return *this;
}

bool operator<(key const &other) const {
if (name < other.name)
return true;
if (name other.name)
return false;
if (prefix < other.prefix)
return true;
return false;
}
};

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #2
On Thu, 08 May 2008 03:26:03 +0200, Brad <br**@16systems.comwrote:
New to C++. Just ran into a need to store three things in a map. Before
trying it, I thought I'd post here and ask for advice. Here is an
example of what I need to store:

map<string name, string prefix, int length>

Both name and prefix would be unique for each map entry. I was wondering
if the the first entry could be an array that holds the two items (name
and prefix), but then I wondered how an iterator would behave when
it->first is called.

Any suggestions or pointers on using a map with three items would be
much appreciated.

Brad

Boost Tuple ?
Jun 27 '08 #3
Jerry Coffin wrote:
class key {
string name;
string prefix;
public:
key(string n, string p) : name(n), prefix(p) {}
key(key const &other) : name(other.name), prefix(other.prefix) {}
key &operator=(key const &other) {
name = other.name;
prefix = other.prefix;
return *this;
}
What exactly do you need that copy constructor and assignment operator
for? They are doing exactly what the default ones would do. They are
useless.

How about simply:

struct Key
{
std::string name, prefix;

Key(std::string& n = "", std::string p = ""): name(n), prefix(p) {}

bool operator<(const Key& rhs)
{
return name == rhs.name ? prefix < rhs.prefix : name < rhs.name;
}
}
Jun 27 '08 #4
In article <2Q*************@read4.inet.fi>, no****@thanks.invalid
says...

[ ... ]
What exactly do you need that copy constructor and assignment operator
for? They are doing exactly what the default ones would do. They are
useless.
You're right -- I should have pointed out that I was including them only
for emphasis, but they're only doing what the compiler would generate if
they weren't included.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Colin Steadman | last post by:
Hope this makes sense! I'm building an ASP page which allows uses to add items to an invoice via a form, ie: Item No Part No Order No Quanity Units Price VAT ------- ...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
0
by: Michael O'Brien | last post by:
I'm trying to hook in a specialized state store server into ASP.NET. I understand I can create a HttpModule and hook the events OnAcquireState and OnReleaseState. So far so good. But, it seems...
0
by: Mark | last post by:
I'm trying to port the asp.net unleashed sample store from SQLServer to Access. I have it all do, execpt when I try and add something to the cart I geet this error: Exception Details:...
0
by: wizardworkz | last post by:
Hello All! Having a bit of a problem combining php with javascript here. What I have is a store with images (There will be a smaller image of each item, and a larger image of each item, but I want...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.