1
0

remove duplicates of input string words

This commit is contained in:
Captain ALM 2022-10-28 12:18:30 +01:00
parent 22e18284ff
commit ccee442f97
3 changed files with 31 additions and 18 deletions

View File

@ -1,6 +1,7 @@
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <iomanip>
using string = std::string;
@ -19,21 +20,22 @@ void printlm(T m){
int prompti(string,bool*);
double promptd(string,bool*);
double avgitr(std::list<double>*);
double avgfor(std::list<double>*);
void delzrfrst(std::list<double>&);
string prompts(string,bool*);
double avg(std::list<double>*);
void remdups(std::list<string>&);
int main() {
printlm("Remove first zero from:");
printlm("Remove duplicates for input:");
bool s {};
std::list<double> lst {};
std::list<string> lst {};
do {
auto v = promptd("Enter a double value: ", &s);
auto v = prompts("", &s);
if (s) lst.push_back(v);
} while (s);
printlm("First zero removed:");
delzrfrst(lst);
printlm("Current data:");
for (const auto c : lst) printlm(c);
printlm("With Duplicates removed:");
remdups(lst);
for (const auto c : lst) printlm(c);
return 0;
}
@ -64,23 +66,34 @@ double promptd(string message, bool *status) {
return tr;
}
double avgitr(std::list<double> *values) {
double sum {};
for (auto itr = values->cbegin(); itr != values->cend(); ++itr) sum += *itr;
return sum / values->size();
string prompts(string message, bool *status) {
string tr {""};
*status = false;
std::cout << message;
if (!(std::cin >> tr)){
std::cin.clear();
std::cin.ignore();
return "";
}
*status = true;
return tr;
}
double avgfor(std::list<double> *values) {
double avg(std::list<double> *values) {
double sum {};
for (const auto c : *values) sum += c;
return sum / values->size();
}
void delzrfrst(std::list<double> &values) {
for (auto itr {values.cbegin()}; itr != values.cend(); ++itr) {
if (*itr == 0) {
void remdups(std::list<string> &values) {
std::map<string,bool> has {};
auto itr {values.cbegin()};
while (itr != values.cend()) {
if (has[*itr]) {
itr = values.erase(itr);
break;
} else {
has[*itr] = true;
++itr;
}
}
}

BIN
main.o

Binary file not shown.

BIN
t

Binary file not shown.