I am looking at the following exercise:
"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." This is a simple problem but I can't see the solution!
So I write the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#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 compile errors:
exercise9.4.cpp:5:22: 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'm not sure of the syntax the iterators should have as function arguments. Can anyone help with this?