gpt4 book ai didi

c++ - 使用 STL multimap 为选择性项目赋值

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

typedef std::pair<int, bool> acq_pair; //edge, channel_quality
typedef std::pair<int, acq_pair> ac_pair;
typedef std::multimap<int, acq_pair> ac_map;
typedef ac_map::iterator It_acq;


int bits = acq_map.size();
std::cout << "bits = " << bits << std::endl;

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

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

std::cout << "c_flags.size() = " << c_flags.size() << std::endl;

for(i = 0; i < c_flags.size(); ++i)
{
for(j = 0; j < bits; ++j)
{
std::cout << c_flags[i][j] << std::endl;

for(It_acq itc = acq_map.begin(); itc!= acq_map.end(); ++itc)
{
acq_pair it1 = itc->second;
itc->second.second = c_flags[i][j];
std::cout << itc->first << " : " << it1.first << " : " << it1.second << std::endl;
}
}
std::cout << "\n" << std::endl;
}

如何一次只访问多 map 容器中的一项?我只想更新 map 中的第 j 个值,但是当我遍历 map 时,所有 bool 值都发生了变化。是否有选择性的方式来访问 map 容器值?

最佳答案

itc->second.second = c_flags[i][j]; performed in a loop with itc from begin() to end() 确实对映射的每个值执行赋值.如果目标是只修改映射中的第 j 个值,则不需要遍历整个映射:

    for(size_t j = 0; j < bits; ++j)
{
std::cout << c_flags[i][j] << std::endl;

It_acq itc = acq_map.begin(); // itc points at the beginning
advance(itc, j); // itc points at the j'th element
itc->second.second = c_flags[i][j]; // the assignment

for(It_acq itc = acq_map.begin(); itc!= acq_map.end(); ++itc)
{
acq_pair it1 = itc->second;
// itc->second.second = c_flags[i][j]; // no assignment here
std::cout << itc->first << " : " << it1.first << " : " << it1.second << std::endl;
}
}

如果此映射以这种方式用于索引访问,则可能值得考虑切换到 vector 。

关于c++ - 使用 STL multimap 为选择性项目赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3143881/

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