By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,657 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,657 IT Pros & Developers. It's quick & easy.

stl map without any information

P: n/a


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

Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
http://www.sgi.com/tech/stl/hash_set.html
probably this is what you are looking for :-/

Jul 23 '05 #2

P: n/a
set< char* >

Jul 23 '05 #3

P: n/a
John wrote:
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #4

P: n/a
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;
}

Jul 23 '05 #5

P: n/a
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

Jul 23 '05 #6

P: n/a


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

Jul 23 '05 #7

P: n/a
John wrote:
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;
}

Jul 23 '05 #8

P: n/a
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

Jul 23 '05 #9

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


Yes it does. set, like map, is a _sorted_ associative container. Look
at http://www.sgi.com/tech/stl/set.html for details.
Jul 23 '05 #10

P: n/a
John wrote:
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #11

P: n/a
I think std::set<char*> is exactly what you need. "set" keeps keys only and
no values.
Michael
"John" <we**********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...


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

Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.