Effective STL appendix A Locales and Case-Insensitive String Comparisons

struct it_str_1 
    : public std::binary_function<std::string, std::string, bool> {

    struct it_char {
        const std::ctype<char>&ct;
        it_char(const std::ctype<char>& c): ct(c) {}
        bool operator()(char x, char y) const {
            return ct.toupper(x) < ct.toupper(y);
            }
    };

    std::local loc;
    const std::ctype<char>& ct;
    it_str_1(const std::locale& L = std::local::classic())
        : loc(l), ct(std::use_facet<std::ctype<char> >(loc)) {}
    bool operator()(const std::string&x, const std::string& y) const {
            return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), it_char(ct));
    }
};

we are calling toupper in a loop, and the C++ standard requires toupper to make a virtual function call. Some optimizers may be smart enough to move the virtual function overhead out of the loop, but most aren’t.

another one of ctype’s memeber functions

const char* ctype<char>::toupper(char* f, chr* i) const

using it to compare two strings would require copying both strings to buffers and then converting the buffers to uppercase. They can not be fixed size arrays, but dynamic arrays would require an expensive memory allocation.

struct it_str_2:
    public std::binary_function<std::string, std::string, bool> {
    struct it_char {
        const char* tab;
        it_char(const char* t) : tab(t) {}
        bool operator() (char x, char y) const {
            return tab[x - CHAR_MIN] - tab[y - CHAR_MIN];
        }
    };

    char tab[CHAR_MAX - CHAR_MIN + 1];
    it_str_2(const std::locale& L = std::locale::classic()) {
        const std::ctype<char>& ct = std::use_facet<std::ctype<char> >(L);
        for (int i = CHAR_MIN; i <= CHAR_MAX; ++i) {
            tab[i - CHAR_MIN] = (char)i;
            ct.toupper(tab, tab + (CHAR_MAX - CHAR_MIN + 1);
        }
    bool operator()(const std::string& x, const std::string& y) const {
            return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), it_char(tab));
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值