Sign in to follow this  

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

This topic is 3315 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
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

This topic is 3315 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this