另一份算法

编程入门 行业动态 更新时间:2024-10-26 23:39:08
本文介绍了另一份算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个向量。

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()); }

更多推荐

另一份算法

本文发布于:2023-11-17 03:34:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608673.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!