[英]Creating map of boost::tuple<std::string, std::string, int>and std::vector<int>
I want to create map with Key as a combination of two strings and one int and value can be multiple ints based on key. 我想使用两个字符串和一个int的组合键创建map,并且基于key的值可以是多个int。 So I tried to create map of boost::tupleand std::vector. 因此,我尝试创建boost :: tupleand std :: vector的地图。 I tried writing sample program for this like below: 我尝试为此编写示例程序,如下所示:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <string>
#include <boost/tuple/tuple.hpp>
#include <boost/unordered_map.hpp>
using namespace std;
typedef boost::tuple<std::string, std::string, int> tpl_t;
struct key_hash : public std::unary_function<tpl_t, std::size_t>
{
std::size_t operator()(const tpl_t& k) const
{
return boost::get<0>(k)[0] ^ boost::get<1>(k)[0] ^ boost::get<2>(k);
}
};
struct key_equal : public std::binary_function<tpl_t, tpl_t, bool>
{
bool operator()(const tpl_t& v0, const tpl_t& v1) const
{
return (
boost::get<2>(v0) == boost::get<2>(v1) &&
boost::get<0>(v0) == boost::get<0>(v1) &&
boost::get<1>(v0) == boost::get<1>(v1)
);
}
};
typedef boost::unordered_map<tpl_t, std::vector<int>, key_hash,key_equal> map_t;
void function1(map_t& myMap, std::string file, std::string txt, int num1, int num2)
{
tpl_t key = boost::make_tuple(file, txt, num1);
map_t::iterator itr = myMap.find(key);
if(itr != myMap.end())
{
itr->second.push_back(num2);
}
else
{
std::vector<int> num2Vec;
num2Vec.push_back(num2);
myMap.insert(std::make_pair(boost::make_tuple(file,txt,num1),num2Vec));
}
}
int main()
{
map_t myMap;
function1(myMap, "file1", "text", 5, 10);
function1(myMap, "file1", "text_t", 5, 30);
function1(myMap, "file2", "text", 5, 50);
}
This program is working fine but I want to know if there is any better way to do this. 该程序运行良好,但我想知道是否有更好的方法可以做到这一点。 I am worried about performance as size of map can grow to anything. 我担心性能,因为地图的大小可以扩展到任何东西。 I have not measured performance though. 我还没有衡量性能。
Thanks, 谢谢,
Shrik Shrik
I am worried about performance as size of map can grow to anything. 我担心性能,因为地图的大小可以扩展到任何东西。 I have not measured performance though. 我还没有衡量性能。
You should worry about having a performance measurement in place before you worry about the eventuality of your design being unsuitable for the task. 在担心设计的最终结果不适合该任务之前,您应该担心性能评估到位。
Design a number of use cases, and create a sample data distribution - the common cases for composite keys and values, a standard deviation to each side, and the tails. 设计许多用例,并创建样本数据分布-组合键和值的常见用例,每侧的标准偏差以及尾部。 Consider not only the data set itself but also its setup and use profile - the frequency of inserts, searches, removals. 不仅要考虑数据集本身,还要考虑其设置和使用配置文件-插入,搜索和删除的频率。
That said, overall your approach of modelling the composite key as a tuple is sensible, although, subjectively, I'd prefer a struct instead, but that's a very minor comment. 也就是说,总的来说,将组合键建模为元组的方法是明智的,尽管从主观上讲,我更喜欢使用结构,但这只是一个非常小的评论。
For the values - consider using a multi-map instead of a map with a vector, this will likely to be faster, but it depends on the number of values. 对于值-考虑使用多图而不是带有向量的图,这可能会更快,但这取决于值的数量。
Here are a couple more things to consider: 这里还有几件事要考虑:
It's better to have many of the questions above answered with sample data and scenarios which form part of the test suite for your program. 最好将上面的许多问题与示例数据和方案一起回答,这些数据和方案构成了程序测试套件的一部分。 That way you can observe the changes to performance as you change your data structures. 这样,您可以在更改数据结构时观察性能的变化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.