Align columns/C++: Difference between revisions

m
Fixed syntax highlighting.
m (Fixed syntax highlighting.)
 
Line 5:
A reusable template function that handles the tokenizing, and is independent of any work that might wish to be done with the results:
 
<langsyntaxhighlight lang="cpp">#include <vector>
#include <string> // for getline etc.
#include <iostream>
Line 35:
}
}
}</langsyntaxhighlight>
 
A function object that fills an array with column widths:
 
<langsyntaxhighlight lang="cpp">typedef vector< size_t > ColWidths;
 
struct MaxColWidthsDeterminer
Line 60:
else
m_colWidths[ nColIndex ] = max( m_colWidths[ nColIndex ], nWidth );
}</langsyntaxhighlight>
 
A function object that outputs fields formatted in columns:
 
<langsyntaxhighlight lang="cpp">struct FormattedLister
{
enum Alignment { eLeft, eRight, eCenter };
Line 114:
 
m_nPrevColIndex = nColIndex;
}</langsyntaxhighlight>
 
The test program, that makes a pass through the data to determine the column widths, and then three more for outputting in each of the three column alignments:
 
<langsyntaxhighlight lang="cpp">int main()
{
const string strInput(
Line 151:
outFile << endl;
}
}</langsyntaxhighlight>
9,479

edits