Search code examples
c++c++11compiler-errorsauto

Error in a function used to find the position of an element in a vector of string (c++)


I have the following function that works fine that I use to find the position of a string in a string vector:

int FindIndexString(vector <string> *Names, string *name)
{
    auto it = find(Names->begin(), Names->end(), name->c_str());
    if (it == Names->end())
    {
         error("FindIndexString: %s not found",name->c_str());
    }else{
        auto index = distance(Names->begin(), it);
        return((int)index);
    }
}

I need to use this function in a R program that complains about the "auto" specifier and I cannot compile with C++11.

I changed the function to

int FindIndexString(vector <string> *Names, string *name)
 {
     vector<string>::iterator it;
     it = find(Names->begin(), Names->end(), name->c_str());

     int pos = (int)distance(Names->begin(), it);

     if(it==Names->end())
     {
          error("FindIndexString: %s not found",name->c_str());;
     }else{
         return(pos);
     }    
  }

but it doesn't work and I get the following error

In function 'int FindIndexString(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >*, std::string*)':
F_Utils.cpp:92: error: no matching function for call to 'find(__gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, __gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, const char*)'

Any help?


Solution

  • You need to #include <algorithm> for std::find