forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex6_33.cpp
94 lines (77 loc) · 1.95 KB
/
ex6_33.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! @Alan
//!
//! Exercise 6.33:
//! Write a recursive function to print the contents of a vector.
//!
// question about this exercise on stackoverflow:
// http://stackoverflow.com/questions/20184299/how-to-understand-and-fix-the-segmentation-fault-in-this-code
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
using namespace std;
//!
//! @brief my code...can handle fixed size vector, cann't handle vectors
//! that change size during rumtime..
//!
//! @author @Alan
//!
//! @badcode Misc Comment: Stop using underscores to start your variable names.
//! _v is a really bad name, just go with v. @ RichardPlunkett.
//!
template<class T>
void vector_print(const vector<T> &_v);
//!
//! @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);
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 ) ;
}
}
//!
//! @brief my code...can handle fixed size vector, cann't handle vectors
//! that change size during rumtime..
//!
//! @author @Alan
//!
//! @badcode Misc Comment: Stop using underscores to start your variable names.
//! _v is a really bad name, just go with v. @ RichardPlunkett.
//!
template<class T>
void vector_print(const vector<T> &_v)
{
static
typename vector<T>::const_iterator it = _v.begin();
cout << *it
<<" ";
++it;
if(it != _v.end())
{
vector_print(_v);
}
}