访问嵌套对
要拆开一对,可以执行以下操作。
boost::bind(&std::pair::second, _1); // returns the value of a pair
使用不同容器的组合,如何访问嵌套对?
例如,当我想将向量划分为补充映射中包含的项目和补充映射中未包含的项目时,我使用了以下内容:
typedef int DWORD; typedef std::pair<std::string, bool> user_info;
typedef std::map<DWORD, user_info> USER_MAP;
typedef std::vector<DWORD> VEC_STAFF;
VEC_STAFF::iterator it = std::partition(
Staff.begin(), Staff.end(),
(bind(&USER_MAP::find, m_Users, _1) != m_Users.end()));
现在我遇到了第二个问题 - 在应用程序运行期间, user_info 的 status bool 可以更改,稍后我想使用状态 bool 为 true 的项目重新分区向量,而不仅仅是包含在补充地图中。
但是,我似乎在访问嵌套对的第二项时遇到问题。
我尝试了以下操作,但我似乎无法访问嵌套对!
VEC_STAFF::const_iterator itCurEnd = partition(Staff.begin(), Staff.end(),
bind(&USER_MAP::value_type::second::second,
bind(&USER_MAP::find, &m_Users, _1)) == true);
To take apart a pair, the following can be done
boost::bind(&std::pair::second, _1); // returns the value of a pair
What about using combinations of different containers, how can a nested pair be accessed?
For example when I wanted to partition a vector into items contained in a supplemental map and items that where not contained in the supplemental map I used the following:
typedef int DWORD; typedef std::pair<std::string, bool> user_info;
typedef std::map<DWORD, user_info> USER_MAP;
typedef std::vector<DWORD> VEC_STAFF;
VEC_STAFF::iterator it = std::partition(
Staff.begin(), Staff.end(),
(bind(&USER_MAP::find, m_Users, _1) != m_Users.end()));
Now I have a second problem - during the running of the application the status bool of user_info can change, and later on I want to re-partition the vector with items that have a status bool of true rather than just being contained in the supplemental map.
However I seem to have a problem accessing the second item of a nested pair.
I tried the following but I cannot seem to access the nested pair!
VEC_STAFF::const_iterator itCurEnd = partition(Staff.begin(), Staff.end(),
bind(&USER_MAP::value_type::second::second,
bind(&USER_MAP::find, &m_Users, _1)) == true);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定我是否了解那里发生的事情,但通常当我开始遇到绑定问题时,我会放弃并实现一个函子。这可能会简化您的情况。
对我来说,下面的内容比那些乱七八糟的多级绑定更容易阅读
Not sure I follow what's going on there, but usually when I start running into problems with bind I give up and implement a functor. This might simplify things in your case.
To me, the following is a lot easier to read than all that messing around with multiple levels of binds
您使用的语法显然不起作用。第一个“::second”已经表示非静态成员而不是类型。如果一对内有一对,您可能必须使用两个绑定调用:(
我没有测试这一点。也许这就是您想要的)
但是在我看来,如果绑定相当具有挑战性,则需要三个级别。
编辑:这个怎么样?
The syntax you used obviously doesn't work. The first "::second" already signifies a non-static member and not a type. If you have a pair inside a pair you probably have to use two bind calls:
(I didn't test this. Perhaps this is what you want)
But three levels if bind is quite challenging, in my opinion.
Edit: How about this?