/problem-solving-cpp

c++ problem-solving template with missile launcher

Primary LanguageC++MIT LicenseMIT

Problem Solving Template for C++

This template provides several built-in useful features for problem solving with c++, such as

  • auto include your own libraries
  • AtCoder library included by default
  • flexible and IDE-friendly compilation upon cmake
  • library management powered by vcpkg
  • basic stdin/out automated testing

Prerequisites

  • c++ compiler
  • cmake >= 3.17
  • git

Preparation

  1. Download vcpkg with git submodule.

    git submodule init
    git submodule update
  2. Bootstrap vcpkg.

    # for macOS & linux
    ./vcpkg/bootstrap-vcpkg.sh --disable-metrics
    
    # for windows
    ./vcpkg/bootstrap-vcpkg.bat --disable-metrics
  3. Generate cmake build system and install vcpkg dependencies.

    cmake .

Solve problems

  1. Create cpp file in solutions directory and update cmake

    # You can start with `solution/example.cpp`.
    cp solutions/example.cpp solutions/cf553.cpp
    
    # keeping file inside nested directory is also ok EXCEPT that file name must be unique.
    mkdir -p solutions/codeforce
    cp solutions/example.cpp solutions/codeforce/cf553.cpp
    cmake .
  2. Solve the problem!

  3. Compile code with cmake and run.

    cmake --build . --target cf553
    ./build/cf553

Run test case

  1. Add test input and expected output respectively as {solution_filename}.in {solution_filename}.out besides {solution_filename}.cpp. For example, if solution file is solutions/cf553.cpp,

    solutions/cf553.in

    5 2 1
    5 3
    4 2
    6 4
    3 2
    2 2

    solutions/cf553.out

    2 5
    TT
    
  2. Build and run test. The test is defined as {solution_filename}_test by cmake.

    cmake --build . --target cf553_test
    ./build/cf553_test
    

Write and use your own library

Just write your libraries inside include directory and use it in your solution. Codes inside include directory is included by default. For example, suppose that you just implemented header-only include/dijkstra.hpp, by then

// inside solution.cpp
#include <dijkstra.hpp>

...

Add and use vcpkg package

AtCoder library which is included by default can be used right away.

// inside solution.cpp
#include <atcoder/fenwicktree>

using namespace atcoder;

fenwick_tree<int> fw(2 << 20);
fw.add(1, 10)

Add desired package in dependencies array in vcpkg.json. Suppose that you want to add abseil. Then,

  1. add "abseil" inside "dependencies" key of vcpkg.json

  2. run cmake .

  3. done!

    // inside solution.cpp
    #include <absl/container/btree_set.h>
    
    using namespace absl;
    
    btree_set<std::string> set1;