472,143 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 7881
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

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.