string 不区分大小写比较,出现“string iterators incompatible”debug错误

std::string中只有compare(),是区分大小写的。今天在VS2010环境下写了一个不区分大小写比较的方法,如下:

int CompareIgnoreCase(const std::string& string1,const std::string& string2) 
{
  std::string::const_iterator i1 = string1.begin();
  std::string::const_iterator i2 = string2.begin();
  std::string::const_iterator string1end = string1.end();
  std::string::const_iterator string2end = string2.end();
  for ( ; i1 != string1end && i2 != string2end; ++i1, ++i2) 
  {
    char c1 = std::toupper(*i1);
    char c2 = std::toupper(*i2);
    if (c1 < c2)
      return -1;
    if (c1 > c2)
      return 1;
  }
  if (i1 != string1end)
    return 1;
  if (i2 != string2end)
    return -1;
  return 0;
}

当运行到, for ( ; i1 != string1end && i2 != string2end; ++i1, ++i2) ,蹦出Debug Assertion Failed!     Expression:string iterators incompatible!

出现错误的源码为 xstring中

#if _ITERATOR_DEBUG_LEVEL == 2
void _Compat(const _Myiter& _Right) const
{ // test for compatible iterator pair
  if (this->_Getcont() == 0 || this->_Getcont() != _Right._Getcont())
    { // report error
        _DEBUG_ERROR("string iterators incompatible");
        _SCL_SECURE_INVALID_ARGUMENT;
    }
}

后将方法参数修改便可运行成功,修改为:

int CompareIgnoreCase(std::string string1, std::string string2) 

这是什么原因呢?哪位大侠解惑一下?

===============================================

以上代码为7-27所写。8-10想试一下const_cast。谁知只是将参数改为const,上述代码又不出错了。

int CompareIgnoreCase(const std::string& string1,const std::string& string2) 

原因还是未知。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值