469,626 Members | 883 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

std::map and case insensitive search

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
4 7670
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
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
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
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.

Similar topics

1 post views Thread by Antti Granqvist | last post: by
5 posts views Thread by cppaddict | last post: by
44 posts views Thread by jmoy | last post: by
14 posts views Thread by Flzw | last post: by
reply views Thread by Erik Arner | last post: by
17 posts views Thread by Gernot Frisch | last post: by
13 posts views Thread by kamaraj80 | last post: by
4 posts views Thread by Raider | last post: by
8 posts views Thread by mveygman | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.