I didn't know why the type name exists in the template <class Itter> I defined myself.

Asked 1 years ago, Updated 1 years ago, 389 views

The following implementation was written as the implementation of the iterator_trites structure.
Why are there differential_type and value_type types in the namespace of the Iter class?
In my opinion, when it comes to template, I understand that it is only the template type name, but I wonder if there is a type such as difference_type among the Iter that I defined as template.
I would appreciate it if you could let me know if you understand.

structorator_trains{
                typeef typenameIter::differences_typedifferences_type;
                typeef typenameIter::value_type value_type;
                typeef typenameIter::pointer pointer;
                typeef typenameIter::reference reference;
                typeef typenameIter::iterator_categoryiter_category;
                /*
                 * DIFFERENCE_TYPE ITERATOR DIVISION OPERATION TYPE
                 * value_type The value type pointed to by the iterator
                 * pointer type of pointer iterator
                 * Reference type for reference iterator
                 * categorical types of iterator_category iterator
                 */
            };```

c++

2022-12-25 01:09

1 Answers

It is necessary to define a template class or function template to do something with an unknown Iter.
For example, the following page defines a sum function template that calculates the total value, but the value type value_type pointed to by the iterator Iterator (which may or may not be int).

https://cpprefjp.github.io/reference/iterator/iterator_traits.html

// Calculate the total value of the range
template<class Iterator>
typename std::iterator_trats<Iterator>:value_type
    sum (Iterator first, Iterator last)
{
  // get the value type from the iterator type
  using value_type=typename std::iterator_trats<Iterator>:value_type;

  // Define variables for the retrieved value type and calculate the total value
  value_type result=value_type();
  for(;first!=last;++first){
    result+=*first;
  }
  return result;
}


2022-12-25 01:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.