123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <fstream> using namespace std; int main() { vector<string> words; string inputFileName, reply; ifstream inputFile; cout << "Input file name: "; getline(cin, inputFileName); inputFile.open(inputFileName.c_str()); //If the file doesn't open, then display error and then ask to press enter to close if (inputFile.is_open()) { while(!inputFile.eof()) { string word; while(inputFile >> word) { words.push_back(word); } sort(words.begin(), words.end()); for(size_t i=0; i<words.size(); i++) { cout << words[i] << endl; } } } else { cout << "Unable to open input file." << endl; cout << "Press enter to continue..."; getline(cin, reply); exit(1); } inputFile.close(); return 0; }
123456789101112
for(size_t i = 0; i < words.size(); i++) { for(size_t j = i + 1; j < words.size(); j++) { if(words.at(i) == words.at(j)) { words.at(j) = words.at(words.size() - 1); words.pop_back(); j--; } } }