对std::string的功能增进


inline string &operator++(string &str)
{
    char szBuf[32];
    int num=atoi(str.c_str());
    str = itoa(szBuf, ++num, 10);
    return str;
}

 

 

inline string &operator--(string &str)
{
    char szBuf[32];
    int num=atoi(str.c_str());
    str = itoa(szBuf, --num, 10);
    return str;
}

 

inline string operator+(string const &str)
{
    string ret(str);
    return ++ret;
}

 

 

inline string &operator-(string &str)
{
    for (int i=0; i<str.size(); ++i)
    {
        str[i] = tolower(str[i]);  // 非大写字母不转换
    }
    return str;
}

 

 

inline string operator-(string const &str)
{
    string ret(str);
    return --ret;
}

 


inline bool operator<<=(string const &sBuf, string const &sPre)  // 前缀比较
{
    return sBuf.compare(0, sPre.size(), sPre) == 0;
}

 

 

inline bool operator>>=(string const &sBuf, string const &sSuf)  // 后缀比较
{
    if (sBuf.size() < sSuf.size()) return false;
    return sBuf.compare(sBuf.size()-sSuf.size(), sSuf.size(), sSuf) == 0;
}

 


static char const *const g_Mark=" .,;()/r/n";  // 其它字符被当作标识符的组成部分处理

 

 

inline string::size_type operator%(string const &str, string::size_type index)  // 返回直接后趋单词的偏移值
{
    index = str.find_first_of(g_Mark, index);  // 注意连续分隔符被归并处理,当作一个分隔符解释
    if (index == string::npos) return index;
    index = str.find_first_not_of(g_Mark, index);
    return index;
}

 

 

inline string operator->*(string const &str, string::size_type index)  // 返回与指定偏移最近当前单词之克隆体
{
    index = str.find_first_not_of(g_Mark, index);
    if (index == string::npos) return "";
    string::size_type ender=str.find_first_of(g_Mark, index);
    if (ender == string::npos) ender = str.size();
    return str.substr(index, ender-index);
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值