简体   繁体   English

迭代器for循环不会确认循环完成条件

[英]Iterator for-loop won't acknowledge loop finished condition

This is a snippet of my program which takes a list of integers A, a number of queries Q (a bunch of ranges in the list to be sorted), and an index k. 这是我的程序的一个片段,其中包含一个整数列表A,一个查询数量Q(要排序的列表中的一堆范围)和一个索引k。 The list is sorted according to the queries, and afterwards the k th index is printed out. 根据查询对列表进行排序,然后打印出第k个索引。

Specifically: 特别:

  • The list of integers is [8,5,3,9,2,4,3,1,9,7] 整数列表为[8,5,3,9,2,4,3,1,9,7]
  • The list of queries is [(0,1),(3,4),(0,4)] 查询列表为[(0,1),(3,4),(0,4)]
  • The index k is 3 索引k为3

Sorting A from (0,1), then (3,4), and finally (0,4) gives the final list of integers [2,3,5,8,9,4,3,1,9,7] where the k th index is 8, and this number is printed out. 从(0,1)开始排序A,然后是(3,4),最后是(0,4)给出最终的整数列表[2,3,5,8,9,4,3,1,9,7]其中第k个索引为8,并打印出该数字。

After sorting a range of the integers, that range is recorded to avoid sorting the same range multiple times unnecessarily. 在对整数范围进行排序后,将记录该范围,以避免不必要地对同一范围进行多次排序。 These recorded ranges are stored in the variable sortedSegments. 这些记录的范围存储在变量sortedSegments中。

Before sorting A according to a query, the for-loop will iterate through sortedSegments to see if the new range to sort A by has already been sorted. 在根据查询对A进行排序之前,for循环将遍历sortedSegments以查看对A进行排序的新范围是否已被排序。 The problem is that the for-loop is not exiting when it should be after it iterates through all of sortedSegments. 问题在于,在遍历所有sortedSegments之后,for循环不会在应有的时候退出。

The reason that it won't exit the loop I cannot figure out, and so I figured I would post it here so someone could point out my inevitably obvious mistake: 它不会退出循环的原因我无法弄清楚,所以我想将其张贴在这里,以便有人指出我不可避免的明显错误:

    typedef vector<pair<int,int>>   pairVec;

    bool hasOverlappingRange(pair<int,int> r1, pair<int,int> r2) {
         return (r1.first)<=(r2.second) && (r1.second)>=(r2.first);
    }

    pairVec sortedSegments;

    pairVec::iterator sit = sortedSegments.begin();
    for(sit; sit!=sortedSegments.end(); ++it) 
    { // iterate through the known ordered segments, check if query overlaps one.
        if(hasOverlappingRange( {lowIdx,highIdx} , *sit)){
            sortPortion(a, {lowIdx, highIdx}, *sit);
            segmentSorted = true;
        }
        // This is where the program breaks
    }

    if (!segmentSorted) {
        sortPortion(a, {lowIdx,highIdx},{-1,-1});
        sortedSegments.push_back( {lowIdx,highIdx} );
    }

I've inserted print lines throughout the code to watch the execution of the program and after running this verbose version of the code, the output is: 我在整个代码中插入了打印行,以观察程序的执行情况,并在运行此详细版本的代码后,输出为:

Pre-sort  A:  8 5 3 9 2 4 3 1 9 7 Segments: [] This Q: (0,1)
done checking all sorted segments
seg not sorted yet
uP:  5 sP:  8
seg sorted
new idx pushed onto segments
Post-sort A:  5 8 3 9 2 4 3 1 9 7 Segments: [ (0, 1)]

Pre-sort  A:  5 8 3 9 2 4 3 1 9 7 Segments: [ (0, 1)] This Q: (3,4)
checking if Q(3,4) overlaps (0,1)
no overlapping region here

But the program never completes, and the program fails when it should be exiting the for-loop. 但是程序永远不会完成,并且程序应该退出for循环时会失败。 I tested the exit condition by manually incrementing the iterator where it would be incremented by the loop itself, and checking to see if the iterator equals sortedSegments.end() which it does. 我通过手动递增迭代器(通过循环本身将其递增)来测试退出条件,并检查迭代器是否等于它所做的sortedSegments.end()。

    for(sit; sit!=sortedSegments.end(); ++it) 

Your not actually iterating throught sit. 您实际上并没有在坐下重复。 Your iterating variable is it which I am guessing is a typo 您正在迭代的变量是我猜的错字吗

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

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