Re: iterator as argument to function
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:
// 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
.. Also, what's "iterator"? Have you defined that type? Where?
std::cout << "Input a number for the first element in the range: " ;
std::cin >> num1;
std::cout << "Input a number for the second element in the range: ";
std::cin >> num2;
std::cout << "Input any integer value to see if it is in the vector:";
std::cin >> val;
bool result = myvec(num1,num2,val);
std::cout << result << std::endl;
// 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;
result = 0;
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
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
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
I do not respond to top-posted replies, please don't ask