Help with displaying output
Code:
// Output the cross reference.
void Xref::generateCrossReference( )
{
typedef map<string,list<int>, less<string> > MapType;
typedef MapType::const_iterator MapIteratorType;
MapType xrefMap;
string current;
// Insert identifiers into the map.
while( ( current = tok.getNextID( ) ) != "" )
xrefMap[ current ].push_back( tok.getLineNumber( ) );
// Iterate through map and output
// identifiers and their line number.
MapIteratorType itr;
for( itr = xrefMap.begin( ); itr != xrefMap.end( ); ++itr )
{
const list<int> & theLines = (*itr).second;
list<int>::const_iterator lineItr = theLines.begin( );
list<int>::const_iterator demo = theLines.begin( );
list<int>::const_iterator beg = theLines.begin( );
list<int>::const_iterator end = theLines.begin( );
// Print identifier and first line where it occurs.
cout << (*itr).first << ": " << *lineItr;
// Print all other lines where the word exists
for( ++lineItr; lineItr != theLines.end( ); ++lineItr )
if(*lineItr == *demo)
{
cout << "";
}
else if (*lineItr - *demo == 1)
{
while(*lineItr - *--lineItr == 1)
{
lineItr++;
}
cout << "-" << *lineItr;
}
else
{
cout << ", " << *lineItr;
++demo;
}
cout << endl;
}
}
I've been working with the Cross-Reference Generator. For example, if I had a simple file with the first 3 lines of it containing the word 'test', the output would be test: 1, 2, 3. I modified it so that if the word appears on the same line more than once than it only prints the line number once. I am now working on, and having trouble with, displaying the range for words that appear on consecutive lines. For example, I want the output to be test: 1-3 and not test: 1, 2, 3. The else if statement I have at the bottom is what I have come up with and I know is totally wrong, so could someone help me and point me in the right direction? Thanks for any help.