Transform with Lambda function not doing what i want it to C++

The idea is to have a map represent a vector of words + how many times they occur in that vector. Note that I am not allowed to use any loops or for_each.

I don't think the compiler likes that lambda function, what am i doing wrong?

FUNCTION:

map<string, int> CreateMap(vector<string> v)
{
  sort(v.begin(), v.end());
  auto last = unique(v.begin(), v.end());
  map<string,int> m;
  transform(v.begin(), last, v.begin(), inserter(m, m.begin()),
            [&v](string const& x){return make_pair(x, count(v.begin(), v.end(), x));});
  return m;
}

COMPILE ERROR:

In file included from /usr/include/c++/7/algorithm:62:0,
                 from Ordlista.cc:3:
/usr/include/c++/7/bits/stl_algo.h: In instantiation of ‘_OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation) [with _IIter1 = __gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >; _IIter2 = __gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >; _OIter = std::insert_iterator<std::map<std::__cxx11::basic_string<char>, int> >; _BinaryOperation = CreateMap(std::vector<std::__cxx11::basic_string<char> >)::<lambda(const string&)>]’:
Ordlista.cc:102:86:   required from here
/usr/include/c++/7/bits/stl_algo.h:4345:25: error: no match for call to ‘(CreateMap(std::vector<std::__cxx11::basic_string<char> >)::<lambda(const string&)>) (std::__cxx11::basic_string<char>&, std::__cxx11::basic_string<char>&)’
  *__result = __binary_op(*__first1, *__first2);
              ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Ordlista.cc:102:33: note: candidate: CreateMap(std::vector<std::__cxx11::basic_string<char> >)::<lambda(const string&)>
             [&v](string const& x){return make_pair(x, count(v.begin(), v.end(), x));});

Upvotes: 0

Views: 368

Answers (1)

cigien
cigien

Reputation: 60208

You could construct a map quite easily using accumulate, like this:

std::map<std::string,int> CreateMap(std::vector<std::string> v)
{
  return std::accumulate(std::begin(v), std::end(v), 
                         std::map<std::string,int>{},
                         [](auto &m, auto &s) -> auto& { 
                           m[s]++; 
                           return m;
         });
}

Upvotes: 1

Related Questions