Can std::iterator check, if the next element exist over that iterator?












-2















I have a template Iterator class that contains std::iterator of container that is specified by template. I did not find any way to check if the next element exist over the iterator, without using container.



There is a control like this;



vector<int> v; 
vector<int>::iterator itr;
if(itr== v.end()) { /*...*/}


but I wanted to do this control in my Iterator class and my class is like following...



  template <class E, class C= vector<E> >
class Iterator {
public:
/*...*/
bool hasNext()noexcept;
/*...*/
private:
typename C::iterator itr; // is there any problem with this decleration?
};

//implementation of hasNext() function.
template<class E, class C>
bool
Iterator<E,C>::hasNext()noexcept {
return(itr!=end())?true:false; // this line is wrong. How can I fix it?
}









share|improve this question




















  • 5





    You are free to do that if you want, but know that it won't be used by the standard functions that use iterators or even range-based for loops. You'll still need to provide a range for those purposes.

    – François Andrieux
    Dec 28 '18 at 18:08








  • 6





    This is a Java design. Importing Java notions into C++ hardly ever works well.

    – Pete Becker
    Dec 28 '18 at 18:12








  • 2





    No, you need a container to call it's end function. Your design is unfixable.

    – SergeyA
    Dec 28 '18 at 18:14








  • 1





    And to add up upon @SergeyA's comment an iterator implementation should be bound to a specific container instance.

    – πάντα ῥεῖ
    Dec 28 '18 at 18:15


















-2















I have a template Iterator class that contains std::iterator of container that is specified by template. I did not find any way to check if the next element exist over the iterator, without using container.



There is a control like this;



vector<int> v; 
vector<int>::iterator itr;
if(itr== v.end()) { /*...*/}


but I wanted to do this control in my Iterator class and my class is like following...



  template <class E, class C= vector<E> >
class Iterator {
public:
/*...*/
bool hasNext()noexcept;
/*...*/
private:
typename C::iterator itr; // is there any problem with this decleration?
};

//implementation of hasNext() function.
template<class E, class C>
bool
Iterator<E,C>::hasNext()noexcept {
return(itr!=end())?true:false; // this line is wrong. How can I fix it?
}









share|improve this question




















  • 5





    You are free to do that if you want, but know that it won't be used by the standard functions that use iterators or even range-based for loops. You'll still need to provide a range for those purposes.

    – François Andrieux
    Dec 28 '18 at 18:08








  • 6





    This is a Java design. Importing Java notions into C++ hardly ever works well.

    – Pete Becker
    Dec 28 '18 at 18:12








  • 2





    No, you need a container to call it's end function. Your design is unfixable.

    – SergeyA
    Dec 28 '18 at 18:14








  • 1





    And to add up upon @SergeyA's comment an iterator implementation should be bound to a specific container instance.

    – πάντα ῥεῖ
    Dec 28 '18 at 18:15
















-2












-2








-2


0






I have a template Iterator class that contains std::iterator of container that is specified by template. I did not find any way to check if the next element exist over the iterator, without using container.



There is a control like this;



vector<int> v; 
vector<int>::iterator itr;
if(itr== v.end()) { /*...*/}


but I wanted to do this control in my Iterator class and my class is like following...



  template <class E, class C= vector<E> >
class Iterator {
public:
/*...*/
bool hasNext()noexcept;
/*...*/
private:
typename C::iterator itr; // is there any problem with this decleration?
};

//implementation of hasNext() function.
template<class E, class C>
bool
Iterator<E,C>::hasNext()noexcept {
return(itr!=end())?true:false; // this line is wrong. How can I fix it?
}









share|improve this question
















I have a template Iterator class that contains std::iterator of container that is specified by template. I did not find any way to check if the next element exist over the iterator, without using container.



There is a control like this;



vector<int> v; 
vector<int>::iterator itr;
if(itr== v.end()) { /*...*/}


but I wanted to do this control in my Iterator class and my class is like following...



  template <class E, class C= vector<E> >
class Iterator {
public:
/*...*/
bool hasNext()noexcept;
/*...*/
private:
typename C::iterator itr; // is there any problem with this decleration?
};

//implementation of hasNext() function.
template<class E, class C>
bool
Iterator<E,C>::hasNext()noexcept {
return(itr!=end())?true:false; // this line is wrong. How can I fix it?
}






c++ stl iterator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 18:15









SergeyA

41.6k53783




