Q:

Use find to find the last element in a list of ints with value 0

0

Use find to find the last element in a list of ints with value 0.

All Answers

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

#include <vector>
#include <iostream>

int main() {
  std::vector<int> vi{0, 1, 2, 3, 4, 5, 0};
  auto rit = find(vi.crbegin(), vi.crend(), 0);
  std::cout << "The last " << *rit << " in vector is at position "
      << rit.base() - vi.cbegin() - 1 << std::endl;
      //<< vi.crend() - rit - 1 << std::endl;
  // rit.base() is the iterator one past element 0, just like end iterator

  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