Hi,

I have a data I want to make fix length records Like suppose:-

one two three four
one two three
one

In this case the fixed length should be of the maximum string length which is of the first line. My code below is giving creating faulty fix lengths. Can somebody help.

so the output will be like this

one two three four
     one two three
               one

Here is my code and I have to do it with multimap:-

#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(); 

 for (it=(mymm.begin()); it!=(mymm.end()); it++){ //cout pairs sorted lexicographically
   cout.width(maxStringSize);
   cout<<it->first<<"-"<<it->second<<endl;
 }
    return 0;
 }

Thanks

Edited 6 Years Ago by Web_Sailor: n/a

So just to clarify in your example the output should look like:

<space><space>y12-100<space> z1234-250<space><space>  x1adsf-5
<space><space>y12-150<space> z1234-300
 <space><space>y12-200

this? (with width = 8)

I'm just a tad confused...

Edited 6 Years Ago by jonsca: keep losing the spaces

So just to clarify in your example the output should look like:

<space><space>y12-100<space> z1234-250<space><space>  x1adsf-5
<space><space>y12-150<space> z1234-300
 <space><space>y12-200

this? (with width = 8)

I'm just a tad confused...

It should be like this.

x1adsf-50
   y12-100
   y12-150
   y12-200
 z1234-250
 z1234-300

I haven't managed to cobble together a solution just yet, but essentially what you have to do it get the total length of each line by converting your ints to strings (via stringstreams), geting the length for each line (stored in a vector, say) finding the maximum length. Then you can iterate through the mmap this time to output and put in the number of spaces difference between maxstring and the length on the current line.

EDIT: Your cout.width() was causing me some trouble so I changed it to a setw(). Not sure if that will make any difference

#include<sstream>
#include <iomanip>   //add these to your existing headers
#include <vector>

 //all this replacing your line int maxStringSize=max_element ... and below

stringstream ss;
int maxlength = -1;
vector<int> linelength;
 
for (it=(mymm.begin()); it!=(mymm.end()); it++){
  
   ss.clear();
   ss.str(""); 
   ss <<it->second;
   linelength.push_back(it->first.length()+ss.str().length());
   if(linelength[linelength.size()-1] > maxlength)
	   maxlength = linelength[linelength.size()-1];
  
 }


setw(maxlength);

for (it = mymm.begin(),vecindex = 0;it!=mymm.end() && vecindex<linelength.size()<;it++,vecindex++){
	 
	 for (int i = 0;i<maxlength-linelength[vecindex];i++)
		cout <<" ";
	 cout<<it->first<<"-"<<it->second<<endl;
	vecindex++;
 }

My usual caveat: there's probably a better way to do it. The second for loop isn't very pretty. You get the idea though.

Edited 6 Years Ago by jonsca: added code

This article has been dead for over six months. Start a new discussion instead.