1
votes

I am trying to read a file and put values in a multimap of multimap. Insertion doesn't throw any errors, but when I try to print the values, the output is not not as expected.

My input file:

Server1, Database, MySQL, 5.5
Server2, Database, MySQL, 5.1
Server2, OS, Ubuntu, 12.04
Server3, Language, Python, 2.6.3

I want to store the values like this:

MySQL,5.5,Server1
MySQL,5.1,Server2

My code:

#include<bits/stdc++.h>
using namespace std;

typedef multimap<string,string> innermap;

multimap<string,innermap> mp;

void print()
{
    multimap<string, innermap >::iterator it;
    multimap<string, string>::iterator inner_it;

    for ( it=mp.begin() ; it != mp.end(); it++ ) 
    {
        cout<<(*it).first<<endl;
        for( inner_it=(*it).second.begin(); inner_it != (*it).second.end(); inner_it++)
            cout << (*inner_it).first<<"  " << (*inner_it).second << endl;
    }
}
int main() 
{
    string str_temp;
    vector<string>str;

    std::ifstream infile("abc.txt");

    while ( getline (infile,str_temp) )
    {
       int i=0;

       stringstream ss(str_temp);
       char delim=',';
       string server;
       string software;
       string version;
       string item;

       while (getline(ss,item,delim))
       {

           i++;
           if(i==1)
               server=item;
           else if(i==3)
               software=item;
           else if(i==4)
               version=item;
           //else
               // i++;

       }    

       cout<<"server  "<<server<<"software  "<<software<<"version   "<<version<<endl;
       multimap<string, innermap >::iterator it;
       it=mp.insert(make_pair(software,innermap()));            
       it->second.insert(make_pair(version,server));

       //print();  
    }
    print();
    return 0;
}

I debugged my code, and the values are read in correct from my file before insertion. But when I call print(), all but one version are missing:

MySQL
Server1
MySQL
Server2
Python
2.6.3 Server3
Ubuntu
Server2

Why aren't all version strings printed?

Did you try stepping through your code with a debugger? - Algirdas Preidžius
yes before insert version i cheked version value available.. - RAW
When stepping through your code with a debugger, one should check all values of variables, at each, and every step of execution, and inspect whether they are the expected values. Did you do that? - Algirdas Preidžius
at line 55, version value is present just before insertion. - RAW
@user2279196 : so finally so found your bug ! congratulations - JeJo