PCLP2/lab05/tema.cpp

52 lines
913 B
C++
Raw Normal View History

2020-04-12 09:02:41 +00:00
#include <iostream>
#include <string>
#include <list>
#include <sstream>
#include <algorithm>
using namespace std;
void insertToPlace(list<string>* v, const string* word){
if(!v->size() || *word < v->front()){
v->push_front(*word);
return;
}
if(*word == v->front()){
return;
}
for(list<string>::iterator it = next(v->begin()); it != v->end(); it++){
if(*word == *it){
return;
}
if(*word > *prev(it) && *word < *it){
v->insert(it, *word);
return;
}
}
v->push_back(*word);
}
int main(){
ios::sync_with_stdio(false);
cout<<"Introduce a line: ";
string line;
getline(cin, line);
list<string> v;
istringstream sstream(line);
string word;
while(getline(sstream, word, ' ')){
insertToPlace(&v, &word);
}
cout<<"Distinct words ordered alphabetically:\n";
for(list<string>::iterator it = v.begin(); it != v.end(); it++){
cout<<*it<<"\n";
}
cout.flush();
return 0;
}