简体   繁体   English

创建boost :: tuple的地图 <std::string, std::string, int> 和std :: vector <int>

[英]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: 这里还有几件事要考虑:

  • Does your (multi-)map have to be ordered or can it remain unordered? 您的(多)地图是否必须排序或可以保持无序状态? Depending on the use profile, an unordered map could be significantly faster. 根据使用情况,无序地图可能会更快。
  • Could you tailor your knowledge of the keys to make comparisons faster? 您能否定制您对按键的了解以使比较更快? For example, if the strings are long and static (eg, almost always "file1"), could you benefit by evaluating the integer part of two compared key first? 例如,如果字符串较长且是静态的(例如,几乎总是“ file1”),那么您可以通过首先评估两个比较键的整数部分来受益吗?
  • Could you benefit by having a hierarchical map instead of a map with a composite key? 通过使用分层映射而不是具有复合键的映射,您能从中受益吗?

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.

相关问题 复制std :: map <int, vector<int> &gt;到std :: map <std:string, vector<int> &gt; - copy std::map<int, vector<int>> to std::map<std:string, vector<int>> std :: cout地图<string, int> - std::cout for map<string, int> 类型为typedef映射的数据成员的类中的编译器错误 <std::string,std::pair<std::string,vector<int> &gt;&gt; MapPairList ;? - compiler error in class with datamember of type typedef map<std::string,std::pair<std::string,vector<int>>> MapPairList;? 从std :: map转换 <std::basic_string<char> ,std :: pair - conversion from std::map<std::basic_string<char>,std::pair<int,int(*)(const std::vector::Mat 如何在std :: unordered_map中更新向量 <std::string, std::vector<int> &gt;没有复制? - How to update the vectors in a std::unordered_map<std::string, std::vector<int>> without copying? 是否可以使用pair std:string和std :: vector重载&lt;&lt; operator for std :: map <int> ? - Would it be possible to overload << operator for a std::map with pair std:string and std::vector<int>? 如何迭代boost :: variant <std::vector<int> ,std :: vector <String> &gt;? - How to iterate over boost::variant<std::vector<int>, std::vector<String>>? 如何遍历std :: map <string,int> 和std :: vector <int> 使用单for循环? - How to iterate through a std::map<string,int> and std::vector<int> using single for loop? 的std ::矢量 <std::pair<int,std::pair<Bone,std::string> &gt;&gt;不按int排序? - std::vector<std::pair<int,std::pair<Bone,std::string> > > not sorting by int? const std :: map <boost::tuples::tuple, std::string> ? - const std::map<boost::tuples::tuple, std::string>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM