The 1st one: ------------ #include #include #include #include #include #include using namespace std; struct LineOfText { LineOfText() : m_LineNumber(1) {} unsigned int m_LineNumber; string m_Text; }; ostream& operator<<(ostream &s, const LineOfText &line) { s << line.m_Text << " (line " << line.m_LineNumber << ")"; return s; } struct SortByText { bool operator()(const LineOfText &l, const LineOfText &r) const { return l.m_Text < r.m_Text; } }; struct IsTextEqual { bool operator()(const LineOfText &l, const LineOfText &r) const { return l.m_Text == r.m_Text; } }; struct SortByLine { bool operator()(const LineOfText &l, const LineOfText &r) const { return l.m_LineNumber < r.m_LineNumber; } }; typedef vector Lines; Lines g_Lines; int main(int argc, char* argv[]) { // Open the input file. ifstream ifs("C:\\a.txt"); if (!ifs) { return 1; } // Read lines of input file into a vector. LineOfText line; while (getline(ifs, line.m_Text)) { g_Lines.push_back(line); ++line.m_LineNumber; } // Elimiate duplicate text but keep what remains in line order. stable_sort(g_Lines.begin(), g_Lines.end(), SortByText()); Lines::iterator new_end = unique(g_Lines.begin(), g_Lines.end(), IsTextEqual()); sort(g_Lines.begin(), new_end, SortByLine()); // Output the results. copy(g_Lines.begin(), new_end, ostream_iterator(cout, "\n")); return 0; } Steve