-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-2.cpp
65 lines (49 loc) · 1.63 KB
/
10-2.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
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
// TableFormatter formats data for output to a stream of characters
// of type T.
template<typename T>
class TableFormatter {
public:
TableFormatter(basic_ostream<T>& os) : out_(os) {}
~TableFormatter( ) {out_ << flush;}
template<typename valT>
void writeTableRow(const vector<valT>& v, int width);
//...
private:
basic_ostream<T>& out_;
};
template<typename T> // refers to class template param list
template<typename valT> // refers to mem fn template param list
void TableFormatter<T>::writeTableRow(const std::vector<valT>& v,
int width) {
ios_base::fmtflags flags = out_.flags( );
out_.flush( );
out_ << setprecision(2) << fixed; // Set the precision, in case
// this is floating-point data
for (vector<valT>::const_iterator p = v.begin( );
p != v.end( ); ++p)
out_ << setw(width) << left << *p; // Set the width, justify,
// and write the element
out_ << endl; // Flush
out_.setf(flags); // Set the flags back to normal
}
int main( ) {
TableFormatter<char> fmt(cout);
vector<string> vs;
vs.push_back( "Sunday" );
vs.push_back( "Monday" );
vs.push_back( "Tuesday" );
fmt.writeTableRow(vs, 12);
fmt.writeTableRow(vs, 12);
fmt.writeTableRow(vs, 12);
vector<double> vd;
vd.push_back(4.0);
vd.push_back(3.0);
vd.push_back(2.0);
vd.push_back(1.0);
fmt.writeTableRow(vd, 5);
}