41.6k53783










asked Dec 28 '18 at 18:06









husnuhusnu

63




63








  • 5





    You are free to do that if you want, but know that it won't be used by the standard functions that use iterators or even range-based for loops. You'll still need to provide a range for those purposes.

    – François Andrieux
    Dec 28 '18 at 18:08








  • 6





    This is a Java design. Importing Java notions into C++ hardly ever works well.

    – Pete Becker
    Dec 28 '18 at 18:12








  • 2





    No, you need a container to call it's end function. Your design is unfixable.

    – SergeyA
    Dec 28 '18 at 18:14








  • 1





    And to add up upon @SergeyA's comment an iterator implementation should be bound to a specific container instance.

    – πάντα ῥεῖ
    Dec 28 '18 at 18:15
















  • 5





    You are free to do that if you want, but know that it won't be used by the standard functions that use iterators or even range-based for loops. You'll still need to provide a range for those purposes.

    – François Andrieux
    Dec 28 '18 at 18:08








  • 6





    This is a Java design. Importing Java notions into C++ hardly ever works well.

    – Pete Becker
    Dec 28 '18 at 18:12








  • 2





    No, you need a container to call it's end function. Your design is unfixable.

    – SergeyA
    Dec 28 '18 at 18:14








  • 1





    And to add up upon @SergeyA's comment an iterator implementation should be bound to a specific container instance.

    – πάντα ῥεῖ
    Dec 28 '18 at 18:15










5




5





You are free to do that if you want, but know that it won't be used by the standard functions that use iterators or even range-based for loops. You'll still need to provide a range for those purposes.

– François Andrieux
Dec 28 '18 at 18:08







You are free to do that if you want, but know that it won't be used by the standard functions that use iterators or even range-based for loops. You'll still need to provide a range for those purposes.

– François Andrieux
Dec 28 '18 at 18:08






6




6





This is a Java design. Importing Java notions into C++ hardly ever works well.

– Pete Becker
Dec 28 '18 at 18:12







This is a Java design. Importing Java notions into C++ hardly ever works well.

– Pete Becker
Dec 28 '18 at 18:12






2




2





No, you need a container to call it's end function. Your design is unfixable.

– SergeyA
Dec 28 '18 at 18:14







No, you need a container to call it's end function. Your design is unfixable.

– SergeyA
Dec 28 '18 at 18:14






1




1





And to add up upon @SergeyA's comment an iterator implementation should be bound to a specific container instance.

– πάντα ῥεῖ
Dec 28 '18 at 18:15







And to add up upon @SergeyA's comment an iterator implementation should be bound to a specific container instance.

– πάντα ῥεῖ
Dec 28 '18 at 18:15














1 Answer
1






active

oldest

votes


















5














An iterator represents a position within a sequence of items. An iterator knows how to get to the next element in that sequence, but the nature of C++'s iterator model is based on the idea that there exists a "past-the-end" iterator after the end of such a sequence. Such an iterator does not represent a valid item in the sequence; it merely represents the end of the sequence, and iterators can be tested against it.



This construct is useful, as it allows you to talk about sub-ranges of elements within a sequence. For example, if a container has 10 elements, you can pass the begin/end iterators to the std::sort algorithm to sort them. However, you could also sort the first 10 elements by passing the begin iterator and the begin() + 10 iterator to the same function. The sort algorithm will treat the given ending iterator as the "past-the-end" iterator, not an iterator to a valid element.



What you are trying to do is combine two distinct ideas: position and range. Some iteration models do things that way, but that is not the STL-derived model that C++'s standard library is based on.



