Hi,

I was wondering if there is any way to get the largest size of a multimap key / value. I mean something like this:-

if there are 4 values in a multimap:-

my 
name
is 
web_sailor

Suppose I want to get the maximum size key / value. Like here I should get the size of "web_sailor" 10 which is the maximum size and 2 is the minimum size for in / my. This is because I want to set the record lengths as fixed with printf function as per my requirement.

One more question:-

suppose I have a file like below and I would like to create 3 output files based on my first field .. .i.e, 1 /2/3.

Thats means 3 lines will go to the three files

1    AAAAAA    BBBBBB    CCCCC    
2    DDDDDD    EEEEEE    FFFFF
3    GGGGGG    HHHHHH    IIIII
2    copy       paste    insert
1    andrew    mathew    horto

I expect something like this to happen

file1:-
1    AAAAAA    BBBBBB    CCCCC
1    andrew    mathew    horto

file2:-
2    DDDDDD    EEEEEE    FFFFF
2    copy       paste    insert
        
file3:-
3    GGGGGG    HHHHHH    IIIII

Here is my code so far but I am not able to create file dynamically. I mean here I am predefining the file names.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
void parseFile(string file, string line)
{
    ofstream out(file.c_str(),ios::app);
    out<<line<<endl;
    out.close();
}
int main()
{
    ifstream in("inputfile.txt");
    string line = "";
    int count = 0;
    while(getline(in,line))
    {
        string file = "file";
        count++;
    
        switch (count)
        {
        case 1:
            parseFile("file1.txt",line); break;
        case 2: 
            parseFile("file2.txt",line); break;
        case 3:
            parseFile("file3.txt",line); break;
        case 4: 
            parseFile("file2.txt",line); break;
        case 5:
            parseFile("file1.txt",line); break;
        }
    }
    cin.ignore();
    cin.get();
    return 0;
}

Isn't there any way to automate this process ? I mean here I am defining the filenames in switch statement. i mean something like this :-

ofstream files[3];
    
    for (int i = 0; i < 3; i++) {
        string name = "file";
        name += '0' + i + 1;
        name += ".txt";
        cout << name << endl;
        files[i].open(name.c_str());
    }

but here I don't know howto make a character string from an integer number in C++

for C it would be char line[100]; sprintf(line, "%d", i); // Googling :)

Need help in this with some example snippets.

Thanks

For the question about multimaps: multimaps are automatically sorted, and strings comparison (>) is done primary by length of the string, so the last element in the multimap is the longest string. In your case you could use multiMapOfStrings.rbegin()->size() (rbegin() gives iterator to the last element, end() gives iterator past last element). Of course it is only applicable if you want to get maximum size of the first "column" of the multimap, for the second column you could use for_each loop.

If you make function like here: http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.1 you can easily convert int to string, therefore making loop for all the files.

About maximum size of multimap I am still confused:-
Lemme give a more insight I guess to make the problem clear.
Lets say I have this multimap as below

mymm.insert(pair<char,int>('x1',50));
  mymm.insert(pair<char,int>('y12',100));
  mymm.insert(pair<char,int>('y12',150));
  mymm.insert(pair<char,int>('y12',200));
  mymm.insert(pair<char,int>('z1234',250));
  mymm.insert(pair<char,int>('z1234',300));

In view of the above I want to know the length of each key. I mean length and then set the format to the maximum size with printf. Here z1234 has a length of 5. So I will set the size of key as fixed with 5. Likewise for value.

I hope I am clear now.

Thanks

I'm sorry, strings are not sorted by length, but lexicographically. I would solve this problem using strings and cout, not printf. My idea is here:

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

bool pairStringComp(pair<string,int> i,pair<string,int> j){ //compare pairs by string size
return i.first.size()<j.first.size();
}
int main(){
    multimap<string,int> mymm;
    multimap<string,int>::iterator it;
    mymm.insert(pair<string,int>("x1adsf",50));
    mymm.insert(pair<string,int>("y12",100));
    mymm.insert(pair<string,int>("y12",150));
    mymm.insert(pair<string,int>("y12",200));
    mymm.insert(pair<string,int>("z1234",250));
    mymm.insert(pair<string,int>("z1234",300));

    int maxStringSize=max_element(mymm.begin(), mymm.end(),pairStringComp)->first.size(); //get size of the longest element of multimap as cout width
    for (it=(mymm.begin()); it!=(mymm.end()); it++){  //cout pairs sorted lexicographically 
        cout.width(maxStringSize);
        cout<<it->first<<"-"<<it->second<<endl;
    }
    return 0;
}

Thanks a lot :) Great example. I appreciate your goodwill gesture.

I have marked the thread as solved :)

This question has already been answered. Start a new discussion instead.