【问题标题】:Boost bind and assign to convert vector to a stringBoost bind 和 assign 将向量转换为字符串
【发布时间】:2014-08-11 12:42:38
【问题描述】:

假设我有以下容器:

vector<string> input = assign::list_of("one")("two")("three")("four");
vector<map<string, int> > result;

并说我希望结果看起来像:

{{"one", 1}, {"two", 1}, {"three", 1}, {"four", 1}}

我想使用 STL 算法,我认为 transform 或 for_each 都可以。 对于转换,我有代码:

transform(input.begin(), input.end(), back_inserter(result), boost::bind(assign::map_list_of(_1, 1)));

但这会产生一个编译错误,类似于 no type named 'result_type' in 'class boost::assign_detail::generic_list, int> >'

对于 for_each 我有代码:

    for_each(input.begin(), input.end(), 
        boost::bind<void, void(std::vector<std::map<std::string, int> >::*)(const map<string, int>&)>(
            &std::vector<std::map<std::string, int> >::push_back, 
            &result, 
            assign::map_list_of(_1, 1)));

但这会产生一个编译错误,总结如下: no match for call to '(boost::_mfi::dm, int>&), std::vector, int> >) (std: :vector, int> >*&, boost::assign_detail::generic_list, int> >&)'

这样做的正确方法是什么?请注意,我不能使用 C++11,我想将 boost_bind 与 STL 算法结合使用,只是为了了解有关 boost::bind 的更多信息。


WRT @Joachim 关于调用 map_list_of 的评论,我做了如下修改:

for_each(input.begin(), input.end(),
        boost::bind<void, void(std::vector<std::map<std::string, int> >::*)(const map<string, int>&)>(
            &std::vector<std::map<std::string, int> >::push_back,
            &result,
            boost::bind<void, map<string, int>(const string&, int)>(&assign::map_list_of, _1, 1));

产生编译错误:cannot convert '& boost::assign::map_list_of' (type '') to type 'std::map, int> (*)(const std::basic_string&, int )'

【问题讨论】:

  • 顺便说一句,您在boost::bind 调用中使用boost::assign::map_list_of 是错误的,因为您实际上调用 map_list_of 函数,将其结果作为函数传递调用(这当然会给你错误)。

标签: c++ boost boost-bind st


【解决方案1】:

试试这个:

#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <vector>
#include <map>
#include <string>

int main()
{
    std::vector<std::string> input = boost::assign::list_of("one")("two")("three")("four");
    std::vector<std::map<std::string, int> > result;

    for_each
    (
        input.begin()
      , input.end()
      , boost::bind
        (
            static_cast<void(std::vector<std::map<std::string, int> >::*)(const std::map<std::string, int>&)>(&std::vector< std::map<std::string, int> >::push_back)
          , &result
          , boost::bind(&boost::assign::map_list_of<std::string, int>, _1, 1)
        )
    );    

    return 0;    
}

【讨论】:

  • 您应该在#includes 中使用正斜杠。
【解决方案2】:
BOOST_FOREACH(const std::string& str, input)
  result[str] = 1;

我意识到这不使用 STL 算法和 Boost.Bind,但它简洁直观。

【讨论】:

  • 但是result 不是地图,而是矢量。
  • 这并没有回答问题,因为我明确表示我想使用 STL 算法并绑定。
  • @Tom:好吧,如果你能告诉我们更多关于你为什么有这种愿望的信息,我们可以为你提供更多帮助。否则,很难有动力去搞砸一个完美的“for”循环。
  • @JoachimPileborg:你是对的,但是 OP 的示例也没有真正显示地图矢量,所以....我想我们可以使用 1 构造结果然后执行 @987654325 @。从这个问题中并不完全清楚。
  • @Tom 如果您告诉我们您实际尝试使用解决方案解决的问题,这可能会有所帮助。可能还有其他甚至更好的解决方案。如果只是为了好玩和学习也没关系,但是你至少应该在问题中提到它。只是说“我有这个解决方案,请帮助我让它工作”,没有问题描述对我们试图帮助你的人来说没有多大帮助。您需要帮助我们来帮助您。 :) 如果您还没有这样做,请阅读the XY problem
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 2021-09-19
  • 2013-07-09
  • 1970-01-01
  • 2020-02-27
相关资源
最近更新 更多