- #1
Eclair_de_XII
- 1,083
- 91
- TL;DR Summary
- I am primarily a Python user. I am trying to learn C++ because of a superficial fascination with speed and job prospects. Currently, I am experimenting with C++. I am trying to find a short algorithm to find out if a given element is in a list. I'm not able to figure out one as simple as Python's built-in algorithm. Is there one?
C++:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(void) {
vector <int> v = {0, 5, 9, 3, 2, 1};
bool nine_exists = false;
for (int i: v){
if (i == 9) {
nine_exists = true;
break;
}
}
string message = nine_exists ? "No nines found" : "At least one nine found.";
cout << message << endl;
}
Python:
v = [0, 5, 9, 3, 2, 1]
message = ("At least one nine found" if 9 in v else "No nines found.")
print(message)