I was loading data from database and inserting it into a map. When I tried to print the map size and data, it still shows size 1 and only the last row prints. All previous data is overwritten and contains last value. Any pointer in this
issue. I have changed variable names because of a company issue.
I have done some check Point also. All key values are unique.
typdef long_char char[38];
for(int j = 0; j < 31; j++)
{
sample_enum param_sub_type = result_set[j];
long_char param_name;
strncpy(param_name,result_set[j], sizeof(param_name));
input_status_cd.insert(std::pair<long_char,sample_enum>(param_name, param_sub_type));
/*Insert Into Map */ <I suspect this may be issue but not sure>
}
/*Printing Size of map */
input_status_sd::size_type input_status_cd_size;
etlog_msg(intput_status_cd_size :] [%d]",intput_status_cd.size());
That is because
long_charis defined aschar[38]so when you insert into the map it will use try to useoperator<which will result in comparing the address of the first char of the array. Since you are creating this array in the loop, most probably the same stack frame gets allocated so the address of the first char remains same for every object. Hence all the previous data gets overwritten as themapthinks that you are inserting the same object again. The simplest way to solve this would be to usestd::stringas a typedef forlong_char.