-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNormalData.h
121 lines (96 loc) · 2.64 KB
/
NormalData.h
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef NORMAL_DATA_HEADER_H
#define NORMAL_DATA_HEADER_H
#include <vector>
#include <list>
#pragma once
using namespace std;
const int LENGTH = 10;
const int MAXNUM = 80;
const int MAXSTRLEN = 300;
const int COLUMNS = 10;
const int CHARCOLUMNS = 80;
const int ALPHABET = 26;
#define SIZE(a) (unsigned int)((sizeof(a)/sizeof(a[0])))
void initializeIndexVector(vector<int> &v, const int size);
template <typename T>
void printFunc(const T &p)
{
cout << p << "\t";
}
// Template function must be in the calling site or header file,
// otherwise it would not compile.
// T must be a container type.
// Avoid to use the keyword class to define template variable name
// here; otherwise will get "elaborated type refers to a typedef"
// compile error.
template<typename T>
void printVector(const vector<T> &v)
{
int column = 0;
for (typename vector<T>::const_iterator itr = v.begin(); itr != v.end(); ++itr)
{
cout << *itr << "\t";
if (++column % COLUMNS == 0)
{
cout << endl;
}
}
cout << endl;
}
// specialization
template<typename T>
void printVector(const vector<pair<T, T> > &v)
{
int column = 0;
for (typename vector<pair<int, int> >::const_iterator itr = v.begin(); itr != v.end(); ++itr)
{
cout << "(" << itr->first << ", " << itr->second << ") ";
if (++column % COLUMNS == 0)
{
cout << endl;
}
}
cout << endl;
}
template <typename T>
void print2DVector(const vector<vector<T> > &v)
{
for (typename vector<vector<T> >::const_iterator itr = v.begin(); itr != v.end(); ++itr)
{
for_each((*itr).begin(), (*itr).end(), printFunc<T>);
cout << endl;
}
}
// Don't put the definition of this function in header file.
// Otherwise, it would cause duplicate symbol link error.
// Or define this function as inline
//inline void printString(const string &str)
void printString(const string &str);
template <typename T>
void print2DVector2(const vector<vector<T> > &v)
{
for (typename vector<vector<T> >::const_iterator itr = v.begin(); itr != v.end(); ++itr)
{
for_each((*itr).begin(), (*itr).end(), printString);
cout << endl;
}
cout << endl;
}
void initialize2DVector(vector<vector<int> > &v, int row, int column);
// Template to calculate factorial of n, n!
template <unsigned int n>
struct factorial {
enum { value = n * factorial<n - 1>::value };
};
template <>
struct factorial<0> {
enum { value = 1 };
};
template<typename T>
void printList(list<T> &l) {
for (auto &i: l) {
cout << "(" << i.first << ", " << i.second << ")"<< "\t";
}
cout << endl;
}
#endif