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

时间:2014-03-15 09:47:03

标签: c++ visual-studio-2010 boost stl

我想用Key创建map作为两个字符串和一个int的组合,value可以是基于key的多个int。 所以我尝试创建boost :: tupleand std :: vector的地图。我尝试为此编写示例程序,如下所示:

#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);
}

这个程序工作正常,但我想知道是否有更好的方法来做到这一点。我担心性能,因为地图的大小可以增长到任何东西。我没有测量过表现。

谢谢,
Shrik

1 个答案:

答案 0 :(得分:0)

  

我担心表现,因为地图的大小可以增长到任何东西。我没有测量过表现。

在担心设计的可能性不适合任务之前,您应该担心已经进行了性能测量。

设计一些用例,并创建一个样本数据分布 - 复合键和值的常见情况,每一侧的标准偏差和尾部。不仅要考虑数据集本身,还要考虑其设置和使用情况 - 插入,搜索,删除的频率。

尽管如此,总的来说,将复合键建模为元组的方法是明智的,但主观上,我更喜欢结构,但这只是一个非常小的评论。

对于值 - 考虑使用multi-map而不是带矢量的地图,这可能会更快,但它取决于值的数量。

还有以下几点需要考虑:

  • 您的(多)地图是否必须订购或是否仍然无序?根据使用情况,无序地图可能会明显加快。
  • 您能否定制关键的知识,以便更快地进行比较?例如,如果字符串很长并且是静态的(例如,几乎总是&#34; file1&#34;),您是否可以通过首先评估两个比较键的整数部分来获益?
  • 您可以通过使用分层地图而不是带有复合键的地图来获益吗?

最好让上面的许多问题回答样本数据和场景,它们构成了程序测试套件的一部分。这样,您可以在更改数据结构时观察性能变化。