iterator and function argument

closed account (z0My6Up4)
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?
Last edited on
Do you want the function to return a bool or a vector?
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);
Last edited on
closed account (z0My6Up4)
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.
closed account (z0My6Up4)
I've changed it to:
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 (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;
}

But am now getting the following 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’

1) you have two return types declared. As you want to return bool, remove std::vector<int> before myvec.

2) myvec if function, not a container. You should access elements throught passed iterators.

http://en.cppreference.com/w/cpp/algorithm/find
Here. Loot at Possible implementation, First version.
Topic archived. No new replies allowed.