A
Alex Buell
The short snippet below demonstrates the problem I'm having with
std::lexicographical_compare() in that it does not reliably work!
#include <iostream>
#include <vector>
#include <ctype.h>
bool compare_ignore_case_equals(char c1, char c2)
{
return toupper(c1) == toupper(c2);
}
bool compare_ignore_case_less(char c1, char c2)
{
return toupper(c1) < toupper(c2);
}
int main(int argc, char *argv[])
{
std::vector<std::string> args(argv + 1, argv + argc);
const char *words[] =
{
"add", "del", "new", "help"
};
std::vector<std::string> list(words, words + (sizeof words / sizeof words[0]));
std::vector<std::string>::iterator word = list.begin();
while (word != list.end())
{
std::cout << "Testing " << *word << " = " << args[0];
if (std::lexicographical_compare(
word->begin(), word->end(),
args[0].begin(), args[0].end(),
compare_ignore_case_equals))
{
std::cout << " found!\n";
break;
}
std::cout << "\n";
word++;
}
}
Here's an example:
./quick new
Testing add = new
Testing del = new found!
That simply cannot be correct, what is it that I've done wrongly? Thanks
std::lexicographical_compare() in that it does not reliably work!
#include <iostream>
#include <vector>
#include <ctype.h>
bool compare_ignore_case_equals(char c1, char c2)
{
return toupper(c1) == toupper(c2);
}
bool compare_ignore_case_less(char c1, char c2)
{
return toupper(c1) < toupper(c2);
}
int main(int argc, char *argv[])
{
std::vector<std::string> args(argv + 1, argv + argc);
const char *words[] =
{
"add", "del", "new", "help"
};
std::vector<std::string> list(words, words + (sizeof words / sizeof words[0]));
std::vector<std::string>::iterator word = list.begin();
while (word != list.end())
{
std::cout << "Testing " << *word << " = " << args[0];
if (std::lexicographical_compare(
word->begin(), word->end(),
args[0].begin(), args[0].end(),
compare_ignore_case_equals))
{
std::cout << " found!\n";
break;
}
std::cout << "\n";
word++;
}
}
Here's an example:
./quick new
Testing add = new
Testing del = new found!
That simply cannot be correct, what is it that I've done wrongly? Thanks