A
Adrian
Hi,
I want a const static std::set of strings which is case insensitive
for the values.
So I have the following which seems to work but something doesnt seem
right about it. Is there a better way or any gotcha's from my code
below.
TIA
Adrian
#include <iostream>
#include <functional>
#include <algorithm>
#include <set>
#include <string>
#include <iterator>
class Test
{
public:
void p()
{
std::copy(fields.begin(), fields.end(),
std:
stream_iterator<std::string>(std::cout, ","));
std::cout << std::endl;
}
private:
struct nocase_cmp : public std::binary_function<const
std::string &, const std::string &, bool>
{
struct nocase_char_cmp : public std::binary_function<char,
char, bool>
{
bool operator()(char a, char b)
{
return std::toupper(a) < std::toupper(b);
}
};
bool operator()(const std::string &a, const std::string &b)
{
return std::lexicographical_compare(a.begin(), a.end(),
b.begin(), b.end(),
nocase_char_cmp());
}
};
typedef std::set<std::string, nocase_cmp> Field_names_t;
static const Field_names_t fields;
};
const char *f[]={
"string1",
"string2",
"string3",
"STRIng1",
"string5"};
const Test::Field_names_t Test::fields(f, f+5);
int main(int argc, char *argv[])
{
Test t;
t.p();
return 0;
}
I want a const static std::set of strings which is case insensitive
for the values.
So I have the following which seems to work but something doesnt seem
right about it. Is there a better way or any gotcha's from my code
below.
TIA
Adrian
#include <iostream>
#include <functional>
#include <algorithm>
#include <set>
#include <string>
#include <iterator>
class Test
{
public:
void p()
{
std::copy(fields.begin(), fields.end(),
std:
std::cout << std::endl;
}
private:
struct nocase_cmp : public std::binary_function<const
std::string &, const std::string &, bool>
{
struct nocase_char_cmp : public std::binary_function<char,
char, bool>
{
bool operator()(char a, char b)
{
return std::toupper(a) < std::toupper(b);
}
};
bool operator()(const std::string &a, const std::string &b)
{
return std::lexicographical_compare(a.begin(), a.end(),
b.begin(), b.end(),
nocase_char_cmp());
}
};
typedef std::set<std::string, nocase_cmp> Field_names_t;
static const Field_names_t fields;
};
const char *f[]={
"string1",
"string2",
"string3",
"STRIng1",
"string5"};
const Test::Field_names_t Test::fields(f, f+5);
int main(int argc, char *argv[])
{
Test t;
t.p();
return 0;
}