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:
I need some help please to complete this 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".
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