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

std::map and case insensitive search

P: n/a
bb
Hi,

void fun(const std::map<std::string,int>& m1) {
// How to make a case insensitive search of this map without
making a copy?
}

cheers.

May 18 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
bb wrote:
Hi,

void fun(const std::map<std::string,int>& m1) {
// How to make a case insensitive search of this map without
making a copy?
}

cheers.

Don't use std::map<string, int>. Use std::map<string, int, predicate>.
May 18 '07 #2

P: n/a
bb wrote:
Hi,

void fun(const std::map<std::string,int>& m1) {
// How to make a case insensitive search of this map without
making a copy?
}

cheers.
Essentially what you're asking for is a std::map with two different
keys, (one case sensitive, one case insensitive) which is impossible.

You need to rework your problem. You could try

std::map<case_insensitive_string, int>

or

std::map<std::string, int, case_insensitive_comparator>

or

you could convert all strings to one case before inserting into the map.

If you really need both case sensitive and case insensitive searches on
the same map, you are going to have to come up with a more complex data
structure yourself. You can't expect the standard library to do
everything for you.

john
May 19 '07 #3

P: n/a
bb wrote:
void fun(const std::map<std::string,int>& m1) {
// How to make a case insensitive search of this map without
making a copy?
}
The map data structure is organized with the default < operator
in this case. It's simply physically impossible, using this map
instance only, to perform a O(log n) search when the comparator
is different because the items simply aren't organized that way.

You can perform a linear search, though (in this case it's not
possible to achieve anything faster). Simply traverse from begin()
to end() and compare keys.
May 19 '07 #4

P: n/a
John Harrison wrote:
Essentially what you're asking for is a std::map with two different
keys, (one case sensitive, one case insensitive) which is impossible.
Actually he didn't impose any speed limitations on the search.
Nothing stops him from making a linear search through they elements
of the map.
May 19 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.