为什么这个向量要复制它的第一个元素

Why is this vector copying its first element?

本文关键字:第一个 元素 复制 向量 为什么      更新时间:2023-10-16

我对向量比较陌生,这个问题让我很困惑。

我有2个数据向量,其中包含先前分析过的图形的分解(峰值和拐点的位置)。下面的函数应该做的是找到平均位置两侧的拐点,并将它们存储在"左"answers"右"向量中。左边的向量得到正确的数据,但是无论我做什么,右边的向量收集第一个点,然后每次我尝试添加一个新的点时复制这个点。

代码:

void pairs(std::vector<int> &means, std::vector<int> &inflexions, std::vector<int> &inflexLeft, std::vector<int> &inflexRight)
{
    //////////////
    //INITIALISE//
    //////////////
    if((means.size() <= 0) || inflexions.size() <= 0)               // If nothing to do
    {
        means.clear();                                              // Leave nothing to chance...
        inflexions.clear();
        return;                                                     // And do nothing =)
    }
    inflexLeft.clear();
    inflexRight.clear();
    inflexLeft.reserve(means.size());
    inflexRight.reserve(means.size());
    std::vector<int>::iterator itI = inflexions.begin();
    std::vector<int>::iterator inflexStart = itI;
    std::vector<int>::iterator endI = inflexions.end();
    std::vector<int>::iterator itM = means.begin();
    std::vector<int>::iterator endM = means.end();
    /////////////////
    //COLLECT PAIRS//
    /////////////////
    for(; (itM != endM) && (itI != endI); itM++, itI++)             // For all the elements in the "means" list
    {
        for(; itI != endI; itI++)
        {
            std::cout<<*itI<<", t";
            if((*itI) > (*itM))                                     // If we've gone past a mean value...
            {
                if(itI == inflexStart)                              // And this isn't the beginning
                {inflexLeft.push_back(-1);}
                else
                {inflexLeft.push_back(*(itI-1));}                   // Then store the previous inflexion point as LHS inflex for this mean
                inflexRight.push_back(*itI);                        // And store the current inflexion point as the RHS variant
                break;
            }
            else if((itI == endI-1) && (inflexStart != endI-1))     // If we're at the end and have nowhere else to go
            {
                inflexLeft.push_back(*(itI-1));                     // Set the LHS inflex point as the one prior to this...
                inflexRight.push_back(-1);                          // And set the RHS to NULL
                break;
            }
        }
        std::cout<<std::endl;
    }
}

示例输出:

1,  2,  3,  4,  11,     
13,     20,     22,     23,     25,     29,     32,     35,     36,     42,     
44,     47,     53,     54,     58,     
Output: (inflexLeft on left, inflexRight on right)
4,      11,     
36,     11,     
54,     11,

在上面的例子中,每一个新的行都是一个平均值出现的地方,拐点应该被适当地存储。输出的左列是左拐点,右列应该显示类似的内容(例如6,50,73,…)等等)。

我使用QT creator 2.6.1,基于QT 4.8.3使用minGW,在Windows 7 64位。包含的头文件:

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <cmath>
#include <stdexcept>

提前感谢您的帮助。

//////////////////编辑/////////////////

我在调试器上加倍努力。我使用下面的代码来查看我所指向的点的内存地址,就在我试图把它推回去之前:

std::cout << &*itI << ", t" << *itI << ", t";

由此我确认了迭代器本身并没有神奇地指向其他地方,而是确实在迭代。这意味着问题所在的代码行是:

inflexRight.push_back(*itI);                        // And store the current inflexion point as the RHS variant

我已经试过把这行放在LHS push_back操作等上面,结果是一样的。我甚至尝试将数据复制到一个临时值,然后将其移交给inflexRight。Push_back,结果没有变化。

输出最终结果的代码在main函数中:

for(; itLeft != itEnd; itLeft++, itRight++)
{
    std::cout << *itLeft << ", t" << *itRight << ", t" << std::endl;
}
std::cout << std::endl;

所以我用你的确切函数对运行了一个简化的版本(平均值设置为输出类似于你应该有我认为的东西)

int main() {
    std::vector<int> inflex = {
            1,  2,  3,  4,  11,
            13, 20, 22, 23, 25, 29, 32, 35, 36, 42,
            44, 47, 53, 54, 58
        },
        means = { 10, 40, 55 },
        left,
        right;
    pairs(means, inflex, left, right);
    std::vector<int>::const_iterator itLeft(left.begin()),
        itRight(right.begin()),
        itEnd(left.end());
    for(; itLeft != itEnd; itLeft++, itRight++)
    {
        std::cout << *itLeft << ", t" << *itRight << ", t" << std::endl;
    }
    std::cout << std::endl;
    return 0;
}

and I got this

1,  2,  3,  4,  11,     
13,     20,     22,     23,     25,     29,     32,     35,     36,     42,     
44,     47,     53,     54,     58,     
// Output: (inflexLeft on left, inflexRight on right)
4,      11,     
36,     42,     
54,     58,

所以我不认为问题来自你所显示的代码部分,你的pairs函数应该是正确的。