MergeRecord_C++中map的使用

//============================================================================
// Name        : TTTest.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdlib.h>
#include <map>
typedef struct TIME_STRU {
    int nIndex; /* 表索引 */
    int nValue; /**/
} TABLE_RECORD;

void MergeRecord(TABLE_RECORD* pOriRecord, int nOriRecordNum,
        TABLE_RECORD* pMergeRst, int* pnMergeRecordNum);

using std::cout;
using std::endl;
using std::map;
using std::make_pair;

void MergeRecord(TABLE_RECORD* pOriRecord, int nOriRecordNum,
        TABLE_RECORD* pMergeRst, int* pnMergeRecordNum) {
    map<int, int> inMap;

    for (int i = 0; i < nOriRecordNum; ++i) {
        auto ret = inMap.insert(make_pair(pOriRecord[i].nIndex, pOriRecord[i].nValue));
        if(!ret.second) {
       // 或者直接+=即可 inMap[pOriRecord[i].nIndex]
+= pOriRecord[i].nValue); } } cout << inMap.size() << endl; cout << inMap[2] << endl; return; } int main() { // TABLE_RECORD pOriRecord[] = { { 1, 3 }, { 2, 3 }, { 2, 8 } }; // cout << pOriRecord[0].nIndex << endl; // // int nOriRecordNum = sizeof(pOriRecord) / sizeof(pOriRecord[0]); // cout << nOriRecordNum << endl; // // TABLE_RECORD* pMergeRst; // int nMergeRecordNum = 0; // int* pnMergeRecordNum = &nMergeRecordNum; // MergeRecord(pOriRecord, nOriRecordNum, pMergeRst, pnMergeRecordNum); int ttt = -123; char s[8] = { 0 }; itoa(ttt, s, 10); cout << s <<endl; return 0; }

 

posted on 2016-07-21 22:31  _bob  阅读(367)  评论(0编辑  收藏  举报