Now to be fair, there are some cases where position inherently carries range information. stream-based iterators know whether they are at the end of the stream or not, because to function as an iterator, they have to store a reference to the stream they iterate over. And the stream knows whether it is out of data. But overall, a single iterator is not meant to know whether it is at the end of its range or not.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53962598%2fcan-stditerator-check-if-the-next-element-exist-over-that-iterator%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    An iterator represents a position within a sequence of items. An iterator knows how to get to the next element in that sequence, but the nature of C++'s iterator model is based on the idea that there exists a "past-the-end" iterator after the end of such a sequence. Such an iterator does not represent a valid item in the sequence; it merely represents the end of the sequence, and iterators can be tested against it.



    This construct is useful, as it allows you to talk about sub-ranges of elements within a sequence. For example, if a container has 10 elements, you can pass the begin/end iterators to the std::sort algorithm to sort them. However, you could also sort the first 10 elements by passing the begin iterator and the begin() + 10 iterator to the same function. The sort algorithm will treat the given ending iterator as the "past-the-end" iterator, not an iterator to a valid element.



    What you are trying to do is combine two distinct ideas: position and range. Some iteration models do things that way, but that is not the STL-derived model that C++'s standard library is based on.



    Now to be fair, there are some cases where position inherently carries range information. stream-based iterators know whether they are at the end of the stream or not, because to function as an iterator, they have to store a reference to the stream they iterate over. And the stream knows whether it is out of data. But overall, a single iterator is not meant to know whether it is at the end of its range or not.






    share|improve this answer




























      5














      An iterator represents a position within a sequence of items. An iterator knows how to get to the next element in that sequence, but the nature of C++'s iterator model is based on the idea that there exists a "past-the-end" iterator after the end of such a sequence. Such an iterator does not represent a valid item in the sequence; it merely represents the end of the sequence, and iterators can be tested against it.



      This construct is useful, as it allows you to talk about sub-ranges of elements within a sequence. For example, if a container has 10 elements, you can pass the begin/end iterators to the std::sort algorithm to sort them. However, you could also sort the first 10 elements by passing the begin iterator and the begin() + 10 iterator to the same function. The sort algorithm will treat the given ending iterator as the "past-the-end" iterator, not an iterator to a valid element.



      What you are trying to do is combine two distinct ideas: position and range. Some iteration models do things that way, but that is not the STL-derived model that C++'s standard library is based on.



      Now to be fair, there are some cases where position inherently carries range information. stream-based iterators know whether they are at the end of the stream or not, because to function as an iterator, they have to store a reference to the stream they iterate over. And the stream knows whether it is out of data. But overall, a single iterator is not meant to know whether it is at the end of its range or not.






      share|improve this answer


























        5












        5








        5







        An iterator represents a position within a sequence of items. An iterator knows how to get to the next element in that sequence, but the nature of C++'s iterator model is based on the idea that there exists a "past-the-end" iterator after the end of such a sequence. Such an iterator does not represent a valid item in the sequence; it merely represents the end of the sequence, and iterators can be tested against it.



        This construct is useful, as it allows you to talk about sub-ranges of elements within a sequence. For example, if a container has 10 elements, you can pass the begin/end iterators to the std::sort algorithm to sort them. However, you could also sort the first 10 elements by passing the begin iterator and the begin() + 10 iterator to the same function. The sort algorithm will treat the given ending iterator as the "past-the-end" iterator, not an iterator to a valid element.



        What you are trying to do is combine two distinct ideas: position and range. Some iteration models do things that way, but that is not the STL-derived model that C++'s standard library is based on.



        Now to be fair, there are some cases where position inherently carries range information. stream-based iterators know whether they are at the end of the stream or not, because to function as an iterator, they have to store a reference to the stream they iterate over. And the stream knows whether it is out of data. But overall, a single iterator is not meant to know whether it is at the end of its range or not.






        share|improve this answer













        An iterator represents a position within a sequence of items. An iterator knows how to get to the next element in that sequence, but the nature of C++'s iterator model is based on the idea that there exists a "past-the-end" iterator after the end of such a sequence. Such an iterator does not represent a valid item in the sequence; it merely represents the end of the sequence, and iterators can be tested against it.



        This construct is useful, as it allows you to talk about sub-ranges of elements within a sequence. For example, if a container has 10 elements, you can pass the begin/end iterators to the std::sort algorithm to sort them. However, you could also sort the first 10 elements by passing the begin iterator and the begin() + 10 iterator to the same function. The sort algorithm will treat the given ending iterator as the "past-the-end" iterator, not an iterator to a valid element.



        What you are trying to do is combine two distinct ideas: position and range. Some iteration models do things that way, but that is not the STL-derived model that C++'s standard library is based on.



        Now to be fair, there are some cases where position inherently carries range information. stream-based iterators know whether they are at the end of the stream or not, because to function as an iterator, they have to store a reference to the stream they iterate over. And the stream knows whether it is out of data. But overall, a single iterator is not meant to know whether it is at the end of its range or not.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 28 '18 at 18:19









        Nicol BolasNicol Bolas

        283k33468643




        283k33468643






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53962598%2fcan-stditerator-check-if-the-next-element-exist-over-that-iterator%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas