Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6146503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:57:32+00:00 2026-05-23T18:57:32+00:00

I am new to C++ so please bear with me. I am trying to

  • 0

I am new to C++ so please bear with me. I am trying to understand STL iterator_traits. In the book "The C++ Standard Library" the structure iterator_traits is defined as follows:

template <class T>
struct iterator_traits {
  typedef typename T::value_type value_type;
  typedef typename T::difference_type difference_type;
  typedef typename T::iterator_category iterator_category;
  typedef typename T::pointer pointer;
  typedef typename T::reference reference;
};

So it seems to me that it is re-exposing the subtypes that T already exposes. Moving ahead further, the book gives an example of how to use it, which is something like the following

template <class MyIterator>
void do_something(MyIterator start, MyIterator end) {
    typedef typename iterator_traits<MyIterator>::value_type value_type; 
    value_type v = *start;
    .....
}

My question is why do I need this iterator_traits structure here, if the idea was to obtain the value_type, couldn’t I have obtained it from MyIterator directly ? My confusion seems to arise from my (surely incorrect) understanding that the information of the subtypes have to be sourced from the template <class T> used to instantiate the iterator_trait. So if you could explain, and preferably with an example why and where would I need iterator_traits that would be very helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:57:34+00:00Added an answer on May 23, 2026 at 6:57 pm

    Pointers into an array can be used as random access iterators.

    There needs to be some consistent way to get these types both for pointers (which obviously can’t have the types declared as nested types, since only classes can have nested types) and for class-type iterators. The traits class template provides this consistent way.

    The iterator_traits class template is specialized for pointers like so:

    template <typename T>
    struct iterator_traits<T*>
    {
        typedef std::random_access_iterator_tag iterator_category;
        typedef T                               value_type;
        typedef T*                              pointer;
        typedef T&                              reference;
        typedef std::ptrdiff_t                  difference_type;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please bear with me, I'm new at C and I'm trying to program an
I'm new to Jquery, so please bear with me. I'm trying to create a
I'm new to jQuery, so please bear with me. I'm trying to make my
I am brand new to javascript so please bear with me. I am trying
I'm new to both git and unix so please bear with. I'm trying to
I'm pretty new to PHP so please bear with me. I'm trying to log
I'm fairly new to the whole android-scene so please bear with me. I'm trying
I'm pretty new to this so please bear with me. I'm trying to make
I'm very new to WPF, so please bear with me. Basically I have defined
I'm relatively new to C#, so please bear with me. I am trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.