1. 程式人生 > >map與結構體結合使用

map與結構體結合使用

在編寫map中新增結構體的時候,嘗試了好多種的方法,網上的這種方法給我提供了一個新奇的思路,在此記錄下。

#include <iostream>
#include <map>
 
using namespace std;
 
typedef struct alertInfo {
    double alertUp;
    double alertDown;
    alertInfo(double up, double down) {
        alertUp = up;
        alertDown = down;
    };
} alert;
 
int main()
{
    map<int, alert> alert_map;
 
    for (int i = 0; i < 10; i++) {
        alert_map.insert(pair<int, alert>(i, alert(240 * i, 200)));
    }
 
    for (auto it = alert_map.begin(); it != alert_map.end(); ++it) {
        cout << it->first  << " - > " << it->second.alertUp << endl;
    }
}