c++の標準入力ってどうすればいいのか迷って、ある程度落ち着いたのでメモ。
というのも、c++には標準入力として、cinなんてのがありますが、いかんせん速度が遅いそうで入力の行数が多いケースを考えるとちょっと採用しにくいです。また、c++の入力のもう一つの問題として、splitがstd::stringにないために、一行読み込んで分割なんてのもちょっとめんどくさい…
かといってその場で合わせてscanfを書くのも個人的にはよほど特殊な形式の入力以外は避けたいなという気分です。
でsplitを実装したりなんだりで最終的に行き着いたテンプレートは以下。
main.cpp
#include "bits/stdc++.h"
template <typename type> void print_vector(type d1_arr);
template <typename type> void print_2dvector(type d2_arr);
template <typename type> void show(type arr);
inline std::vector<long long> str_int_split(char *s);
inline std::vector<std::vector<long long>> in_d2_arr(int N, int size = 100000);
inline std::vector<long long> in_d1_arr(int size = 100000);
class mystdin {
public:
std::vector<long long> i1;
std::vector<std::vector<long long>> i2;
std::string s1;
std::vector<std::string> s2;
mystdin(int, std::string);
mystdin(int);
void show();
private:
int dimension;
std::string kind;
};
long long gcm(long a, long b) {
long long result = a;
long long k = 0;
long long n = b;
do {
k = result % n;
result = n;
n = k;
} while (k != 0);
return result;
}
long long lcm(long long a, long long b) {
long long g;
g = gcm(a, b);
return a * b / g;
}
long long lcm_n(std::vector<long long> &numbers) {
long long l;
l = numbers[0];
for (int i = 1; i < numbers.size(); i++) {
l = lcm(l, numbers[i]);
}
return l;
}
int main() {
/*****************************************************************
array mystdin(dimension,optional(string))
numbers, 1d -> mystdin.i1
numbers, 2d -> mystdin.i2
string 1d -> mystdin.s1
string 2d -> mystdin.s2
******************************************************************/
auto in = mystdin(2);
in.show();
auto in2 = mystdin(2, "str");
in2.show();
}
mystdin::mystdin(int height) {
long long size = int(pow(10, 9));
dimension = height;
kind = "num";
if (height == 1) {
i1 = in_d1_arr(size);
} else {
i2 = in_d2_arr(height, size);
}
}
mystdin::mystdin(int height, std::string typ) {
long long size = int(pow(10, 9));
dimension = height;
if (typ == "long long") {
kind = "num";
if (height == 1) {
i1 = in_d1_arr(size);
} else {
i2 = in_d2_arr(height, size);
}
} else {
kind = "str";
if (height == 1) {
char *temp = new char[1000000000];
scanf(" %[a-zA-Z (+-0-9_) #.]/n", temp);
s1 = std::string(temp);
delete[] temp;
} else {
for (int i = 0; i < height; ++i) {
char *temp = new char[1000000000];
scanf(" %[a-zA-Z (+-0-9_) #.]/n", temp);
s2.push_back(std::string(temp));
delete[] temp;
}
}
}
}
void mystdin::show() {
if (kind == "num") {
if (dimension == 1) {
print_vector(i1);
} else {
print_2dvector(i2);
}
} else {
if (dimension == 1) {
std::cout << s1 << std::endl;
} else {
for (auto line : s2) {
std::cout << line << std::endl;
}
}
}
}
template <typename type> void print_vector(type d1_arr) {
for (auto seg : d1_arr) {
std::cout << seg;
std::cout << " ";
}
std::cout << std::endl;
}
template <typename type> void print_2dvector(type d2_arr) {
for (auto seg1 : d2_arr) {
for (auto seg2 : seg1) {
std::cout << seg2;
std::cout << " ";
}
std::cout << std::endl;
}
}
inline std::vector<long long> str_int_split(char *s) {
char *tok = strtok(s, " ,");
std::vector<long long> v;
while (tok != NULL) {
v.push_back(atoll(tok));
tok = strtok(NULL, " ,"); /* 2回目以降 */
}
return v;
}
inline std::vector<std::vector<long long>> in_d2_arr(int N, int size) {
std::vector<std::vector<long long>> v;
for (int i = 0; i < N; ++i) {
char *lines;
lines = new char[size];
scanf(" %[a-zA-Z (+-0-9_) #.]/n", (lines));
v.push_back(str_int_split(lines));
delete[] lines;
}
return v;
}
inline std::vector<long long> in_d1_arr(int size) {
std::vector<long long> v;
char *lines;
lines = new char[size];
scanf(" %[a-zA-Z (+-0-9_) #.]/n", (lines));
v = str_int_split(lines);
delete[] lines;
return v;
}
1 2 3
45 677
1 2 3
45 677
adfhoa kfn
nfdlskn da
adfhoa kfn
nfdlskn da
おうむ返しするだけのプログラムなのでわかりにくいかもしれませんが、
auto hoge = mystdin(n);
で
a1 a2 a3 a4
b1 b2 b3 b4
みたいな形式のn行の入力をうけることができます。
文字列をうけるときは
auto hogehoge = mystdin(n,"str");
で文字列の入力を受けることができます。
果たしてこの実装が良いか悪いかはおいておいて、ひとまず入力をうけることはできているのでまぁいいかなと思って
ます。