Thread: help with iterators

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    13

    help with iterators

    Hello,


    I have a node defined as

    Code:
    struct node
    {  
       string data;
       int val;
    
    }
    I have a vector<node> and a list<node>.

    Code:
    vector<node> vectorName{/* initialized to some value */};
    list<node> listName{/* initialized to some value */};

    I want to run find() on the list<node> for every node in the vector<node>.

    something like

    Code:
    for(auto itr = vectorName.begin(); itr != vectorName.end(); ++itr)
    
    {
    auto item = find(listName.begin(), listName.end(), *itr);
    
    // do something with item
    }
    but I keep getting a compiler error when I try to compile. Looks like I am doing something wrong in the find() call. I understand that itr is pointing to a node in the vector. but shouldn't I be able to pass *itr which is just a node as the third argument to find()?

    This is the error I am getting

    error C2678: binary '==': no operator found which takes a left-hand operand of type 'node' (or there is no acceptable conversion)

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    You need to define how to compare nodes. Something like:
    Code:
    bool operator==(const node& a, const node& b) {
        return a.data == b.data && a.val == b.val;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iterators?
    By Warrax in forum C++ Programming
    Replies: 6
    Last Post: 06-11-2007, 03:13 PM
  2. iterators
    By linucksrox in forum C++ Programming
    Replies: 5
    Last Post: 03-12-2006, 06:46 PM
  3. iterators
    By Raven Arkadon in forum C++ Programming
    Replies: 6
    Last Post: 06-28-2005, 05:44 PM
  4. iterators
    By strickey in forum C++ Programming
    Replies: 10
    Last Post: 02-15-2005, 12:45 PM
  5. iterators
    By /Muad'Dib\ in forum C++ Programming
    Replies: 23
    Last Post: 06-12-2004, 12:30 PM

Tags for this Thread