iterator as argument to function


N

nvangogh

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
 
Ad

Advertisements

V

Victor Bazarov

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;
}

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
 
N

nvangogh

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

V
Alright sorry to bother you. I will look in my book.
 
Ad

Advertisements

A

Anand Hariharan

On Fri, 16 May 2014 14:36:17 +0100 in comp.lang.c++, nvangogh


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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top