我对矢量比较新,这个问题让我感到困惑。
我有2个数据向量,其中包含先前分析的图表(峰值位置和拐点)的明细。下面的函数应该是找到平均位置两侧的拐点,并将它们存储在“左”和“右”向量中。左边的矢量得到正确的数据,但无论我做什么,右边的矢量都会收集第一个点,然后每当我尝试添加一个新点时复制该点。
代码:
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创建者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>
提前感谢您的帮助。
////////////////// EDIT /////////////////
我用调试器重新加倍努力。在我尝试将其推回之前,我已经使用下面的代码来查看我指向的点的内存地址:
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;
答案 0 :(得分:0)
所以我运行了一个带有精确函数对的简化版本(均值设置为输出与我想的相似的东西)
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;
}
我得到了这个
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
功能应该是正确的。