/
ex6_47.cpp
63 lines (50 loc) · 1.22 KB
/
ex6_47.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! @Alan
//!
//! Exercise 6.47:
//! Revise the program you wrote in the exercises in § 6.3.2 (p. 228) that
//! used recursion to print the contents of a vector to conditionally print
//! information about its execution. For example, you might print the size
//! of the vector on each call. Compile and run the program with debugging
//! turned on and again with it turned off.
//!
#define NDEBUG //here to toggle NDEBUG
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
using namespace std;
//!
//! @brief a better approach
//!
//! @author @Shafik Yaghmour from Stack Overflow.
//!
template <typename Iterator>
void printVector( Iterator first, Iterator last);
int main()
{
string s;
vector<string> v;
cout<<"Please Enter:\n";
while(cin>>s)
{
v.push_back(s);
#ifndef NDEBUG
cout << v.size() << std::endl;
#endif
printVector(v.begin(),v.end());
}
}
//!
//! @brief a better approach
//!
//! @author @Shafik Yaghmour from Stack Overflow.
//!
template <typename Iterator>
void printVector( Iterator first, Iterator last)
{
if( first != last )
{
std::cout << *first << " " ;
printVector( std::next( first ), last ) ;
}
}