簡體   English   中英

遞減迭代器,直到向量的.begin()

[英]Decrementing an iterator until the .begin() of a vector

我需要從迭代器的某個位置向后讀取向量,並在字典數組中放入n1個值。

我雖然要進行循環並減少迭代器,但是由於某些原因,我陷入了無限循環。

我認為條件是否存在問題?

//I read all the vSignal vector
while (vSignalIt != vSignal.end()) {

    i = 0;
    vSignalIt2 = vSignalIt;

    //Filling the dictionnary array
    if (vSignalIt != vSignal.begin()) {
        do 
        {
            dictionnary[i] = *vSignalIt2;
            vSignalIt2--;
            i++;
        } while (vSignalIt2 != vSignal.begin() || i < (uint8_t)n1);
    }

    //Do something with the value of dictionnary and increase vSignalIt to advance forward in the vSignal vector

}

更改“ ||” 至 '&&'

您為語句使用了錯誤的條件檢查器。 當前是:

while (vSignalIt2 != vSignal.begin() || i < (uint8_t)n1);

但是應該是:

while (vSignalIt2 != vSignal.begin() && i < (uint8_t)n1); //<-- && means 'and'

另外,作為建議,您可能應該使用reverse_iterator因為它是為此目的而構建的。

您可能要考慮使用標准算法:

auto vSignalIt = vSignal.rbegin(); // or some other reverse iterator
auto n = std::min(n1, std::distance(vSignalIt, vSignal.rend());
std::copy_n(vSignalIt, n, std::back_inserter(dictionary));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM