add missing file soi-io.hpp

This commit is contained in:
Johannes Kapfhammer 2019-10-10 14:19:14 +02:00
parent 11b6ae9729
commit e3f3f6470d
1 changed files with 82 additions and 0 deletions

82
include/bits/soi-io.hpp Normal file
View File

@ -0,0 +1,82 @@
#include <iostream>
#include <cassert>
#include <vector>
#include <string>
template <typename T>
T read() {
T x;
std::cin >> x;
return x;
}
int read_int() { return read<int>(); }
char read_char() { return read<char>(); }
double read_double() { return read<double>(); }
std::string read_string() { return read<std::string>(); }
template <typename T>
std::vector<T> read_vector(int n) {
assert(n >= 0);
std::vector<T> v;
v.reserve(n);
std::generate_n(std::back_inserter(v), n, read<T>);
return v;
}
namespace soi {
namespace io {
template <typename T, typename TChar, typename TCharTraits>
inline typename std::enable_if<!::soi::prettyprint::is_container<T>::value,
std::basic_ostream<TChar, TCharTraits> &>::type
print(std::basic_ostream<TChar, TCharTraits> &stream, const T &x) {
return stream << x;
}
template <typename TChar, typename TCharTraits>
inline std::basic_ostream<TChar, TCharTraits>&
print(std::basic_ostream<TChar, TCharTraits> &stream, std::string const& x) {
return stream << x;
}
template <typename TChar, typename TCharTraits>
std::basic_ostream<TChar, TCharTraits> &
print(std::basic_ostream<TChar, TCharTraits> &stream, const bool &x) {
return stream << (x ? "true" : "false");
}
struct space_separated_delims {
static const ::soi::prettyprint::delimiters_values<char> values;
};
const ::soi::prettyprint::delimiters_values<char> space_separated_delims::values = { "", " ", "" };
template <typename T, typename TChar, typename TCharTraits>
inline typename std::enable_if<::soi::prettyprint::is_container<T>::value,
std::basic_ostream<TChar, TCharTraits> &>::type
print(std::basic_ostream<TChar, TCharTraits> &stream, const T &x) {
return ::soi::prettyprint::pretty_print(stream,
soi::prettyprint::custom_delims<space_separated_delims>(x));
}
template <typename T, typename TChar, typename TCharTraits>
void print_with_space(std::basic_ostream<TChar, TCharTraits> &stream, T&& x) {
stream << ' ';
::soi::io::print(stream, std::forward<T>(x));
}
}
}
template <typename Arg, typename... Args>
void print(Arg&& arg, Args&&... args) {
soi::io::print(std::cout, arg);
using expander = bool[];
(void)expander{0, (soi::io::print_with_space(std::cout, std::forward<Args>(args)), false)...};
std::cout << '\n';
}
void print() {
std::cout << '\n';
}