chore(cpp): improve the template

* add missing headers: ‹bit›, ‹map›, ‹queue› and ‹set›
* name the namespaces
* math
  * introduce ‹MODULO› for big numbers in problems
* input
  * implement ‹load_vector›
* output
  * implement ‹operator<<› for ‹std::pair›
  * implement ‹yesno› as shortcut for YES/NO answers
* implement ‹LOOP(n)› for quick range-like for-loops
* make ‹N› test cases the default

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2023-07-23 11:39:05 +02:00
parent 0bfc6f808e
commit 663472e5da
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -1,18 +1,31 @@
#include <algorithm> #include <algorithm>
#include <bit>
#include <cassert> #include <cassert>
#include <cctype> #include <cctype>
#include <cstdint> #include <cstdint>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <map>
#include <optional> #include <optional>
#include <queue>
#include <set>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
template <typename T, typename U>
std::ostream &operator<<(std::ostream &s, std::pair<T, U> const &p) {
return s << p.first << " " << p.second;
}
namespace helpers { namespace helpers {
using namespace std; using namespace std;
namespace math {
static constexpr int32_t MODULO = 1000000007;
long pow(long base, long exp) { long pow(long base, long exp) {
if (exp == 0) return 1; if (exp == 0) return 1;
long half = pow(base, exp / 2); long half = pow(base, exp / 2);
@ -20,6 +33,27 @@ long pow(long base, long exp) {
return half * half * base; return half * half * base;
} }
} // namespace math
namespace input {
template <typename T>
std::vector<T> load_vector(std::size_t size) {
std::vector<T> result{};
for (auto i = 0u; i < size; ++i) {
T x;
std::cin >> x;
result.push_back(std::move(x));
}
return result;
}
} // namespace input
namespace output {
template <typename T> template <typename T>
inline void answer(const T &ans) { inline void answer(const T &ans) {
cout << ans << "\n"; cout << ans << "\n";
@ -28,9 +62,28 @@ inline void answer(const T &ans) {
inline void yes() { cout << "YES\n"; } inline void yes() { cout << "YES\n"; }
inline void no() { cout << "NO\n"; } inline void no() { cout << "NO\n"; }
inline void yesno(bool ans) {
if (ans) {
yes();
} else {
no();
}
}
} // namespace output
using namespace math;
using namespace input;
using namespace output;
#define LOOP(n) for (auto i = 0; i < n; ++i)
} // namespace helpers } // namespace helpers
namespace { // for N test cases, uncomment for single test case
// #define SINGLE
namespace solution {
using namespace std; using namespace std;
using namespace helpers; using namespace helpers;
@ -39,10 +92,9 @@ void solve() {
// TODO // TODO
} }
} // namespace } // namespace solution
// for single test case, comment out for N test cases using namespace solution;
#define SINGLE
#ifdef TEST #ifdef TEST
@ -58,7 +110,7 @@ int main(void) {
#ifdef SINGLE #ifdef SINGLE
solve(); solution::solve();
#else #else
@ -67,7 +119,7 @@ int main(void) {
std::cin >> N >> std::ws; std::cin >> N >> std::ws;
for (auto i = 0; i < N; ++i) { for (auto i = 0; i < N; ++i) {
solve(); solution::solve();
} }
#endif #endif