iterator as argument to function

Discussion in 'C++' started by nvangogh, May 16, 2014.

  1. nvangogh

    nvangogh Guest

    I am looking at an exercise that has the following problem:
    "Write a function that takes a pair of iterators to a vector<int> and an
    int value. Look for that value in the range and return a bool indicating
    whether it was found".

    So this is what I wrote:

    #include <iostream>
    #include <vector>

    // function declaration
    bool std::vector<int>myvec (iterator begin, iterator end, int);

    int main()
    std::cout << "Input a number for the first element in the range: " ;
    int num1;
    std::cin >> num1;
    std::cout << "Input a number for the second element in the range: ";
    int num2;
    std::cin >> num2;
    std::cout << "Input any integer value to see if it is in the vector:";
    int val;
    std::cin >> val;

    bool result = myvec(num1,num2,val);
    std::cout << result << std::endl;
    return 0;

    // function definition
    bool std::vector<int> myvec (iterator begin, iterator end, int val)
    bool result = 0;
    for(auto i: myvec)
    if (i == val)
    result = 1;
    result = 0;
    return result;
    but I get the following errors when I try to compile it:
    I need some help please to complete this exercise
    nvangogh, May 16, 2014
    1. Advertisements

  2. What is "std::vector<int>" doing before the name of the function? The
    type of the return value is "bool", then name of the function is
    "myvec", that's fine. So the declaration of the function should start with

    bool myvec(

    .. Also, what's "iterator"? Have you defined that type? Where?
    Again, what that thing after 'bool' and before 'myvec'?
    'myvec' is the name of the function, not the name of the collection.
    You can't use it here.
    You clearly do. What help do you think you need? I could write it for
    you, and so could pretty much anybody here. Is that something you'd want?

    There are so many problems with the outcome of your attempt that I am at
    a loss where to start...

    An iterator is not an index. It's a special object that allows you to
    access the elements of the collection (vector in your case) in some
    known way. It's an interface of sorts that designates an element of the
    sequence, and also can be made to designate the next or the previous
    element (using the increment or decrement operator) and in some cases
    can be randomly "repositioned" to designate some other element, not
    immediately adjacent.

    Every collection defines its own type that is an iterator for that
    collection. For instance, std::set defines 'iterator' type, and to use
    it you would write std::set<arguments>::iterator (where 'arguments' is
    the list of arguments you supply to instantiate the 'set' template, for
    instance, for a set of strings it would be
    std::set<std::string>::iterator ).

    What book are you reading that doesn't explain those concepts?

    If you're reading any specific book, you might think that you've
    understood those aspects, but this post shows that you most likely
    missed quite a lot. I strongly advise you to go back to the beginning
    of the chapter that deals with collections.

    There are some holes in your comprehension of functions and types, too.
    Perhaps you need to revisit (and do some exercises on) those topics as

    Victor Bazarov, May 16, 2014
    1. Advertisements

  3. nvangogh

    nvangogh Guest

    Alright sorry to bother you. I will look in my book.
    nvangogh, May 16, 2014
  4. ITYM typedef std::vector<int>::iterator iter;


    OP -- look up documentation for std::find. The textbook is asking you to
    implement a specialization of find for a vector of ints (and return a bool instead of an iterator).

    - Anand
    Anand Hariharan, May 19, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.