-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.cpp
137 lines (124 loc) · 3 KB
/
template.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "include.h"
//#include <bits/stdc++.h>
using namespace std;
//获取一个数字
template <typename T>
T getNum(){
T result = 0;
string str;
bool isFushu = false;
getline(cin, str);
for(int i = 0; i < str.size(); ++i){
if(str[i] == '-'){
isFushu = true;
continue;
}
if(str[i]>='0' && str[i] <= '9'){
result = isFushu? result*10 - str[i] + '0' : result*10 + str[i] - '0';
}
}
return result;
}
//获取一个字符串
string getString(){
string result;
getline(cin, result);
return result;
}
//获取一维数组
template <typename T>
vector<T> getNums(char c = ' '){
vector<T> result;
string str;
T tmp = 0;
bool minus = false;
getline(cin, str);
if(str.size() == 0) return result;
for(int i = 0; i < str.size(); i++){
if(str[i] == c){
result.push_back(tmp);
tmp = 0;
minus = false;
continue;
}
if(str[i] == '-'){
minus = true;
continue;
}
tmp = minus? tmp*10 - str[i] + '0' : tmp*10 + str[i] - '0';
}
result.push_back(tmp);
return result;
}
//获取一维字符串组
vector<string> getStrings(char c = ' '){
vector<string> result;
string str;
string tmpStr;
getline(cin, str);
if(str.size() == 0) return result;
for(int i = 0; i < str.size(); i++){
if(str[i] == c){
if(!tmpStr.empty()){
result.push_back(tmpStr);
tmpStr.clear();
}
continue;
}
tmpStr.push_back(str[i]);
}
result.push_back(tmpStr);
return result;
}
//获取二维数组
template <typename T>
vector< vector<T> > getNums2(int n, char c = ' '){
vector< vector<T> > result;
vector<T > tmpResult;
while(n--){
tmpResult = getNums<T>(c);
if(tmpResult.size() == 0) break;
result.push_back(tmpResult);
tmpResult.clear();
}
return result;
}
//获取二维字符串
vector< vector <string> > getStrings2(int n, char c = ' '){
vector< vector <string> > result;
vector< string > tmpString;
while (n--)
{
tmpString = getStrings(' ');
if(tmpString.size() == 0) break;
result.push_back(tmpString);
tmpString.clear();
}
return result;
}
//输出一维vector
template <typename T>
void printVector(vector<T> input, string str = "\t"){
for(int i = 0; i < input.size(); ++i){
cout << input[i] << str;
}
cout << endl;
}
//输出二维vector
template <typename T>
void printVector2(vector<vector<T> > input, string str = "\t"){
for(int i = 0 ; i < input.size(); ++i){
for(int j = 0; j < input[i].size(); ++j){
cout << input[i][j] << str;
}
cout << endl;
}
}
int main(int argc, char const *argv[])
{
// vector<vector<string> > nums = getStrings2(2);
// printVector2(nums);
int num = getNum<int>();
cout << num;
return 0;
}