1
0
LearningCPP/main.cpp
2022-11-04 12:22:52 +00:00

86 lines
1.5 KiB
C++

#include <iostream>
#include <list>
#include <algorithm>
#include <map>
#include <iomanip>
using string = std::string;
using ilist = std::list<int>;
//Place in header!
template <typename T>
void printm(T m){
std::cout << m;
}
template <typename T>
void printlm(T m){
printm(m);
printm("\n");
}
int prompti(string,bool*);
int prompti(string,bool*);
string prompts(string,bool*);
bool isneg(int);
int main() {
printlm("Get the number of negatives from::");
bool s {};
ilist lst {};
do {
auto v = prompti("Enter an integer: ", &s);
if (s) {
lst.push_back(v);
}
} while (s);
printm("Number of negatives (Defined): ");
printlm(count_if(lst.cbegin(), lst.cend(), isneg));
printm("Number of negatives (Lambda): ");
printlm(count_if(lst.cbegin(), lst.cend(), [] (int x) { return x < 0;}));
return 0;
}
int prompti(string message, bool *status) {
int tr {};
*status = false;
std::cout << message;
if (!(std::cin >> tr)){
std::cin.clear();
std::cin.ignore();
return 0;
}
*status = true;
return tr;
}
double promptd(string message, bool *status) {
double tr {};
*status = false;
std::cout << message;
if (!(std::cin >> tr)){
std::cin.clear();
std::cin.ignore();
return 0.0;
}
*status = true;
return tr;
}
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;
}
bool isneg(int x) {
return x < 0;
}