2
votes

Have piece of code to scan large string (html) and then parse any words out of it. Push all the instances to vector or pairs (word and count) in case it's not there already, otherwise increment count (second element of pair).

vector < pair <string,int> > vect; 
vector < pair <string,int> >::iterator it;

    ...

it = find_if (vect.begin(), vect.end(), currentword);
if (it != vect.end())
        it->second++;
else 
    vect.push_back( make_pair(currentword, 1));
    ...

Getting compilation error:

In file included from C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/bits/stl_algobase.h:71:0,
                 from C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/bits/char_traits.h:39,
                 from C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/ios:40,
                 from C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/ostream:38,
                 from C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/iostream:39,
                 from webCounter.cpp:19:
C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_pred::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator, int>*, std::vector, int> > >; _Predicate = std::__cxx11::basic_string]':
C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/bits/stl_algo.h:120:14:   required from '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator, int>*, std::vector, int> > >; _Predicate = __gnu_cxx::__ops::_Iter_pred >]'
C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/bits/stl_algo.h:161:23:   required from '_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator, int>*, std::vector, int> > >; _Predicate = __gnu_cxx::__ops::_Iter_pred >]'
C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/bits/stl_algo.h:3815:28:   required from '_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator, int>*, std::vector, int> > >; _Predicate = std::__cxx11::basic_string]'
webCounter.cpp:68:58:   required from here
C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/bits/predefined_ops.h:234:30: error: no match for call to '(std::__cxx11::basic_string) (std::pair, int>&)'
  { return bool(_M_pred(*__it)); }
1

1 Answers

4
votes

The problem is there is no built-in comparison between a pair <string,int> and what appears to be a string. You will have to provide one. Eg.

it = find_if (vect.begin(), 
              vect.end(), 
              [currentword](const pair <string,int>& p){ // this is a lambda expression
                  return p.first == currentword; // compare strings
              });

Documentation for Lambda Expressions

That said,

std::map<string, int> freqmap;

could be a better choice for this task if it is allowed. Just about all of the code reduces to

freqmap[currentword]++;

Documentation for std::map