• Advertisement
Sign in to follow this  

[C++] std::find on a std::vector

This topic is 3390 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

namespace R2e {


class ArgList
{
  public:

    ArgList( int, char** );

    const std::string& operator[] ( int ) const;

    int find( std::string ) const;

    int& getVal( int&, std::string ) const;


  private:

    std::vector <std::string> m_list;

}; // class ArgList


} // namespace R2e

int
R2e::ArgList::
find( std::string term ) const
{
    std::vector <std::string>::iterator it;
    it = std::find( m_list.begin(), m_list.end(), term );

    if ( it == m_list.end() )
      return -1;

    return it - m_list.begin();
}

This code is not compiling.
-------------- Build: Debug in R2e ---------------

Compiling: R2e/arglist.cpp
/home/rob/Development/.codeblocks/R2e/R2e/arglist.cpp: In member function ‘int R2e::ArgList::find(std::string) const’:
/home/rob/Development/.codeblocks/R2e/R2e/arglist.cpp:49: error: no match for ‘operator=’ in ‘it = std::find [with _InputIterator = __gnu_cxx::__normal_iterator<const 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> > > > >, _Tp = std::string](((const R2e::ArgList*)this)->R2e::ArgList::m_list.std::vector<_Tp, _Alloc>::begin [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >](), ((const R2e::ArgList*)this)->R2e::ArgList::m_list.std::vector<_Tp, _Alloc>::end [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >](), ((const std::string&)((const std::string*)(& term))))’
/usr/include/c++/4.2/bits/stl_iterator.h:637: note: candidates are: __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> > > > >::operator=(const __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> > > > >&)
I'm a little stumped by the error message. I don't understand what I'm doing wrong... I saw some examples on the internet using std::find just like this... Why won't it work for me?
std::vector<foo> vec;
std::vector<foo>::iterator it;
it = std::find ( vec.begin(), vec.end(), bar );

Share this post


Link to post
Share on other sites
Advertisement
Your find function is const. That means your m_list is const. That means that only the const versions of begin() and end() are available. That means that you're passing const_iterators into std::find. That means that std::find returns a const_iterator. That means that you're trying to assign a const_iterator to an iterator.

Share this post


Link to post
Share on other sites
You have to use a std::vector <std::string>::const_iterator, because your member function is a const function.

EDIT: Beaten twice :D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement