For 循环条件(整数向量)。如何获取之前的值?
For-loop-condition (vector of ints). How to get the previous value?
我有整数向量,仅由 1
或 0
值填充。我想要做的是,当 current 值为 1 和 previous/old是 0 或相反,如果 Current=0
和 Previous=1
,则将另一个变量(AvgCan
)分配给 0
.
我正在尝试从 FOR 条件中获取先前的值。但是,如果我以通常的方式尝试它,我仍然会一直得到相同的值,直到循环结束。问题出在第一个 if 语句中。
int AvgCan = 0;
int OldAvgCan = 0;
int iteration = 0;
int iterationDecrease = 0;
for (int i = 0; i < resultINT.size(); i++)
{
//myFileO << to_string(resultINT.at(i)) + "\n";
cout << to_string((resultINT.at(i))) + " Current" + "\n";
cout << to_string((resultINT.at(i - iteration))) + " Old" + "\n" << endl;
cout << to_string(AvgCan) + "\n" << endl;
iteration = i;
iterationDecrease = i - 1;
if ((resultINT.at(i)) != (resultINT.at(iteration - iterationDecrease)))
{
AvgCan = 0;
}
if ((resultINT.at(i)) == 1)
{
/*if ((resultINT.at(i- iteration)) != 1)
{
AvgCan = 0;
}*/
AvgCan++;
}
if ((resultINT.at(i)) == 0)
{
/*if ((resultINT.at(i- iteration))!=0 )
{
AvgCan = 0;
}*/
AvgCan--;
}
myFileO << to_string(AvgCan) + "\n";
}
如您所见,我将迭代器 i
分配给了 iteration
变量,将 i - 1
分配给了 iterationDecrease
。 (我也尝试了 i--
和类似的可能方法。)
我模拟了数据所以结果是1,1,1,1,0,0,0,0
。当它从 1
变为 0
并进入 if 条件时,但每次下一次迭代它仍然 returns 1
像 old values,即使它是 0
.
我还添加了屏幕截图以便更好地理解。右边是控制台输出。
想想表达式 iteration - iterationDecrease
。它有一个常数值 1.
您可能希望 resultINT.at(iterationDecrease)
,但它会在第一次迭代时导致异常,因为它会尝试访问 -1 元素。
这里
iteration = i;
iterationDecrease = i - 1;
if ((resultINT.at(i)) != (resultINT.at(iteration - iterationDecrease)))
iteration - iterationDecrease
等于 i - (i - 1)
,它总是 1
。实际上意味着 if
该语句等同于
if ((resultINT.at(i)) != (resultINT.at(1)))
你的意思可能是
if (resultINT.at(iteration) != resultINT.at(iterationDecrease))
仍然不正确,因为当 i==0
时,iterationDecrease = -1
将抛出异常,用于调用 .at(-1)
因此,您需要在循环中从 i=1
开始,
for (int i = 1; i < resultINT.size(); i++)
{
// ...code
if (resultINT[i] != resultINT[i-1])
{
// ...code
}
}
尽可能避免索引。这不是最终解决方案,但它可能会为您指明正确的方向。使用 adjacent_find to find pairs. I made this example for you here.
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> vec{ 0,0,1,1,0,0,0,1,0,1,0,0,0,0,1,1 };
auto it = std::begin(vec);
while (1) {
it = std::adjacent_find(it, std::end(vec), [](int v1, int v2) {
return std::min(v1, v2) == 0 && std::max(v1, v2) == 1;
});
if (it != std::end(vec)) {
std::cout << "Pairs " << *it << " and " << *(it + 1) << " with indexes "
<< std::distance(std::begin(vec), it) << " and "
<< std::distance(std::begin(vec), it + 1) << std::endl;
++it;
}
else {
break;
}
}
return 0;
}
Output
Pairs 0 and 1 with indexes 1 and 2
Pairs 1 and 0 with indexes 3 and 4
Pairs 0 and 1 with indexes 6 and 7
Pairs 1 and 0 with indexes 7 and 8
Pairs 0 and 1 with indexes 8 and 9
Pairs 1 and 0 with indexes 9 and 10
Pairs 0 and 1 with indexes 13 and 14
我有整数向量,仅由 1
或 0
值填充。我想要做的是,当 current 值为 1 和 previous/old是 0 或相反,如果 Current=0
和 Previous=1
,则将另一个变量(AvgCan
)分配给 0
.
我正在尝试从 FOR 条件中获取先前的值。但是,如果我以通常的方式尝试它,我仍然会一直得到相同的值,直到循环结束。问题出在第一个 if 语句中。
int AvgCan = 0;
int OldAvgCan = 0;
int iteration = 0;
int iterationDecrease = 0;
for (int i = 0; i < resultINT.size(); i++)
{
//myFileO << to_string(resultINT.at(i)) + "\n";
cout << to_string((resultINT.at(i))) + " Current" + "\n";
cout << to_string((resultINT.at(i - iteration))) + " Old" + "\n" << endl;
cout << to_string(AvgCan) + "\n" << endl;
iteration = i;
iterationDecrease = i - 1;
if ((resultINT.at(i)) != (resultINT.at(iteration - iterationDecrease)))
{
AvgCan = 0;
}
if ((resultINT.at(i)) == 1)
{
/*if ((resultINT.at(i- iteration)) != 1)
{
AvgCan = 0;
}*/
AvgCan++;
}
if ((resultINT.at(i)) == 0)
{
/*if ((resultINT.at(i- iteration))!=0 )
{
AvgCan = 0;
}*/
AvgCan--;
}
myFileO << to_string(AvgCan) + "\n";
}
如您所见,我将迭代器 i
分配给了 iteration
变量,将 i - 1
分配给了 iterationDecrease
。 (我也尝试了 i--
和类似的可能方法。)
我模拟了数据所以结果是1,1,1,1,0,0,0,0
。当它从 1
变为 0
并进入 if 条件时,但每次下一次迭代它仍然 returns 1
像 old values,即使它是 0
.
我还添加了屏幕截图以便更好地理解。右边是控制台输出。
想想表达式 iteration - iterationDecrease
。它有一个常数值 1.
您可能希望 resultINT.at(iterationDecrease)
,但它会在第一次迭代时导致异常,因为它会尝试访问 -1 元素。
这里
iteration = i;
iterationDecrease = i - 1;
if ((resultINT.at(i)) != (resultINT.at(iteration - iterationDecrease)))
iteration - iterationDecrease
等于 i - (i - 1)
,它总是 1
。实际上意味着 if
该语句等同于
if ((resultINT.at(i)) != (resultINT.at(1)))
你的意思可能是
if (resultINT.at(iteration) != resultINT.at(iterationDecrease))
仍然不正确,因为当 i==0
时,iterationDecrease = -1
将抛出异常,用于调用 .at(-1)
因此,您需要在循环中从 i=1
开始,
for (int i = 1; i < resultINT.size(); i++)
{
// ...code
if (resultINT[i] != resultINT[i-1])
{
// ...code
}
}
尽可能避免索引。这不是最终解决方案,但它可能会为您指明正确的方向。使用 adjacent_find to find pairs. I made this example for you here.
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> vec{ 0,0,1,1,0,0,0,1,0,1,0,0,0,0,1,1 };
auto it = std::begin(vec);
while (1) {
it = std::adjacent_find(it, std::end(vec), [](int v1, int v2) {
return std::min(v1, v2) == 0 && std::max(v1, v2) == 1;
});
if (it != std::end(vec)) {
std::cout << "Pairs " << *it << " and " << *(it + 1) << " with indexes "
<< std::distance(std::begin(vec), it) << " and "
<< std::distance(std::begin(vec), it + 1) << std::endl;
++it;
}
else {
break;
}
}
return 0;
}
Output
Pairs 0 and 1 with indexes 1 and 2
Pairs 1 and 0 with indexes 3 and 4
Pairs 0 and 1 with indexes 6 and 7
Pairs 1 and 0 with indexes 7 and 8
Pairs 0 and 1 with indexes 8 and 9
Pairs 1 and 0 with indexes 9 and 10
Pairs 0 and 1 with indexes 13 and 14