本文介绍了另一份算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个向量。
vector<Object> objects;
vector<string> names;
这两个向量填充并具有相同的尺寸。
我需要一些算法不分配给对象变量。这可能是使用boost ::拉姆达。
比方说:
These two vectors are populated and have the same size.I need some algorithm which does assignment to the object variable. It could be using boost::lambda.Let's say:
some_algoritm(objects.begin(), objects.end(), names.begin(), bind(&Object::Name, _1) = _2);
任何建议?
推荐答案
我想不出一个的std ::
算法这一点。但是,你总是可以编写自己的:
I can't think of a std::
algorithm for this. But, you can always write your own:
template < class It1, class It2, class Operator >
It2 zip_for_each ( It1 first1, It1 last1,
It2 result, Operator op )
{
while (first1 != last1)
op(*first++, *result++);
return result;
}
修改:另一种选择,如果你能够定义
运算符=
适当,是的std ::复制
:EDIT: Another alternative, if you are able to define
operator=
appropriately, is std::copy
:#include <vector>
#include <string>
struct Object {
std::string name;
int i;
void operator=(const std::string& str) { name = str; }
};
int main () {
std::vector<Object> objects(3);
std::vector<std::string> names(3);
names[0] = "Able";
names[1] = "Baker";
names[2] = "Charlie";
std::copy(names.begin(), names.end(), objects.begin());
}
这篇关于另一份算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!