gpt4 book ai didi

C++ 案例 multimap

转载 作者:行者123 更新时间:2023-11-28 08:28:29 29 4
gpt4 key购买 nike

我有一个 multimap 定义

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() 变化?因此,当 au_map.size() = 4 时,我将有 C1、C2、C3、C4。当 au_map.size() = 5 时,C1、C2、C3、C4、C5 .

此外,具有这些值的 vector 或将其添加到多重映射内的对中哪个更好? vector 查找时间少于 multimap 。

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

最佳答案

和其他人一样,我不确定我是否完全理解您的问题。看起来您所需要的只是从 0 到 2 的每个整数的二进制表示 bits -1 您可以方便地引用(在您提到的情况下,bits 是 4 或 5,但您想要概括)。如果是这样的话,一个更易于管理和访问的结构将是 bool vector 的 vector 。也就是说,而不是使用 std::multimap , 对于 bits 的一般值, 替换 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]包含表示 i 的二进制数字的 bool vector 其中 truefalse分别对应1和0。

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

关于C++ 案例 multimap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3133333/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com