⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 C++ Source code from a tutorial
💻 CPP
字号:
#include <iostream>
#include <stdlib.h>
#include <map>
#include <string>

using namespace std;

class StoreMe {
public:
    int Item;
};

bool operator < (const StoreMe & first, 
const StoreMe & second) {
    return first.Item < second.Item;
}

int main(int argc, char *argv[])
{

    // First try storing the instances
    map<StoreMe, StoreMe> instances;

    StoreMe key1 = {10}; // braces notation!
    StoreMe value1 = {20};
    StoreMe key2 = {30};
    StoreMe value2 = {40};
    
    instances[key1] = value1;
    instances[key2] = value2;
    
    value1.Item = 12345;
    cout << instances[key1].Item << endl;
    
    instances[key1].Item = 34567;
    cout << instances[key1].Item << endl;
    
    // Next try storing pointers to the instances
    
    map<StoreMe*, StoreMe*> pointers;
    
    StoreMe key10 = {10};
    StoreMe value10 = {20};
    StoreMe key11 = {30};
    StoreMe value11 = {40};
    
    pointers[&key10] = &value10;
    pointers[&key11] = &value11;

    value10.Item = 12345;
    cout << (*pointers[&key10]).Item << endl;
    
    // Finally try storing references to the instances.
    // (I commented it out because it will
    // get an error. See the text!)
    
    // map<StoreMe&, StoreMe&> pointers;

    system("PAUSE");	
    return 0;
}

⌨️ 快捷键说明

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