开发者

assign values to selective items using STL multimap

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_m开发者_运维知识库ap.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;
}

How can I access only one item from the multimap container at a time? I want to update only the jth value in the map, but when I iterate through the map all the bool values are changed. Is there a selective way to access the map container values?


The line itc->second.second = c_flags[i][j]; performed in a loop with itc from begin() to end() indeed performs assignment to every value of the map. If the goal was to modify only the j'th value in the map, there was no need for a loop over the entire map:

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

If this map is used for indexed access in this manner, it may be worthwhile to consider switching to vector, though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