CSES - Datatähti 2018 alku - Results
Submission details
Task:Bittijono
Sender:eliaskosunen
Submission time:2017-10-13 19:03:42 +0300
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In member function 'void table::generate()':
input/code.cpp:117:21: error: 'struct row' has no member named 'get_char'
                 ref.get_char() = c + 'A';
                     ^
input/code.cpp:118:21: error: 'struct row' has no member named 'get_int'
                 ref.get_int() = i;
                     ^
input/code.cpp: In function 'std::ostream& operator<<(std::ostream&, const table&)':
input/code.cpp:135:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(auto i = 0; i < t.t.size(); ++i)
                                      ^
input/code.cpp:137:24: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
             os << t.t[i]; 
                        ^
In file included from /usr/include/c++/4.8/iostream:39:0,
                 from input/code.cpp:5:
/usr/include/c++/4.8/ostream:602:5: error:   initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>&...

Code

#include <algorithm>
#include <array>
#include <list>
#include <map>
#include <iostream>
#include <random>
#include <set>
#include <vector>

template <typename T>
T random_integer(T min, T max)
{
    static thread_local std::random_device rd;
    static thread_local std::default_random_engine eng(rd());

    std::uniform_int_distribution<T> dist(min, max);
    return dist(eng);
}

struct cell {
    using cell_type = std::pair<char, int>;

    constexpr cell() = default;

    char& get_char()
    {
        return c.first;
    }
    char get_char() const
    {
        return c.first;
    }
    int& get_int()
    {
        return c.second;
    }
    int get_int() const
    {
        return c.second;
    }

    bool char_set() const
    {
        return get_char() != '\0';
    }
    bool int_set() const
    {
        return get_int() != -1;
    }

    friend std::ostream& operator<<(std::ostream& os, const cell& c) {
        os << c.get_int() << c.get_char();
        return os;
    }

    cell_type c{'\0', -1};
};

template <typename T>
void remove(std::vector<T>& v, std::size_t i)
{
    std::swap(v[i], v.back());
    v.pop_back();
}

struct row {
    using cell_type = cell;
    static constexpr auto row_size = 10;
    using row_type = std::array<cell, row_size>;

    constexpr row() = default;

    void generate()
    {
        std::vector<char> available_chars(row_size);
        std::iota(available_chars.begin(), available_chars.end(), 'A');

        std::vector<int> available_ints(row_size);
        std::iota(available_ints.begin(), available_ints.end(), 0);

        for (auto i = 0; i < row_size; ++i) {
            {
                auto rand = random_integer<int>(0, available_ints.size());
                r[i].get_int() = available_ints[rand];
                remove(available_ints, rand);
                /* available_ints.erase(n_begin); */
            }

            {
                auto rand = random_integer<int>(0, available_chars.size());
                r[i].get_char() = available_chars[rand];
                remove(available_chars, rand);
            }
        }
    }

    row_type r{};
};

struct table {
    using row_type = row;
    using cell_type = typename row_type::cell_type;
    using table_type = std::array<row_type, 10>;

    constexpr table() = default;

    void generate()
    {
        /* for (auto& r : t) { */
        /*     r.generate(); */
        /* } */
        for(auto i = 0; i < row_type::row_size; ++i)
        {
            for(auto c = 0; c < row_type::row_size; ++c)
            {
                auto& ref = t[i * row_type::row_size + c];
                ref.get_char() = c + 'A';
                ref.get_int() = i;
            }
        }
    }

    void reorder() {

    }

    /* void set_column(std::size_t col, row_type data) */
    /* { */
    /*     for (auto i = 0; i < row_type::row_size; ++i) { */
    /*         t[i].r[col] = data.r[i]; */
    /*     } */
    /* } */

    friend std::ostream& operator<<(std::ostream& os, const table& t) {
        for(auto i = 0; i < t.t.size(); ++i)
        {
            os << t.t[i]; 
            if(i != 0 && (i + 1) % 10 == 0) {
                os << '\n'; 
            } else {
                os << ' ';
            }
        }
        return os;
    }

    table_type t{};
};

int main()
{
    table t;
    t.generate();
    std::cout << t;
    return 0;
}