简体   繁体   English

string :: replace在有效的迭代器上抛出std :: out_of_range

[英]string::replace throws std::out_of_range on valid iterators

Really can't figure out the reason for 'terminate called after throwing an instance of 'std::out_of_range'' 真的无法弄清楚抛出``std :: out_of_range''实例后调用``终止''的原因

std::cerr << std::string(s[0].first, s[0].second) << std::endl;
std::cerr << std::string(e[0].first, e[0].second) << std::endl;
std::cerr << std::string(s[0].first, e[0].second) << std::endl;

the above code return valid strings with matched results 上面的代码返回具有匹配结果的有效字符串

    boost::regex start(elementStartTag);
    boost::regex end(elementEndTag);

    boost::match_results<std::string::const_iterator> s, e;

    if(!boost::regex_search(tmpTemplate, s, start)) {
        dDebug() << "No start token: " << elementStartTag << " was found in file: " << templatePath();
        std::cerr << "No start token: " << elementStartTag << " was found in file: " << templatePath() << std::endl;
        return;
    }

    if(!boost::regex_search(tmpTemplate, e, end)) {
        dDebug() << "No end token: " << elementEndTag << " was found in file: " << templatePath();
        std::cerr << "No end token: " << elementEndTag << " was found in file: " << templatePath() << std::endl;
        return;
    }

    //std::string::iterator si, ei;
    //        si = fromConst(tmpTemplate.begin(), s[0].second);
    //        ei = fromConst(tmpTemplate.begin(), e[0].first);

    //        std::cerr << std::string(si, ei) << "\t" << ss.str(); // return valid string

    std::cerr << std::string(s[0].first, s[0].second) << std::endl;
    std::cerr << std::string(e[0].first, e[0].second) << std::endl;
    std::cerr << std::string(s[0].first, e[0].second) << std::endl;


    std::cerr << "s[0].first - tmpTemplate.begin()\t" << s[0].first - tmpTemplate.begin() << std::endl;
    std::cerr << "s[0].first - e[0].second\t" << s[0].first - e[0].second << std::endl;

    //tmpTemplate.replace(fi, se, ss.str()); //also throws exeption
    tmpTemplate.replace(s[0].first - tmpTemplate.begin(), s[0].first - e[0].second, "test"); // throws exeption

gcc version: 4.7.3 if it really matters gcc版本:4.7.3如果真的很重要

boost version: 1.52.0 提升版:1.52.0

UPDATE: 更新:

First: The following equation is wrong s[0].first - e[0].second should be e[0].second - s[0].first - i wonder why nobody saw this (me also) - but consider it a typo, cause s[0].first - tmpTemplate.begin() return negative number anyway. 首先:以下等式是错误的s [0] .first-e [0] .second应该是e [0] .second-s [0] .first-我想知道为什么没人看到这个(我也是)-但考虑一下错字,导致s [0] .first-tmpTemplate.begin()始终返回负数。

tmpTemplate defined and initialized as tmpTemplate定义并初始化为

  std::string tmpTemplate= getTemplate();

Great - as i said s[0].first - tmpTemplate.begin() returns negative number 太好了-正如我所说的s [0] .first-tmpTemplate.begin()返回负数

if tmpTemplate is defined and initialized as 如果tmpTemplate被定义并初始化为

std::string tmpTemplate(getTemplate().data(), getTemplate().length());

everything is fine. 一切顺利。

Second: 第二:

stop boost::match_results uninitialized nonsense please read the regex_search documentation it says: "If i find no match i return false" 停止boost :: match_results未初始化的废话,请阅读regex_search文档,其中指出:“如果没有找到匹配项,则返回false”

Third: 第三:

std::string tmpTemplate= getTemplate();

and

std::string tmpTemplate(getTemplate().data(), getTemplate().length());

DOES REALLY DIFFER. 确实有所不同。

Own Сonclusion: 自己的加入:

It is ether a memory corruption which occurs else where in my code and i can't detect it with valgrind, or a bug which is not part of my code. 这是一个以太内存损坏,它发生在我的代码中的其他地方,而我用valgrind无法检测到它,或者是一个不属于我的代码的错误。

What are the contents of tmpTemplate , elementStartTag and elementEndTag ? 什么是内容tmpTemplateelementStartTagelementEndTag If the elementEndTag precedes the elementStartTag in tmpTemplate , then you'll definitely get an out_of_range error. 如果elementEndTag先于elementStartTagtmpTemplate ,那么你一定会得到一个out_of_range错误。

In the end, I'd recommend using just one regular expression, along the lines of: 最后,我建议仅使用一个正则表达式,大致如下:

boost::regex matcher( ".*(" + elementStartTag + ")(.*)(" + elementEndTag + ").*");

and then using boost::regex_match rather than search. 然后使用boost::regex_match而不是搜索。 This guarantees the order; 这保证了订单; it may cause problems if there is more than one matching element in the sequence, however. 但是,如果序列中有多个匹配元素,可能会导致问题。 If this is an issue: you should use: 如果这是一个问题:您应该使用:

boost::regex_search( s[1].second, tmpTemplate.end(), e, end )

as the expression for matching the end. 作为匹配末尾的表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM