【问题标题】:The iterator of a map doesn't hold the correct value [closed]地图的迭代器不包含正确的值[关闭]
【发布时间】:2016-05-10 16:05:35
【问题描述】:

我有以下功能:

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

我用另一个函数调用这个函数:

void node_layer_manager_t::ts_clk_est_job_function(void)
{
    vector<response_t> res;
    map<const string_t, device_t*> setRegMsg={{"trx_set_jr_estim_fs_to_cnt "+NUM_FRAMES_TO_COUNT_IN_TS_CLK_EST,&trx},{"trx_set_jr_estim_fs_to_cnt "+NUM_FRAMES_TO_COUNT_IN_TS_CLK_EST,&trx}}, getRegMsg={{"trx_get_jr_estim_params",&trx},{"trx_get_jr_estim_params",&trx}};
    cout<< "sarit ts clk function nlm first"<<endl;
    send_sequence_to_device(setRegMsg,res);
}

NUM_FRAMES_TO_COUNT_IN_TS_CLK=3(定义)

出于某种原因,例如第一对 {"trx_set_jr_estim_fs_to_cnt "+NUM_FRAMES_TO_COUNT_IN_TS_CLK_EST,&amp;trx} send_sequence_to_device func 只打印它应该打印的部分内容。 “estim_fs_to_cnt”而不是“trx_set_jr_estim_fs_to_cnt 5”

【问题讨论】:

    标签: c++ c++11 dictionary iterator


    【解决方案1】:

    问题在于您尝试将字符串与整数连接的方式:

    "trx_set_jr_estim_fs_to_cnt " + 1
    

    不会变成"trx_set_jr_estim_fs_to_cnt 1",而是变成"rx_set_jr_estim_fs_to_cnt "

    会发生这种情况,因为在 c 和 c++ 中,字符串字面量默认为 const char* 类型,当您向指针添加数字时,指针将按此值递增。

    您可以改用字符串流:

    std::stringstream ss;
    ss << "trx_set_jr_estim_fs_to_cnt " << NUM_FRAMES_TO_COUNT_IN_TS_CLK_EST;
    map<const string_t, device_t*> setRegMsg={{ss.str(),&trx},[...]
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 2011-02-06
      • 2010-09-20
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      相关资源
      最近更新 更多