开发者

Map inserstion failure: can not overwrite first element?

开发者 https://www.devze.com 2023-04-13 00:23 出处:网络
static std::map <unsigned int, CPCSteps> bestKvariables; inline void copyKBestVar(MaxMinVarMap& vMaxMinAll, size_t& K, std::vector<unsigned int>& temp)
static std::map <unsigned int, CPCSteps> bestKvariables;

inline void copyKBestVar(MaxMinVarMap& vMaxMinAll, size_t& K, std::vector<unsigned int>& temp)
{
    // copy top k variables and store in a vector.
    MaxMinVarMap::reverse_iterator iter1;
    size_t count;
    for (iter1 = vMaxMinAll.rbegin(), count = 0; iter1 != vMaxMinAll.rend()&& count <= K; ++iter1, ++count)
    {
        temp.push_back(iter1->second);
    }
开发者_StackOverflow}

void myAlgo::phase1(unsigned int& vTarget)
{

    CPCSteps KBestForT; // To store kbest variables for only target variable finally put in a global storage
    KBestForT.reserve(numVars);
    std::vector<unsigned int> tempKbest;
    tempKbest.reserve(numVars);
    .......
    .......
    copyKBestVar(mapMinAssoc, KBestSize, tempKbest); // Store k top variables as a k best for this CPC variable for each step
    KBestForT.push_back(tempKbest);
    .....
    .....
    bestKvariables.insert(make_pair(vTarget, KBestForT)); // Store k best in a Map
    .....
    ....
}

The Problem: Map "bestKvariables" is not overwriting the first element, but it keeps updating the rest of the elements. I tried to debug it but the problem i found is in insert command.

Thanks in advance for helping.

Another Question: whether i can reserve the size of a map(like vector.reserve(..)) in the beginning to avoid the insertion cost.

Sorry for providing insufficient information.

I means if there are four vTarget variables 1, 2, 3, 4. I do some statistical calculations for each variable. There are more than one iterations for these variables, i would like to store top k results of each variable in a map to use it next iteration.

I saw the first inserted variable(with key unsigned int "vTarget") is not updated on further iterations(it remains the value inserted on first iteration). But the other variables (keys inserted after the first) are remains updated.


Another Question: whether i can reserve the size of a map(like vector.reserve(..)) in the beginning to avoid the insertion cost.

std::map does not have a reserve() function unlike std::vector.
Usually, the Standard library provides functions for containers that provide and ensure good performance or provide a means to achieve the same.

For a container like std::vector reallocation of its storage can be a very costly operation. A simple call to push_back() can lead to every element in the std::vector to be copied to a newly allocated block of memory. A call to reserve() can avoid these unnecessary allocations and copy operations for std::vector and hence the same is provided for it.

std::map never needs to copy all of the existing/remaining elements simply because a new element was inserted or removed. Hence it does not provide any such function.

Though the Standard does not specify how a std::map should be implemented the behavior expected and the complexity desired ensures that most implementations implement it as a tree, unlike std::vector which needs elements to be allocated in contiguous memory locations.


map::insert isn't supposed to update/overwrite anything, just insert not-yet-present elements. Use operator[] for updating, it also inserts elements when the specified key is not present yet.

0

精彩评论

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

关注公众号