访问嵌套对

发布于 2024-08-06 00:43:38 字数 917 浏览 1 评论 0原文

要拆开一对,可以执行以下操作。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲欢浪云 2024-08-13 00:43:38

不确定我是否了解那里发生的事情,但通常当我开始遇到绑定问题时,我会放弃并实现一个函子。这可能会简化您的情况。

对我来说,下面的内容比那些乱七八糟的多级绑定更容易阅读

template <class Arg>
struct Whatever : public std::unary_function<Arg, bool>
{
    bool operator()(const Arg &arg1) const {
        //logic here
    } 
};

Whatever<???> functor;
std::partition(Staff.begin(), Staff.end(), functor);

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

template <class Arg>
struct Whatever : public std::unary_function<Arg, bool>
{
    bool operator()(const Arg &arg1) const {
        //logic here
    } 
};

Whatever<???> functor;
std::partition(Staff.begin(), Staff.end(), functor);
临走之时 2024-08-13 00:43:38

您使用的语法显然不起作用。第一个“::second”已经表示非静态成员而不是类型。如果一对内有一对,您可能必须使用两个绑定调用:(

typedef std::pair< int, bool > foo_t;
typedef std::pair< int, foo_t > bar_t;
.....

bind( &foo_t::second, bind(&bar_t::second,
    bind( &USER_MAP::find, _1 )
) )

我没有测试这一点。也许这就是您想要的)

但是在我看来,如果绑定相当具有挑战性,则需要三个级别。

编辑:这个怎么样?

template<typename Iter, typename Return>
struct deref_2nd_2nd : std::unary_function<Iter, Return> {
    Return operator()(Iter const& it) const {
        return (*it).second.second;
    }
};

.....

bind(
    deref_2nd_2nd<USER_MAP::iterator,bool>(),
    bind( &USER_MAP::find, _1 )
)

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:

typedef std::pair< int, bool > foo_t;
typedef std::pair< int, foo_t > bar_t;
.....

bind( &foo_t::second, bind(&bar_t::second,
    bind( &USER_MAP::find, _1 )
) )

(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?

template<typename Iter, typename Return>
struct deref_2nd_2nd : std::unary_function<Iter, Return> {
    Return operator()(Iter const& it) const {
        return (*it).second.second;
    }
};

.....

bind(
    deref_2nd_2nd<USER_MAP::iterator,bool>(),
    bind( &USER_MAP::find, _1 )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文