使用std :: erase调试断言失败

时间:2017-06-14 05:11:18

标签: c++ c++11 isspace

我正在编写一个解析日志文件的程序,并决定尽可能地将其作为C ++,并且我遇到了这行的调试断言 -

<div align="center" class="page">
  <div class="mainwrapper">

    <div class="section-holder">
      <div class="section-item">
        <div class="section-image">
          <img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
        </div>
        <div class="section-info">
          <h5>Winter Jacket</h5>
          <h6>$99.99</h6>
        </div>
      </div>

      <div class="section-item">
        <div class="section-image">
          <img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
        </div>
        <div class="section-info">
          <h5>Winter Jacket</h5>
          <h6>$99.99</h6>
        </div>
      </div>

      <div class="section-item">
        <div class="section-image">
          <img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
        </div>
        <div class="section-info">
          <h5>Winter Jacket</h5>
          <h6>$99.99</h6>
        </div>
      </div>

      <div class="section-item">
        <div class="section-image">
          <img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
        </div>
        <div class="section-info">
          <h5>Winter Jacket</h5>
          <h6>$99.99</h6>
        </div>
      </div>

      <div class="section-item">
        <div class="section-image">
          <img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
        </div>
        <div class="section-info">
          <h5>Winter Jacket</h5>
          <h6>$99.99</h6>
        </div>
      </div>

      <div class="section-item">
        <div class="section-image">
          <img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
        </div>
        <div class="section-info">
          <h5>Winter Jacket</h5>
          <h6>$99.99</h6>
        </div>
      </div>

      <div class="section-item">
        <div class="section-image">
          <img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
        </div>
        <div class="section-info">
          <h5>Winter Jacket</h5>
          <h6>$99.99</h6>
        </div>
      </div>

      <div class="section-item">
        <div class="section-image">
          <img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
        </div>
        <div class="section-info">
          <h5>Winter Jacket</h5>
          <h6>$99.99</h6>
        </div>
      </div>

      <div class="section-item">
        <div class="section-image">
          <img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
        </div>
        <div class="section-info">
          <h5>Winter Jacket</h5>
          <h6>$99.99</h6>
        </div>
      </div>

      <div class="section-item">
        <div class="section-image">
          <img src="https://cdn.shopify.com/s/files/1/0938/8938/products/10231100205_1_1315x1800_300_CMYK_1024x1024.jpeg?v=1445623369">
        </div>
        <div class="section-info">
          <h5>Winter Jacket</h5>
          <h6>$99.99</h6>
        </div>
      </div>

    </div>

  </div>
</div>

Assertion that character must lie between -1 and 256

这似乎是因为日志文件的第2000行的值为-80的字符。

The offending character

所以,我试过这个

sLine.erase(remove_if(sLine.begin(), sLine.end(), isspace), sLine.end()); 

但是这段代码片段没有任何解释。

所以,最后我有三个问题 -

  1. 为什么需要调试断言?
  2. 第二个代码段失败的原因是什么?
  3. 您可以建议的任何变通方法吗?
  4. 感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

  1. 这是一个断言,用于测试字符参数是否超出范围。字符'°'( - 80)不是ascii字符,因此超出范围。
  2. 我不确定您的意思,此代码段会删除所有ascii字符。
  3. 如果您只想删除空格字符,以下代码将起作用

    sLine.erase(std :: remove_if(sLine.begin(),sLine.end(),[](char c){     return(c ==''); }),sLine.end());