dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,481

    [RESOLVED] std::vector iterators

    I'm compiling a header file which contains the following function...

    Code:
    		PamPort * find_port (const std::string& port_name) const {
    			for (std::vector<PamPort*>::const_iterator it = _ports.begin (); it != _ports.end (); ++it) {
    				if ((*it)->name () == port_name) {
    					return *it;
    				}
    			}
    When I run this in the debugger, MSVC complains about incompatible iterator types. The only thing I could see was that it uses a const_iterator but then tries to return the result as non-const. So I changed the iterator to std::vector<PamPort*>::iterator it

    Unfortunately, this seems to have broken the compilation

    error C2440: 'initializing' : cannot convert from 'std::_Vector_const_iterator<_Ty,_Alloc>' to 'std::_Vector_iterator<_Ty,_Alloc>'
    _ports is declared as std::vector<PamPort *> _ports; so I'm not sure why it won't work with a non-const interator. What am I not seeing
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,314

    Re: std::vector iterators

    Code:
    PamPort * find_port (const std::string& port_name) const {
        for (const auto& pn : _ports)
            if (pn->name() == port_name)
                return pn;
    }
    would be a better way if your compiler will let you.

    What am I not seeing?
    find_port() is part of a class that also defines _port - yes?

    find_Port() is declared as const so it can't change the contents of the class variables - so the iterators must be constant

    begin() returns an iterator - not a const iterator, So that is where the incompatible types come. If your compiler supports it then

    Code:
    PamPort * find_port (const std::string& port_name) const {
    			for (auto it = _ports.cbegin (); it != _ports.cend (); ++it) {
    				if ((*it)->name () == port_name) {
    					return *it;
    				}
    			}
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.5)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,481

    Re: std::vector iterators

    Good explanation 2kaud, thanks !
    "A problem well stated is a problem half solved. - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)