简体   繁体   English

迭代器不会遍历整个地图

[英]The iterator doesn't iterate over the entire map

I have the following function: 我有以下功能:

   void send_sequence_to_device( std::map<const string_t,device_t*> &msg2device_p, std::vector<response_t>& result_list, ushort num_attempts)
    {
        cout<<"sarit enter to send_seq_device"<<endl;
        std::map<const string_t, device_t*>::iterator msg_itf;
        for( msg_itf=msg2device_p.begin(); msg_itf!=msg2device_p.end(); msg_itf++ )
        {
            cout<<"sarit enter to seq "<<msg_itf->first<<endl;
        }
    }

I call this function by another function: 我通过另一个函数调用此函数:

 void node_layer_manager_t::calc_ts_job_function()
    {
        vector<response_t> res;
        map<const string_t, device_t*> getRegMsg={{"get_node_ts_est",&tx},{"get_node_ts_est",&rx},{"get_tx_num_clk_ts",&tx}};
        cout<< "sarit ts clk function nlm first"<<endl;
        send_sequence_to_device(getRegMsg,res);
    }

i can see that the loop iterate only 2 instead of 3. The output is: 我可以看到该循环仅迭代2而不是3。输出为:

sarit enter to seq get_node_ts_est

sarit enter to get_tx_num_clk_ts

While I expect for: 虽然我期望:

sarit enter to seq get_node_ts_est

sarit enter to seq get_node_ts_est

sarit enter to get_tx_num_clk_ts

A std::map does not allow duplicate keys. std::map不允许重复的键。

Two of your values have the same key, hence only one of them will make it into the map. 您的两个值具有相同的键,因此只有其中一个会进入地图。

Use std::multimap instead of std::map , if you need duplicate keys. 如果需要重复键,请使用std::multimap而不是std::map

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM