1. 程式人生 > >c++裡的容器

c++裡的容器

#include "stdafx.h"

#include<windows.h>

#include<iostream>

#include<vector>

#include<list>

#include<map>

using namespace std;

//迭代器: 資料型別,用來查詢容器元素的型別

//定義: 容器<資料型別>::iterator 迭代器名字

//typedef struct alertInfo {

//      double alertUp;

//      double alertDown;

//      alertInfo(double up, double down) {

//             alertUp = up;

//             alertDown = down;

//      };

//} alert;

struct sData

{

        sData()

        {

               iRow = 0;

               iCol = 0;

        }

        int iRow;

        int     iCol;

};

int main()

{

        //vector<int> VecNums;

        //for (int i=0; i < 7; i++)

        //{

        //      VecNums.push_back(i);

        //}

        ////VecNums.push_back(0);

        //vector<int>::iterator iter = VecNums.begin();    //iter++可以   *iter賦值可以

        //vector<int>::const_iterator iter2 = VecNums.begin();              //iter++可以   *iter賦值不可以

        //const vector<int>::iterator iter3 = VecNums.begin();              //iter++不可以   *iter賦值可以

        //for (;iter!=VecNums.end() ; ++iter)

        //{

        //      cout << *iter << endl;

        //}

        //定義:list<資料型別> 名字

        //list<int>List;

        //map容器

        //關鍵陣列:鍵值對 key - value

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

        //}

        map<int, sData>MapData;

        for (int i = 0; i < 10; i++)

        {

               sData sNewData;

               sNewData.iRow = i;

               sNewData.iCol = i;

               MapData.insert(pair<int, sData>(i, sNewData));

        }

        for (auto iter = MapData.begin(); iter != MapData.end(); ++iter)

        {

               cout << iter->first << "\t"<< iter->second.iRow << "\t" << iter->second.iCol<<endl;

        }

        system("pause");

    return 0;

        

}