简体   繁体   English

有没有办法以特定方式显示cpp map

[英]Is there a way to display cpp map in a specific way

I need to display the following seats to users and be able to reserve them and cancel the reservation.我需要向用户显示以下座位并能够预订和取消预订。

class luxaryBus {
    public:
            std::map<std::string, int> seats{ {"1A", 0}, {"1B", 0}, {"1C", 0}, {"1D", 0},{"1E", 0},
                                        {"2A", 0}, {"2B", 0}, {"2C", 0}, {"2D", 0},{"2E", 0},
                                        {"3A", 0}, {"3B", 0}, {"3C", 0}, {"3D", 0},{"3E", 0},
                                        {"4A", 0}, {"44", 0}, {"4C", 0}, {"4D", 0},{"4E", 0},
                                        {"5A", 0}, {"5B", 0}, {"5C", 0}, {"5D", 0},{"5E", 0},
                                        {"6A", 0}, {"6B", 0}, {"6C", 0}, {"6D", 0},{"6E", 0},
                                        {"7A", 0}, {"7B", 0}, {"7C", 0}, {"7D", 0},{"7E", 0},
                                        {"8A", 0}, {"8B", 0}, {"8C", 0}, {"8D", 0},{"8E", 0},
                                        {"9A", 0}, {"9B", 0}, {"9C", 0}, {"9D", 0},{"9E", 0},
                                        {"10 A", 0}, {"10B", 0}, {"10C", 0}, {"10D", 0},{"10E", 0},
                                        {"11 A", 0}, {"11B", 0}};
    
    void displaySeats();
}



void luxaryBus::displaySeats()
{
int test = 0;
    for (const auto& p : this->seats) {
        if (test == 5) {
            std::cout << std::endl;
            test = 0;
        }
        test++;
        std::cout << p.first << p.second << "\t ";
    }
}

Apparently, it is sorting the strings in a way that doesn't work for me:显然,它以一种对我不起作用的方式对字符串进行排序:

1A 10A 10B 10C etc... 1A 10A 10B 10C 等...

I tried unordered_map also but no matter what I do I cannot make map display the seats in the way I want (1A, 1B, 1C, 1D, 1E).我也尝试了 unordered_map,但无论我做什么,我都无法让 map 以我想要的方式(1A、1B、1C、1D、1E)显示座位。

1A 1B 1C 1D 1E 1A 1B 1C 1D 1E

2A 2B 2C 2D 2E.... 2A 2B 2C 2D 2E....

Tried to change the seat name to A1 B1 C1 D1... Obviously I am new to C++.试图将座位名称更改为 A1 B1 C1 D1 ......显然我是 C++ 的新手。 I know that map is ordered.And I know that using unordered_map there is no way I can guarantee the way the map will be displayed.我知道 map 是有序的。而且我知道使用 unordered_map 我无法保证 map 的显示方式。

My question is, can I use map in a specific way so I can display it in a way that I want to.我的问题是,我能否以特定方式使用 map,以便以我想要的方式显示它。 Or, is there another data structure in c++ that I can use for my task.或者,c++ 中是否还有其他数据结构可以用于我的任务。

Array was my first choice, however, I found it difficult to follow which seat was sold and handle cancellation. Array 是我的第一选择,但是,我发现很难跟踪哪个座位已售出并处理取消。 Now I can make it work with 2 arrays, one for string representation and one for handling the operations with reservation and cancellation.现在我可以让它与 2 个 arrays 一起工作,一个用于字符串表示,一个用于处理预订和取消操作。

Can anyone give me a suggestion how to tackle this problem?谁能给我一个如何解决这个问题的建议?

std::map uses comparison operator< to compare keys in the map. std::map 使用比较运算符operator<来比较 map 中的键。 However, the problem is that strings are being compared lexicographically, so both "1A" and "10A" are smaller than "1B" .但是,问题在于字符串是按字典顺序比较的,因此"1A""10A"都小于"1B"

But you can use a more complicated map: std::map<std::pair<int, char>, int> in which key is number and character.但是您可以使用更复杂的 map: std::map<std::pair<int, char>, int>其中键是数字和字符。 Here number will be compared as numbers and characters as characters.这里将数字作为数字进行比较,将字符作为字符进行比较。

Use it like:像这样使用它:

std::map<std::pair<int, char>, int> map{{{1, 'B'}, 0}, {{1, 'C'}, 0}};

