pr22024.cpp

来自「c++编程宝典源码及Quincy99编译器 是《标准C++编程宝典》电子工业出」· C++ 代码 · 共 48 行

CPP
48
字号
////////////////////////////////////////
// File Name: pr22024.cpp
////////////////////////////////////////
#include <iostream>
#include <map>

////////////////////////////////////////
// User-defined predicate.
////////////////////////////////////////
class compare
{
public:
    bool operator()(const int c1, const int c2) const
    {
        std::cout << "In Compare: " 
                  << c1 << " -- " << c2 << std::endl;
        return c1 < c2;
    }
};

////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
    // Create the map object.
    std::map<int, char, compare> charMap;

    // Populate the map with values.
    std::cout << "Adding elements to the map:" << std::endl;
    charMap.insert(std::map<int, char>::value_type(1,'A'));
    charMap.insert(std::map<int, char>::value_type(3,'C'));
    charMap.insert(std::map<int, char>::value_type(2,'B'));
    charMap.insert(std::map<int, char>::value_type(4,'D'));

    // Display the contents of the map.
    std::cout << std::endl << "Contents of map: " << std::endl;
    map<int, char>::iterator iter;
    for (iter = charMap.begin();
            iter != charMap.end(); iter++)
    {
        std::cout << (*iter).first << " --> ";
        std::cout << (*iter).second << std::endl;
    }

    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?