为什么这个向量要复制它的第一个元素
Why is this vector copying its first element?
我对向量比较陌生,这个问题让我很困惑。
我有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
函数应该是正确的。
相关文章:
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- 对的排序向量 (std::vector<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- C++ queue.front();为什么不从第一个元素开始呢?
- 指向数组基址的指针而不是指向第一个元素的指针有什么优点?
- 静态堆栈函数不会 1) 输入第一个元素 2)添加新元素时识别旧元素
- 从队列中删除第一个元素C++
- 如果第一个元素包含任何零,则我的程序以不希望的方式运行
- 为什么数组不打印第一个元素?
- 在数组中显示第一个元素用户输入
- 消除集合的第一个元素
- 为什么指向矢量第一个元素的指针会丢失?
- 如何打印出常量字符串的第一个元素?
- 读取数组的第一个元素还是第 4000 个元素更快?
- 如何在数组的一部分中查找特定值的第一个元素
- 对数组进行排序的算法,先是第一个元素,然后是前 2 个元素,然后是前 3 个元素,依此类推
- 访问指向对象指针向量的指针的第一个元素?
- 引用保留向量中的第一个元素
- 指向数组的指针是否应该等于指向其第一个元素的指针?
- 尝试将参数包的第一个元素作为函数调用,并将包的其余部分作为参数传递给它
- 代码是否有效.如果我想显示第一个元素?如果不是,那么 s.begin() 会返回什么?