PS: you can override output like here: PS:您可以像这里一样覆盖 output :

std::ostream& operator<<(std::ostream& out, const std::map<std::pair<int, char>, int>& map)
{
    for (const auto& p : map) {
        out << p.first.first << p.first.second << " ";
    }
    return out;
}

int main() {
    std::map<std::pair<int, char>, int> map{{{1, 'B'}, 0}, {{1, 'C'}, 0}};

    std::cout << map;
    return 0;
}

You can put in a custom comparator to sort the data in the map in the order you want.您可以放入自定义比较器,按照您想要的顺序对 map 中的数据进行排序。

For this, you can refer to the following code:为此,您可以参考以下代码:

    auto seatCompare = [](std::string const& seat1, std::string const& seat2) -> bool {
        std::smatch result1;
        std::smatch result2;
        std::regex pattern(R"(([0-9]+)([a-z A-Z]+))");
        if (!regex_match(seat1, result1, pattern) || !regex_match(seat2, result2, pattern)) {
            throw std::runtime_error("Seat name invalid!");
        }
        if (std::stoi(result1[1]) == std::stoi(result2[1])) {
            return result1[2] < result2[2];
        }
        return std::stoi(result1[1]) < std::stoi(result2[1]);
    };
    std::map<std::string, int, decltype(seatCompare)> seats(seatCompare);
    seats = { {"1A", 0}, {"1B", 0}, {"1C", 0}, {"1D", 0},{"1E", 0},
              {"2A", 0}, {"2B", 0}, {"2C", 0}, {"2D", 0},{"2E", 0},
              {"3A", 0}, {"3B", 0}, {"3C", 0}, {"3D", 0},{"3E", 0},
              {"4A", 0}, {"4B", 0}, {"4C", 0}, {"4D", 0},{"4E", 0},
              {"5A", 0}, {"5B", 0}, {"5C", 0}, {"5D", 0},{"5E", 0},
              {"6A", 0}, {"6B", 0}, {"6C", 0}, {"6D", 0},{"6E", 0},
              {"7A", 0}, {"7B", 0}, {"7C", 0}, {"7D", 0},{"7E", 0},
              {"8A", 0}, {"8B", 0}, {"8C", 0}, {"8D", 0},{"8E", 0},
              {"9A", 0}, {"9B", 0}, {"9C", 0}, {"9D", 0},{"9E", 0},
              {"10A", 0}, {"10B", 0}, {"10C", 0}, {"10D", 0},{"10E", 0},
              {"11A", 0}, {"11B", 0}};

or C++98 style或 C++98 风格

struct SeatCompare {
    bool operator()(std::string const& seat1, std::string const& seat2)
    {
        std::smatch result1;
        std::smatch result2;
        std::regex pattern(R"(([0-9]+)([a-z A-Z]+))");
        if (!regex_match(seat1, result1, pattern) || !regex_match(seat2, result2, pattern)) {
            throw std::runtime_error("Seat name invalid!");
        }
        if (std::stoi(result1[1]) == std::stoi(result2[1])) {
            return result1[2] < result2[2];
        }
        return std::stoi(result1[1]) < std::stoi(result2[1]);
    }
};

std::map<std::string, int, SeatCompare> seats{ 
    {"1A", 0}, {"1B", 0}, {"1C", 0}, {"1D", 0},{"1E", 0},
    {"2A", 0}, {"2B", 0}, {"2C", 0}, {"2D", 0},{"2E", 0},
    {"3A", 0}, {"3B", 0}, {"3C", 0}, {"3D", 0},{"3E", 0},
    {"4A", 0}, {"4B", 0}, {"4C", 0}, {"4D", 0},{"4E", 0},
    {"5A", 0}, {"5B", 0}, {"5C", 0}, {"5D", 0},{"5E", 0},
    {"6A", 0}, {"6B", 0}, {"6C", 0}, {"6D", 0},{"6E", 0},
    {"7A", 0}, {"7B", 0}, {"7C", 0}, {"7D", 0},{"7E", 0},
    {"8A", 0}, {"8B", 0}, {"8C", 0}, {"8D", 0},{"8E", 0},
    {"9A", 0}, {"9B", 0}, {"9C", 0}, {"9D", 0},{"9E", 0},
    {"10A", 0}, {"10B", 0}, {"10C", 0}, {"10D", 0},{"10E", 0},
    {"11A", 0}, {"11B", 0}};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM