TBB concurrent_hash_map基本用法

5 篇文章 0 订阅

TBB concurrent_hash_map基本用法

#include<map>
#include<string>
#include<vector>
#include<fstream>
#include<iostream>
#include <tbb/concurrent_hash_map.h>

using namespace std;


int main () {
  tbb::concurrent_hash_map<std::string, std::vector<int>> hashMap;
  std::vector<std::string> key = {"xiao", "ming"};
  std::vector<std::vector<int>> val = {{1,2,3,4},{5,6,7,8}};
  for(int i = 0; i < 2; ++i){
    tbb::concurrent_hash_map<std::string, std::vector<int>>::value_type hashMapValuePair(key[i], val[i]);
    hashMap.insert(hashMapValuePair);
  }
  for(tbb::concurrent_hash_map<std::string, std::vector<int>>::iterator iterator1 = hashMap.begin();
      iterator1 != hashMap.end(); ++iterator1 ){
    cout << "key : " << iterator1->first << endl;
    cout << "value : {";
    for(auto &ite : iterator1->second){
      cout<< ite << ",";
    }
    cout << "}" << endl;
  }
  cout << "hashMap size = " << hashMap.size() << endl;
  tbb::concurrent_hash_map<std::string, std::vector<int>>::const_accessor hashAccessor;
  if(hashMap.find(hashAccessor,"xiao")){
    cout << "find : " << endl;
    cout << "key : " << hashAccessor->first << endl;
    cout << "value : {";
    for(auto &ite : hashAccessor->second){
      cout<< ite << ",";
    }
    cout << "}" << endl;
  }

  if(hashMap.erase(hashAccessor)){
    cout << "the number of hashMap after delete : " << hashMap.size() << endl;
  }

}

输出结果

key : xiao
value : {1,2,3,4,}
key : ming
value : {5,6,7,8,}
hashMap size = 2
find :
key : xiao
value : {1,2,3,4,}
the number of hashMap after delete : 1

简化一点的代码

#include<map>
#include<string>
#include<vector>
#include<fstream>
#include<iostream>
#include <tbb/concurrent_hash_map.h>

using namespace std;

typedef tbb::concurrent_hash_map<std::string, std::vector<int>> HashMap;
typedef typename HashMap::const_accessor HashMapConstAccessor;
typedef typename HashMap::accessor HashMapAccessor;
typedef typename HashMap::iterator HashMapIterator;
typedef HashMap::value_type HashMapValuePair;

int main () {
  HashMap hashMap;
  std::vector<std::string> key = {"xiao", "ming"};
  std::vector<std::vector<int>> val = {{1,2,3,4},{5,6,7,8}};
  for(int i = 0; i < 2; ++i){
    HashMapValuePair hashMapValuePair(key[i], val[i]);
    hashMap.insert(hashMapValuePair);
  }
  for(HashMapIterator iterator1 = hashMap.begin(); iterator1 != hashMap.end(); ++iterator1 ){
    cout << "key : " << iterator1->first << endl;
    cout << "value : {";
    for(auto &ite : iterator1->second){
      cout<< ite << ",";
    }
    cout << "}" << endl;
  }
  cout << "hashMap size = " << hashMap.size() << endl;
  HashMapConstAccessor hashAccessor;
  if(hashMap.find(hashAccessor,"xiao")){
    cout << "find : " << endl;
    cout << "key : " << hashAccessor->first << endl;
    cout << "value : {";
    for(auto &ite : hashAccessor->second){
      cout<< ite << ",";
    }
    cout << "}" << endl;
  }

  if(hashMap.erase(hashAccessor)){
    cout << "the number of hashMap after delete : " << hashMap.size() << endl;
  }

}

TBB 源码

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值