Q:

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

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <vector>
#include <iostream>
#include <sstream>

bool hasValue(std::vector<int>::const_iterator begin,
              std::vector<int>::const_iterator end,
              int k) {
  for (auto it = begin; it != end; ++it)
    if (k == *it)
      return true;
  return false;
}

int main() {
  std::string str;
  std::getline(std::cin, str);
  std::istringstream iss(str);
  std::vector<int> vi;
  int k;
  while (iss >> k)
    vi.push_back(k);
  std::cin >> k;
  std::cout << hasValue(vi.cbegin(), vi.cend(), k) << std::endl;

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now