0

这个函数的某个地方有问题,不幸的是编译器没有说在哪里。程序只是停止工作,必须关闭。

我是 C++ 的初学者,所以我做了一些愚蠢的事情,但我不知道是什么。

我会感谢任何帮助。

vector< vector<string> >gsiread16::make_section(vector<string> blocks) {

string code;
string code2;
vector<string> section;
vector< vector<string> > sections;

for ( int i = 0; i < blocks.size(); i++) {

    code = blocks[ i ].substr(0,3);

    if( code == "*41" ) {

                code2 = blocks[  i+1 ].substr(0,3);

                if( code2 != "*11" ) continue;

                int index = i +1;

                while(code2 == "*11" ) {

                    section.push_back( blocks[ index ] );
                    index++;

                }

                sections.push_back(section);
                section.clear();
                i = index - 1;

    } else continue;

}

return sections;

}

4

3 回答 3

3
> while(code2 == "*11" ) {
>     section.push_back( blocks[ index ] );
>     index++; }

永远无法通过...

i = index - 1;

可能以无限循环结束

于 2013-10-11T10:19:01.053 回答
2

超出范围的索引 where 0 <= i < size

 blocks [i+1]

i+1超过有效索引 where i=blocks.size()-1

此外,这个循环永远不会结束:

while(code2 == "*11" ) {
   section.push_back( blocks[ index ] );
   index++;
}
于 2013-10-11T10:16:51.150 回答
0

当 i=blocks.size()-1 for 时超出范围: code2 = blocks[ i+1 ].substr(0,3);

这也是一个无限循环:while(code2 == "*11") {

                section.push_back( blocks[ index ] );
                index++; }
于 2013-10-11T10:33:10.417 回答