簡體   English   中英

有沒有辦法以特定方式顯示cpp map

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

我需要向用戶顯示以下座位並能夠預訂和取消預訂。

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 ";
    }
}

顯然,它以一種對我不起作用的方式對字符串進行排序:

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

我也嘗試了 unordered_map,但無論我做什么,我都無法讓 map 以我想要的方式(1A、1B、1C、1D、1E)顯示座位。

1A 1B 1C 1D 1E

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

試圖將座位名稱更改為 A1 B1 C1 D1 ......顯然我是 C++ 的新手。 我知道 map 是有序的。而且我知道使用 unordered_map 我無法保證 map 的顯示方式。

我的問題是,我能否以特定方式使用 map,以便以我想要的方式顯示它。 或者,c++ 中是否還有其他數據結構可以用於我的任務。

Array 是我的第一選擇,但是,我發現很難跟蹤哪個座位已售出並處理取消。 現在我可以讓它與 2 個 arrays 一起工作,一個用於字符串表示,一個用於處理預訂和取消操作。

誰能給我一個如何解決這個問題的建議?

std::map 使用比較運算符operator<來比較 map 中的鍵。 但是,問題在於字符串是按字典順序比較的,因此"1A""10A"都小於"1B"

但是您可以使用更復雜的 map: std::map<std::pair<int, char>, int>其中鍵是數字和字符。 這里將數字作為數字進行比較,將字符作為字符進行比較。

像這樣使用它:

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

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;
}

您可以放入自定義比較器,按照您想要的順序對 map 中的數據進行排序。

為此,您可以參考以下代碼:

    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}};

或 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