number of 0s before 1 (count_if)
This commit is contained in:
parent
413ca3faef
commit
b8eff8c9ea
20
main.cpp
20
main.cpp
@ -22,10 +22,10 @@ void printlm(T m){
|
||||
int prompti(string,bool*);
|
||||
int prompti(string,bool*);
|
||||
string prompts(string,bool*);
|
||||
bool isneg(int);
|
||||
bool num0b1(int);
|
||||
|
||||
int main() {
|
||||
printlm("Get the number of negatives from::");
|
||||
printlm("Get the number of 0s before 1 from:");
|
||||
bool s {};
|
||||
ilist lst {};
|
||||
do {
|
||||
@ -34,10 +34,8 @@ int main() {
|
||||
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;}));
|
||||
printm("Number of 0s before 1: ");
|
||||
printlm(count_if(lst.cbegin(),lst.cend(), num0b1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -80,6 +78,12 @@ string prompts(string message, bool *status) {
|
||||
return tr;
|
||||
}
|
||||
|
||||
bool isneg(int x) {
|
||||
return x < 0;
|
||||
bool num0b1(int x) {
|
||||
static bool has1 {};
|
||||
if (x == 1) has1 = true;
|
||||
if (has1 || x != 0) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user