I have a string s="java" and a map<string,int>M. M contains
(Cpp,1), (jAvA,2), (Cobol,3). I have to check if string s matches (case insensitive) with any key in map. Is there any better way than iterating through entire map and check for each record? In this example string s should match with second record in map, as it is case insensitive matching. In following example we are using compareInterval() with sort(). Is there any way that we can use any function with stricmp() and use map M and string s and perform case insensitive matching efficiently?
// A C++ program to demonstrate STL sort() using
// our own comparator
#include<bits/stdc++.h>
using namespace std;
// An interval has start time and end time
struct Interval
{
int start, end;
};
// Compares two intervals according to staring times.
bool compareInterval(Interval i1, Interval i2)
{
return (i1.start < i2.start);
}
int main()
{
Interval arr[] = { {6,8}, {1,9}, {2,4}, {4,7} };
int n = sizeof(arr)/sizeof(arr[0]);
// sort the intervals in increasing order of
// start time
sort(arr, arr+n, compareInterval);
cout << "Intervals sorted by start time : \n";
for (int i=0; i<n; i++)
cout << "[" << arr[i].start << "," << arr[i].end
<< "] ";
return 0;
}