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 6052601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:56:47+00:00 2026-05-23T07:56:47+00:00

To my knowledge, the hierarchy of iterator categories goes like this: Random access ->

  • 0

To my knowledge, the hierarchy of iterator categories goes like this:

Random access -> Bi-directional -> Forward -> Input
                                           -> Output

Correct?

I always thought there was a rule, that if an algorithm expects a particular type of iterator, you can provide iterators of categories up the chain, but not down. So I was reading this answer, where ildjarn suggests suggested (then later corrected himself) using std::ifstream with std::istream_iterator and std::search to find data in a in a file. I was about to comment that you can’t do that, because search expects Forward iterators, and istream_iterator is an Input iterator. But just to make sure, I tried this:

std::istringstream iss("Elephant hats for sale.");
std::istream_iterator<char> begin(iss), end;

std::string sub("hat");
auto i = std::search(begin, end, sub.begin(), sub.end());

I didn’t expect it to compile, but it did. However, the results seem to be useless because if I follow it with this:

while(i != end)
{
    std::cout << *i;
    ++i;
}

There is no output. So, my question is this: Is my compiler in error for allowing my call to search using istream_iterator? Or are there no rules preventing this sort of thing?

  • 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-23T07:56:47+00:00Added an answer on May 23, 2026 at 7:56 am

    Can input iterators be used where forward iterators are expected?

    No. The difference between an input iterator and a forward iterator is that an input iterator is a “single-pass” iterator but a forward iterator is a “multi-pass” iterator.

    Once you advance an input iterator, you can no longer access the previous elements in the range. If you make a copy of an input iterator, both iterators remain valid until you advance one of them; then the other ceases to be valid.

    With a forward iterator, you can iterate over the sequence any number of times, you can have multiple usable copies of an iterator at once, you can use multiple iterators into the sequence at the same time, and you can dereference an iterator as many times as you’d like before advancing it again.

    So, my question is this: Is my compiler in error for allowing my call to search using istream_iterator?

    There is no rule that the compiler must reject the code.

    The rule is that you must be sure to pass the right type of iterator that is required by the function. Sometimes if you pass the wrong type of iterator you get a compilation error. Sometimes the program will compile but will not function correctly. Sometimes things will appear to work correctly. The results are undefined if you violate the requirements of calling the function.


    Generic algorithms usually impose requirements on their type parameters by assuming that the type arguments provided actually meet the requirements. So, for example, an algorithm that only works with random access iterators will “enforce” this requirement by performing some operation that only works with random access iterators (e.g. it + 1). If the iterator doesn’t support that operation (operator+(iterator, int) here), the code will fail to compile.

    The problem is that there is no way to distinguish between input iterators and forward iterators this way: you can increment and dereference both of them; the difference is in how many times you can perform each of those operations and the sequence in which you can perform those operations. So, an algorithm like std::search will use *it and ++it, which will “work” just fine for input iterators, at least insofar as the code will compile.

    In theory, an algorithm could use the std::iterator_traits class template to determine whether an iterator is an input iterator or a forward iterator; I don’t know whether that would be permitted by the C++ language standard. If the library did that, you could get a compilation error for your code, which would be better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My SQL knowledge is very limited, specially about SQLite, although I believe this is
Is good knowledge of PHP needed to make an installable template for CMS like
My knowledge of code just doesn't go this deep, and I have no idea
I don't know if this is possible or not, or if my limited knowledge
My knowledge of matlab is merely on a need to know basis, so this
It is almost common knowledge that one should always compile with -Wall . What
(Probably just knowledge of the Unix library is enough to answer this, so please
Scenario: There is a hierarchy of PROCESS . Like PR-2, PR-3 are children of
My programming knowledge is very limited, I would really appreciate any help on this
My PHP knowledge is limited and I am trying to implement http://www.tutorialchip.com/php-download-file-script/ this script

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.