"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!
#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;
}
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?
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
[...]
I'm not sure of the syntax the iterators should have as function arguments.
bool search(std::vector<int>::iterator begin, std::vector<int>::iterator end, int value);
I want it to return a bool. I have got confused over the way iterators should be passed as arguments. I think I have errors in my declaration & definition in this respect.
#include <iostream>
#include <vector>
// function declaration
bool std::vector<int>myvec (std::vector<int>::iterator begin, std::vector<int>::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 (std::vector<int>::iterator begin, std::vector<int>::iterator end, int val)
{
bool result = 0;
for(auto i: myvec)
{
if (i == val)
result = 1;
}
else
result = 0;
return result;
}
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’