0

我有一个由定义的多图

typedef std::pair<int, int> au_pair; //vertices
typedef std::pair<int, int> acq_pair; //ch qlty specified by C
typedef std::multimap<int, acq_pair> au_map;
typedef au_map::iterator It_au;

没有。的模拟取决于au_map. 例如:如果au_map.size() = 5我将有 C1、C2、C3、C4、C5。因此 2^5 = 32 例。

例如:如果是au_map.size()=4,我需要模拟我的算法 16 个案例。

for(size_t i = 0; i != 16; ++i)
{
  for(It_au it = a_map.begin(); it != a_map.end();)
  {
    acq_pair it1 = it->second;
    //case 0:
    //C1 = 0, C2 = 0, C3 = 0, C4 = 0
    //@Matthieu M 's suggestion http://stackoverflow.com/questions/3110975/c-case-declaration-closed
    //bool const c1 = i & 1;
    //bool const c2 = i & 2;
    //bool const c3 = i & 4;
    //bool const c4 = i & 8;
    //Update it1.second with corresponding C values
    it->second.second = C1;
    it++;
    it->second.second = C2;
    it++;
    it->second.second = C3;
    it++;
    it->second.second = C4;
    it++;
  }
  //simulate algorithm
}

如何使这个过程自动化,C 的大小根据au_map.size()?因此,我将有 C1, C2, C3, C4 whenau_map.size() = 4和 C1, C2, C3, C4, C5 when au_map.size() = 5

另外,什么是具有这些值的向量的首选,或者将其添加到多图内的一对中?矢量查找时间小于多图。

此外,如果我继续向多图插入值,新/更新的值是否会传递给算法?

4

3 回答 3

1

像其他人一样,我不确定我是否完全理解你的问题。看起来您所需要的只是从 0 到 2 bits-1 的每个整数的二进制表示,您可以方便地引用(在您提到的情况下,bits是 4 或 5,但您想概括)。如果是这种情况,更易于管理和访问的结构将是 bool 向量的向量。也就是说std::multimap,对于 的一般值,不要使用 a ,而是用abits替换... ,例如:std::multimapstd::vector<std::vector<bool> >

std::vector<std::vector<bool> > c_flags(1 << bits);

for (size_t i = 0; i < c_flags().size(); ++i)
{
    for (size_t j = 0; j < bits; ++j)
        c_flags[i].push_back( (i & (1 << j)) > 0);
}

此时,c_flags[i]包含一个布尔向量,表示iwheretruefalse分别对应 1 和 0 的二进制数字。

您也可以使用 astd::map<std::vector<bool> >而不是std::vector<std::vector<bool> >which 可以减少内存需求(如果您不需要所有可能的二进制表示),但代价是计算成本更高。我不明白您为什么需要使用 a std::multimap,但是我对您要解决的问题的细节也没有太多了解。

于 2010-06-28T15:43:36.953 回答
0

什么是 C1、C2 等?它们只是整数还是字符串?在这种情况下,您可以通过保留一个计数器变量来自动生成它们。
为什么要pair<int,int>在多重地图内部?
我不明白最后一个问题。

于 2010-06-28T14:58:43.983 回答
0

没有不尊重的意思,但你问了最令人困惑的问题。我不得不挖掘你之前的问题才能更好地理解这个问题,我也不确定我是否理解了上一个问题。

最初我有 4 个输入 C1、C2、C3、C4。这意味着我总共有 16 种组合:

0000 0001 。. . 1111

在 C 的大小发生变化的情况下,如何使这个过程自动化 [...]

通常最简单的方法是编写嵌套循环来生成组合(我知道这不是你想要的,请继续阅读):

for (int a=0; a < 2; ++a)
{
    for (int b=0; b < 2; ++b)
    {
        for (int c=0; c < 2; ++c)
        {
            for (int d=0; d < 2; ++d)
            {
                // I'm just printing the values here but
                // you could insert them to a container if 
                // you want.
                cout << a << b << c << d << endl;
            }
        }
    }
}

但是,如果您无法确定我们需要提前编写的嵌套循环的数量(例如:如果 C 的大小基于运行时条件),那么请考虑使用递归解决方案来生成组合。

void generate_combinations(int depth, int max_depth, string str)
{
    if (depth < max_depth)
    {
        generate_combinations(depth + 1, max_depth, str + "0");
        generate_combinations(depth + 1, max_depth, str + "1");
    }
    else
        cout << str << " ";
}

int main()
{
    generate_combinations(0, 3, "");
}

这输出:

000 001 010 011 100 101 110 111

虽然这样:

generate_combinations(0, 4, "");

输出:

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

... 等等。您可以根据运行时条件随意控制 C,而且非常容易。

另外,什么是具有这些值的向量的首选,或者将其添加到多图内的一对中?矢量查找时间小于多图。

如果您的数据很密集(例如:索引范围从 0 到 N,没有间隙),那么没有理由使用带有 int 键的映射。仅当您要表示的数据稀疏时,使用带有整数键的映射才有用。否则考虑 std::vector 或 std::deque。

于 2010-06-28T15:22:46.823 回答