更新map中已存在的value值

原因:本来想直接通过插入更新同一key对应的value发现覆盖不了。

实现:

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <map>

using namespace std;

static pthread_mutex_t  mtxBckLoadProgress = PTHREAD_MUTEX_INITIALIZER;

typedef std::map<int,float> mapBackupprogress;   //存放备份进度消息
mapBackupprogress rdMapbckupProgress;

void AddrecordBckProgress(int backupid, float progress)
{
    pthread_mutex_lock(&mtxBckLoadProgress);
    printf("backupid: %d, progress: %f.\n",backupid ,progress);
    map< int ,float >::iterator it = rdMapbckupProgress.find( backupid );
    if (it != rdMapbckupProgress.end())
    {
      /*,如果map里面插入之前的<key, value>,如果key在map里面有的话,不会覆盖之前的value,一般先判断之前有没有数据,有的话先删除,然后再去添加*/
      rdMapbckupProgress.erase(it);
      rdMapbckupProgress.insert(std::make_pair( backupid, progress));
      printf("[AddrecordBckProgress]:Already Find backupid: %d progress:%f update from map.\n",backupid,progress);
    }
    else{
        rdMapbckupProgress.insert(std::make_pair( backupid, progress));//如果map不存在插入新的
        printf( "[AddrecordBckProgress]:instert new bckupProgress backupid: %d, progress: %f.\n", it->first, it->second);
    }

    it = rdMapbckupProgress.begin();
    for(;it != rdMapbckupProgress.end();it++)
    {
        printf("[AddrecordBckProgress]:finally bckupProgress!!! backupid: %d, progress: %f.\n", it->first, it->second);
    }
    pthread_mutex_unlock(&mtxBckLoadProgress);
    return ;
}

int recordGetBackuploadprogress(int inkey)
{
    int ret = -1;
    //Json::Value root;
    //AutoMutexLck lck(&mtxBckLoadProgress); // 如不是嵌套锁,请注意锁死问题
    
    pthread_mutex_lock(&mtxBckLoadProgress);
    map< int ,float >::iterator it = rdMapbckupProgress.find( inkey );
    if (it != rdMapbckupProgress.end())
    {
      //root["backupid"]       = it->first;
      //root["progress"]       = it->second;
      //result = root.toStyledString().c_str();
      printf( "[recordGetBackuploadprogress]:recordGetBackuploadprogress backupid: %d, progress: %f.\n", it->first, it->second);
      ret = 0;
    }
    else{
        printf("[recordGetBackuploadprogress]:Find backupid: %d progress fialed from map.\n",inkey);
    }
    pthread_mutex_unlock(&mtxBckLoadProgress);
    return ret;
}

int main()
{
  float count = 1.03;
  while(1)
  {
    AddrecordBckProgress(1,count);
    count++;
    sleep(1);
    if(count >= 10)
      break;
  }
  recordGetBackuploadprogress(1);
  printf("main exit....\n");
  return 0;
}

运行结果 

huqin@ubuntu:~/Demo$ g++ map.cpp -o map
huqin@ubuntu:~/Demo$ ./map
backupid: 1, progress: 1.030000.
[AddrecordBckProgress]:instert new bckupProgress backupid: 1, progress: 0.000000.
[AddrecordBckProgress]:finally bckupProgress!!! backupid: 1, progress: 1.030000.
backupid: 1, progress: 2.030000.
[AddrecordBckProgress]:Already Find backupid: 1 progress:2.030000 update from map.
[AddrecordBckProgress]:finally bckupProgress!!! backupid: 1, progress: 2.030000.
backupid: 1, progress: 3.030000.
[AddrecordBckProgress]:Already Find backupid: 1 progress:3.030000 update from map.
[AddrecordBckProgress]:finally bckupProgress!!! backupid: 1, progress: 3.030000.
backupid: 1, progress: 4.030000.
[AddrecordBckProgress]:Already Find backupid: 1 progress:4.030000 update from map.
[AddrecordBckProgress]:finally bckupProgress!!! backupid: 1, progress: 4.030000.
backupid: 1, progress: 5.030000.
[AddrecordBckProgress]:Already Find backupid: 1 progress:5.030000 update from map.
[AddrecordBckProgress]:finally bckupProgress!!! backupid: 1, progress: 5.030000.
backupid: 1, progress: 6.030000.
[AddrecordBckProgress]:Already Find backupid: 1 progress:6.030000 update from map.
[AddrecordBckProgress]:finally bckupProgress!!! backupid: 1, progress: 6.030000.
backupid: 1, progress: 7.030000.
[AddrecordBckProgress]:Already Find backupid: 1 progress:7.030000 update from map.
[AddrecordBckProgress]:finally bckupProgress!!! backupid: 1, progress: 7.030000.
backupid: 1, progress: 8.030000.
[AddrecordBckProgress]:Already Find backupid: 1 progress:8.030000 update from map.
[AddrecordBckProgress]:finally bckupProgress!!! backupid: 1, progress: 8.030000.
backupid: 1, progress: 9.030000.
[AddrecordBckProgress]:Already Find backupid: 1 progress:9.030000 update from map.
[AddrecordBckProgress]:finally bckupProgress!!! backupid: 1, progress: 9.030000.
[recordGetBackuploadprogress]:recordGetBackuploadprogress backupid: 1, progress: 9.030000.
main exit....

上一篇:日常记录(66)设计


下一篇:时间操控者/Timespinner