stl map without any information

J

John

Is there a way to use map without any information associated
with keys? I do not need any information stored with the keys.

map<char * , >

I do not want to waste any space and keeping information with
my key will just waste space. If this can not be done, what is
smallest piece of junk i could add?

map<char *,char> ?

Or is there something better in terms of space.

Thanks,
--j
 
J

Jerry Coffin

John said:
Is there a way to use map without any information associated
with keys? I do not need any information stored with the keys.

map<char * , >

I do not want to waste any space and keeping information with
my key will just waste space. If this can not be done, what is
smallest piece of junk i could add?

map<char *,char> ?

Or is there something better in terms of space.

std::set. If you're going to store pointers, you'll also need to pass
a comparison function to get meaningful results -- otherwise, it'll
attempt to compare the pointers themselves, not what they point at. The
result generally won't be useful.

Alternatively, you might want to store something like std::string that
already defines a useful comparison function. OTOH, this might also
negate the space savings you're trying for.
 
J

John

Thanks for all your answers. I also need the next month of the year.
I dont think std::set can do that, can it?

struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};

int main()
{
map<const char*, int, ltstr> months;

months["january"] = 31;
months["february"] = 28;
months["march"] = 31;
months["april"] = 30;
months["may"] = 31;
months["june"] = 30;
months["july"] = 31;
months["august"] = 31;
months["september"] = 30;
months["october"] = 31;
months["november"] = 30;
months["december"] = 31;

cout << "june -> " << months["june"] << endl;
map<const char*, int, ltstr>::iterator cur = months.find("june");
map<const char*, int, ltstr>::iterator prev = cur;
map<const char*, int, ltstr>::iterator next = cur;
++next;
--prev;
cout << "Previous (in alphabetical order) is " << (*prev).first <<
endl;
cout << "Next (in alphabetical order) is " << (*next).first << endl;
}
 
R

Ravi Luthra

It looks to me you are wasting what a map is good for and ignoring
other more suitable data structures. Consider some variations of what
you are attempting:

struct MonthInfo
{
const char *monthName;
int daysInMonth;
};
vector<MonthInfo> v;
// fill v with objects
// or maybe more appropriately:
MonthInfo arr[] = {{"january", 31}, {"february", 28}, etc... };

just a thought
 
J

John

That was just an example, I am going to put a million such strings,
insert and delete them and the query strings not in the map for
strings that are alphabetically just larger than the query string.

Hope this makes it more clear,
--j
 
M

Mark P

John said:
Thanks for all your answers. I also need the next month of the year.
I dont think std::set can do that, can it?

I'm still not clear what you're trying to do. set knows nothing more
nor less than map about the order of mothns in the year. But if you
truly believe that you want a map with junk values, then a set is what
you really want. Essentially a set is a map where the values are the
keys, but they're stored only once of course, so this is certainly
better than any junk data you might attach.

If you mean by "next month of the year" the next ordered element in the
set, then yes, a set can do this just as a map can.

Mark
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};

int main()
{
map<const char*, int, ltstr> months;

months["january"] = 31;
months["february"] = 28;
months["march"] = 31;
months["april"] = 30;
months["may"] = 31;
months["june"] = 30;
months["july"] = 31;
months["august"] = 31;
months["september"] = 30;
months["october"] = 31;
months["november"] = 30;
months["december"] = 31;

cout << "june -> " << months["june"] << endl;
map<const char*, int, ltstr>::iterator cur = months.find("june");
map<const char*, int, ltstr>::iterator prev = cur;
map<const char*, int, ltstr>::iterator next = cur;
++next;
--prev;
cout << "Previous (in alphabetical order) is " << (*prev).first <<
endl;
cout << "Next (in alphabetical order) is " << (*next).first << endl;
}
 
J

John

Cool, this is the answer I was looking for, so the set does have
a order defined inside it? using the iterator++ / -- function?

Thanks a lot,
--j
 
J

Jerry Coffin

John said:
Thanks for all your answers. I also need the next month of the year.
I dont think std::set can do that, can it?

Yes, but a map would be more suitable.
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};

struct month_data {
int days;
char *prev;
char *next;

month_data(int d, char *p, char *n) : days(d), prev(p), next(n) {}
};
months["january"] = 31;

months["January"] = month_data(31, "December", "February");

[ ... ]
cout << "june -> " << months["june"] << endl;

cout << "june -> " << months["june"].days << endl;

month_data &m = months.find("june")->second;
cout << "Previous month -> " << m.prev << endl;
cout << "Next month -> " << m.next << endl;

Whether this is better or worse than an array of structures holding the
month names and number of days in order depends: this uses more memory
to get faster lookups. For only 12 items, it's probably a waste, but if
you had something like millions of items, that might be a different
story.
 
M

Michael Simbirsky

I think std::set<char*> is exactly what you need. "set" keeps keys only and
no values.
Michael
 

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

Members online

No members online now.

Forum statistics

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

Latest Threads

Top