c ++在for循环中初始化2个不同的迭代器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9602918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ Initialising 2 different iterators in a for loop
提问by Derek
Possible Duplicate:
Can I declare variables of different types in the initialization of a for loop?
I'd like to have a for loop in c++ which constructs 2 different kinds of vector iterator in the initialisation.
我想在 C++ 中有一个 for 循环,它在初始化中构造了 2 种不同类型的向量迭代器。
Here is a rough idea of what I would like:
这是我想要什么的粗略想法:
std::vector<double> dubVec;
std::vector<int> intVec;
double result = 0;
dubVec.push_back(3.14);
intVec.push_back(1);
typedef std::vector<int>::iterator intIter;
typedef std::vector<double>::iterator dubIter;
for (intIter i = intVec.begin(), dubIter j = dubVec.begin(); i != intVec.end(); ++i, ++j)
{
result += (*i) * (*j);
}
Anyone know what is the standard to do in this situation? I can't just use a vector of double for the intVec because I'm looking for a general solution. [i.e. I might have some function f which takes int to double and then calculate f(*i) * (*j)]
有谁知道在这种情况下做什么的标准是什么?我不能只对 intVec 使用 double 向量,因为我正在寻找通用解决方案。[即我可能有一些函数 f 将 int 加倍然后计算 f(*i) * (*j)]
回答by hmjd
You could declare a std::pair
with first
and second
as the iterator types:
您可以将std::pair
withfirst
和声明second
为迭代器类型:
for (std::pair<intIter, dubIter> i(intVec.begin(), dubVec.begin());
i.first != intVec.end() /* && i.second != dubVec.end() */;
++i.first, ++i.second)
{
result += (*i.first) * (*i.second);
}
回答by Luchian Grigore
You can't declare variables of different types inside a for
loop.
您不能在for
循环内声明不同类型的变量。
Just declare them outside:
只需在外面声明它们:
intIter i = intVec.begin();
dubIter j = dubVec.begin();
for (; i != intVec.end(); ++i && ++j)
{
}
回答by Alecs
For example
例如
intIter i = intVec.begin();
dubIter j = dubVec.begin();
for (; i != intVec.end(); ++i && ++j)
{
result += (*i) * (*j);
}
you can declare several var. only of the same type in the for. And are you sure with this part
你可以声明几个var。仅在 for 中具有相同类型。你确定这部分
++i && ++j
? I believe you want to write there
? 我相信你想在那里写
++i, ++j
So obviously you must read basics about for loop in C++
所以很明显你必须阅读 C++ for 循环的基础知识
回答by ltjax
Check out the zip iterator. It does exactly what you want: parallel iterate over two or more sequences simultaneously. Using that, I'd write it as:
查看zip 迭代器。它完全符合您的要求:同时并行迭代两个或多个序列。使用它,我将其写为:
using namespace boost;
for (auto i=make_zip_iterator(make_tuple(dubVec.begin(), intVec.begin())),
ie=make_zip_iterator(make_tuple(dubVec.end(), intVec.end()));
i!=ie; ++i)
{
// ...
}
Admittedly, this get's a little more complicated if you don't have support for auto
or other type inference in your specific case, but it can still be quite nice with a typedef.
诚然,如果您auto
在特定情况下不支持或其他类型推断,这会稍微复杂一些,但使用 typedef 仍然非常好。
回答by David
Don't overcomplicate things.
不要把事情复杂化。
for( size_t i = 0; i < intVec.size(); ++i )
{
result += intVec[i] * dubVec[i];
}
回答by Anton Daneyko
It seems you need an inner_product algorithm.
看来您需要一个inner_product 算法。
#include <vector>
#include <functional>
#include <numeric>
#include <iostream>
struct my_plus
{
double operator()(int i, double d)
{
return d + i;
}
};
struct my_multiplies
{
double operator()(int i, double d)
{
return d * i;
}
};
int main()
{
std::vector<double> dubVec;
std::vector<int> intVec;
double result = 0;
dubVec.push_back(3.14);
intVec.push_back(1);
result = std::inner_product(intVec.begin(),
intVec.end(),
dubVec.begin(),
0.0,
my_plus(),
my_multiplies());
std::cout << result << std::endl;
}
I used my own functors, because I suspect the standard multiplies and plus expect both operands to be of similar type, but I might be wrong.
我使用了自己的函子,因为我怀疑标准乘法和加法期望两个操作数的类型相似,但我可能是错的。
回答by Ernest Friedman-Hill
The easiest thing to do, at the expense of widening the scope of the iterators, would be to just hoist them up to the containing scope:
以扩大迭代器的范围为代价,最简单的方法就是将它们提升到包含范围:
intIter i;
dubIter j;
for (i = intVec.begin(), j = dubVec.begin(); i != intVec.end(); ++i && ++j)
{
result += (*i) * (*j);
}
回答by Igor Korkhov
intIter i;
dubIter j;
for (i = intVec.begin(), j = dubVec.begin(); i != intVec.end() && j != dubIter.end(); ++i, ++j)
{
result += (*i) * (*j);
}