使用共享指针向量增强 lambda
下面是一个稍微修改过的代码,来自一个很好的示例,如何将值从一个字符串向量复制到另一个对象向量。 (请参阅:另一种复制算法)
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
using boost::shared_ptr;
using boost::make_shared;
using namespace boost::lambda;
class Object
{
public:
std::string Name;
Object(const std::string& Name_ = "")
: Name(Name_)
{
}
};
int main()
{
//std::vector<Object> objects(3, Object());
std::vector<shared_ptr<Object> > objects(3, make_shared<Object>());
std::vector<std::string> names;
names.push_back("Alpha");
names.push_back("Beta");
names.push_back("Gamma");
std::vector<std::string>::const_iterator names_it;
names_it = static_cast<const std::vector<std::string>&>(names).begin();
//std::for_each(objects.begin(), objects.end(), bind(&Object::Name, _1) = *var(names_it)++);
std::for_each(objects.begin(), objects.end(), bind(&Object::Name, *_1) = *var(names_it)++);
//std::vector<Object>::iterator it, end = objects.end();
std::vector<shared_ptr<Object> >::iterator it, end = objects.end();
for (it = objects.begin(); it != end; ++it) {
//std::cout << it->Name << std::endl;
std::cout << (*it)->Name << std::endl;
}
return EXIT_SUCCESS;
}
在这种情况下,我使用动态分配的对象,因为 boost::lambda:: bind 无法处理像 boost::bind 那样的更改,我需要取消引用占位符才能编译:
std::for_each(objects.begin(), objects.end(), bind(&Object::Name, *_1) = *var(names_it)++);
但是在输出中我得到了:
Gamma
Gamma
Gamma
你的解释是什么?
Below is a slightly modified code from one good example how to copy values fro one vector of strings to another vector of objects. (see: another copy algorithm )
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
using boost::shared_ptr;
using boost::make_shared;
using namespace boost::lambda;
class Object
{
public:
std::string Name;
Object(const std::string& Name_ = "")
: Name(Name_)
{
}
};
int main()
{
//std::vector<Object> objects(3, Object());
std::vector<shared_ptr<Object> > objects(3, make_shared<Object>());
std::vector<std::string> names;
names.push_back("Alpha");
names.push_back("Beta");
names.push_back("Gamma");
std::vector<std::string>::const_iterator names_it;
names_it = static_cast<const std::vector<std::string>&>(names).begin();
//std::for_each(objects.begin(), objects.end(), bind(&Object::Name, _1) = *var(names_it)++);
std::for_each(objects.begin(), objects.end(), bind(&Object::Name, *_1) = *var(names_it)++);
//std::vector<Object>::iterator it, end = objects.end();
std::vector<shared_ptr<Object> >::iterator it, end = objects.end();
for (it = objects.begin(); it != end; ++it) {
//std::cout << it->Name << std::endl;
std::cout << (*it)->Name << std::endl;
}
return EXIT_SUCCESS;
}
In this case I'm using dynamically allocated Objects, and because boost::lambda::bind can't handle such changes as boost::bind do, I need to dereference placeholder in order to compile:
std::for_each(objects.begin(), objects.end(), bind(&Object::Name, *_1) = *var(names_it)++);
But then at the output I've got:
Gamma
Gamma
Gamma
What is your explanation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这相当于:
向量构造函数将创建该
对象
指针的 3 个副本,它们都将引用同一个且唯一< code>Object(即*object
)。用单独的指针填充向量,每个指针都用自己的对象
初始化。This is equivalent to doing:
The vector constructor will then make 3 copies of that
object
pointer, which will all refer to same one and uniqueObject
(that is to say,*object
). Fill the vector with separate pointers each initialized with their ownObject
.