Thread: Explicitly specifying iterator types

  1. #1
    Registered User
    Join Date
    Jul 2014
    Location
    Calgary, Canada
    Posts
    29

    Explicitly specifying iterator types

    Suppose that we want to use iterators to loop over the elements of an std::unordered_map<T, U>. The iterators will point to elements of type std:: pair<const T, U>.

    I know you can do it like this by using ::const_iterator (apologies for this code not being entirely of the cut-and-paste variety):

    Code:
      
      typedef std::tuple<int, int> TupleII;
      std::unordered_map<Key<TupleII>, Sheet<TupleII>> input;
    
      (... do something to fill in the values of "input"...)
    
      for (typename std::unordered_map<Key  <TupleII>, 
                                       Sheet<TupleII>>::const_iterator
           s=input.cbegin(); s!=input.cend(); ++s)
      {
        printf("%s\n", ascii(s->second).c_str());
      }
    or like this, using range-based for loops (leaving out the first few lines from above):

    Code:
      for (const auto& s : input)
      {
        printf("%s\n", ascii(s.second).c_str());
      }
    Here "ascii" is just some function to print values to the screen.

    My question is: Is there a way to explicitly declare the iterator "s" as an object of the iterator type of the class involved? What I mean is something along the lines of

    Code:
      for (const std::pair<const Key<TupleII>, Sheet<TupleII>>
           s=input.cbegin(); s!=input.cend(); ++s)
    
      {
        printf("%s\n", ascii(s.second).c_str());
      }
    ...but which would actually work. As coded here, it doesn't.

    I know that the range-based "for" is the preferable option here. I just wondered whether this could be done.
    Last edited by Grumpulus; 12-04-2014 at 06:50 PM.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    It can be done, how do you think they did it before 'auto' keyword existed? In your example though your iterator definition should be something like this:
    Code:
    std::unordered_map<TupleII, TupleII>::iterator iter;
    Which is why people tend to use typedefs or auto or decltype() to determine complex types automatically without having to type them out which is error prone.

  3. #3
    Registered User
    Join Date
    Jul 2014
    Location
    Calgary, Canada
    Posts
    29
    Aaaargh!!! You're right, I botched the declared type of "input" when constructing that code fragment whilst copy-pasting... fixed. It should make sense now.

    Key and Sheet are just a couple of my own templates. That should be obvious but I forgot to mention it before.
    Last edited by Grumpulus; 12-04-2014 at 06:59 PM.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    No you can't define it as the resultant type that the iterator RETURNS on each iteration.

    The iterator handles the glue code job of dealing with how to actually iterate over the unordered_map (in this case). Declaring a std::pair of the items stored inside the map doesn't give you an iterator, it just gives you a std::pair. A std::pair<...,...> knows nothing about how to iterate over your std::unordered_map...make sense?

  5. #5
    Registered User
    Join Date
    Jul 2014
    Location
    Calgary, Canada
    Posts
    29
    Yeah, that's exactly what I was trying to ask, although I think your answer makes the question more clear.

    I get it now. Thanks for this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clarify explicitly calling destructors
    By monkey_c_monkey in forum C++ Programming
    Replies: 7
    Last Post: 07-19-2012, 12:16 PM
  2. When to handle memory explicitly?
    By Walle in forum C++ Programming
    Replies: 18
    Last Post: 01-12-2010, 07:19 PM
  3. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  4. Passing arrays explicitly
    By drrngrvy in forum C++ Programming
    Replies: 9
    Last Post: 02-01-2006, 11:53 AM
  5. Explicitly calling destructor.
    By anonytmouse in forum C++ Programming
    Replies: 6
    Last Post: 03-11-2004, 01:51 AM