Q:

Write a recursive function to print the contents of a vector

0

 Write a recursive function to print the contents of a vector.

All Answers

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

#include <vector>
#include <iostream>

void printVecInt(const std::vector<int>::iterator bg,
                 const std::vector<int>::iterator ed) {
  if (bg == ed)
    return;
  std::cout << *bg << " ";
  printVecInt(bg + 1, ed);
}

int main() {
  std::vector<int> vi;
  int i;
  while (std::cin >> i)
    vi.push_back(i);

  printVecInt(vi.begin(), vi.end());

  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