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;
    }
    else
    result = 0;
    return result;
    }
    ---------
    but I get the following errors when I try to compile it:
    > g++ -std=c++11 -o exercise9.4 exercise9.4.cpp
    > exercise9.4.cpp:5:23: error: invalid declarator before ‘myvec’
    > exercise9.4.cpp: In function ‘int main()’:
    > exercise9.4.cpp:19:36: error: ‘myvec’ was not declared in this scope
    > exercise9.4.cpp: At global scope:
    > exercise9.4.cpp:25:23: error: invalid declarator before ‘myvec’


    I need some help please to complete this exercise
     
    nvangogh, May 16, 2014
    #1
    1. Advertisements

  2. On 5/16/2014 9:36 AM, nvangogh wrote:
    > 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);


    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?

    > 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)


    Again, what that thing after 'bool' and before 'myvec'?

    > {
    > bool result = 0;
    > for(auto i: myvec)


    'myvec' is the name of the function, not the name of the collection.
    You can't use it here.

    > {
    > if (i == val)
    > result = 1;
    > }
    > else
    > result = 0;
    > return result;
    > }
    > ---------
    > but I get the following errors when I try to compile it:
    >> g++ -std=c++11 -o exercise9.4 exercise9.4.cpp
    >> exercise9.4.cpp:5:23: error: invalid declarator before ‘myvec’
    >> exercise9.4.cpp: In function ‘int main()’:
    >> exercise9.4.cpp:19:36: error: ‘myvec’ was not declared in this scope
    >> exercise9.4.cpp: At global scope:
    >> exercise9.4.cpp:25:23: error: invalid declarator before ‘myvec’

    >
    > I need some help please to complete this exercise


    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
    well.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 16, 2014
    #2
    1. Advertisements

  3. nvangogh

    nvangogh Guest

    On 16/05/14 15:02, Victor Bazarov wrote:
    > On 5/16/2014 9:36 AM, nvangogh wrote:
    >> 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);

    >
    > 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?
    >
    >> 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)

    >
    > Again, what that thing after 'bool' and before 'myvec'?
    >
    >> {
    >> bool result = 0;
    >> for(auto i: myvec)

    >
    > 'myvec' is the name of the function, not the name of the collection. You
    > can't use it here.
    >
    >> {
    >> if (i == val)
    >> result = 1;
    >> }
    >> else
    >> result = 0;
    >> return result;
    >> }
    >> ---------
    >> but I get the following errors when I try to compile it:
    >>> g++ -std=c++11 -o exercise9.4 exercise9.4.cpp
    >>> exercise9.4.cpp:5:23: error: invalid declarator before ‘myvec’
    >>> exercise9.4.cpp: In function ‘int main()’:
    >>> exercise9.4.cpp:19:36: error: ‘myvec’ was not declared in this scope
    >>> exercise9.4.cpp: At global scope:
    >>> exercise9.4.cpp:25:23: error: invalid declarator before ‘myvec’

    >>
    >> I need some help please to complete this exercise

    >
    > 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
    > well.
    >
    > V

    Alright sorry to bother you. I will look in my book.
     
    nvangogh, May 16, 2014
    #3
  4. On Friday, May 16, 2014 6:34:56 PM UTC-5, David Harmon wrote:
    > On Fri, 16 May 2014 14:36:17 +0100 in comp.lang.c++, nvangogh
    > <> wrote,
    > >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);

    >
    > As Victor wrote, that doesn't match a function of the type the
    > assignment asks for. Maybe:
    >
    > typedef std::vector::iterator iter;


    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
    #4
    1. Advertisements

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.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,828
  2. greg
    Replies:
    6
    Views:
    722
    Dietmar Kuehl
    Jul 17, 2003
  3. Ben Kial
    Replies:
    1
    Views:
    1,016
    Eric Enright
    Nov 15, 2004
  4. S?ren Gammelmark
    Replies:
    1
    Views:
    2,335
    Eric Sosman
    Jan 7, 2005
  5. Reckoner
    Replies:
    11
    Views:
    1,057
    Steven D'Aprano
    Jan 19, 2009
  6. AikidoGuy
    Replies:
    11
    Views:
    859
    Seebs
    Nov 21, 2011
  7. User1014
    Replies:
    1
    Views:
    357
    Richard Cornford
    Nov 30, 2006
  8. nvangogh
    Replies:
    6
    Views:
    523
    Jorgen Grahn
    Mar 20, 2014
Loading...