Thread: Function Definitions with Iterators

  1. #1
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401

    Function Definitions with Iterators

    I've got a specialized container that holds objects of class S. I've got the usual void add(S) and bool remove(S) functions. I'd like to add functions to add and remove a range of elements. For example, something like void add_range(InputIterator<S> begin, InputIterator<S> end) and int remove_range(InputIterator<S> begin, InputIterator<S> end).

    I found this link that helped me understand how to define functions that take iterators. However, doing it that way doesn't restrict my range functions to take only InputIterators that have a value_type of S. How can I define a function to take InputIterators with a value_type of S?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is S a specific thing, or is that your template argument? It's not really clear to me exactly what you're worried about. Can't your specialized container define an iterator type (which the standard containers do, after all) and you use that?

    (EDIT: I mean to say, it looks like you're trying to write a function for this specific container, not for any possible container that could ever exist. I don't know, just by looking at the idea of "remove_range", why you care about what S is, when really the important thing would be how the container works.)
    Last edited by tabstop; 05-17-2010 at 09:53 AM.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    S is a specific class, not a template argument.

    Here's an example of what I'm trying to do:
    Code:
    class S 
    { 
       //class definition
    };
    
    class special_collection 
    { 
       //class definition
       //only holds objects of class S
       void add_range(InputIterator<S> begin, InputIterator<S> end);
    };
    
    void do_stuff()
    {
       vector<S> vec;
       special_collection col;
       // add some S's to vec
       col.add_range(vec.begin(), vec.end());
       // do some other stuff
    }
    While the special_collection will definitely have its own iterator class, it's not useful to define add_range in terms of that iterator class because the iterators can come from anywhere, not just the special_collection. The only restrictions I want to put on the iterator arguments for add_range and remove_range are that they are InputIterators (or at least, not OutputIterators) and have a value_type of S.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If I'm understanding what you want, what you are trying to do is not enforced by the type of the parameters as such. You define
    Code:
    template <class InputIterator>
    void add_range(InputIterator start, InputIterator stop)
    and at some point in that function you do
    Code:
    InputIterator foo = start;
    while (foo != stop) {
        push_back(*foo)
    and if your InputIterators were not of type S, then this will fail.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    While researching in the interim, I kept getting nudges in that direction, but it never really clicked until now. So basically you're saying that I should use InputIterator as if it is an InputIterator with a value_type of S. If someone tries to use something else, the compiler will throw an error eventually.

    Since I'm using Boost, it looks like I can throw less ambiguous errors if I use BOOST_MPL_ASSERT to do the type checking.

    Thanks for the answer.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, basically, you would use it as if it as iterator. So long as you use the standard iterator interface, it will work with any type of iterator, S and otherwise.
    Passing in a non-iterator will cause a compile error, naturally. That's the beauty if the static type system.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-24-2009, 02:42 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  4. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  5. help writing function definitions
    By jlmac2001 in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2003, 09:44 PM