I am trying to translate som code from python into c++, my question is how do I translate 'in' into c++.Is there any similar keywords in c++?
Asked
Active
Viewed 117 times
-2
-
1Basically you write a loop or use an [`
`](https://en.cppreference.com/w/cpp/algorithm) – NathanOliver Dec 13 '19 at 20:16 -
No, there isn't. Read about [C++ Containers](http://www.cplusplus.com/reference/stl/) – CoderCharmander Dec 13 '19 at 20:16
-
There is not a similar keyword in c++. – François Andrieux Dec 13 '19 at 20:16
-
In general, you shouldn't be trying to transliterate code. You read and understand the code in one language and then write it in a way that makes sense in another language. C++ and Python have very different idioms, so you shouldn't be trying to map keywords etc. – juanpa.arrivillaga Dec 13 '19 at 20:31
1 Answers
1
If you're talking about looping over a set of items, that'd be a range-based for loop. The syntax is as follows:
std::vector<int> vec;
//init vector
for(int i : vec) {
//do something with i
}
If you're talking about the operator that checks if a given value is inside of a set, that doesn't have a direct parallel, and is often solved by writing a loop to go over the set and check the values (or some other access method like searching a BST).
std::find
works for all containers - this might be the functionality you're looking for? Check the docs for how to use it with your individual data type.

Nick Reed
- 4,989
- 4
- 17
- 37
-
That is exactly what I'm looking for, I want to find out how meny times do an element exist in a set of elements. – Adam Alrefai Dec 13 '19 at 20:31
-
2@AdamAlrefai the algorithm you should reach for is [`std::count` or `std::count_if`](https://en.cppreference.com/w/cpp/algorithm/count). You could also edit your question to clarify what you're looking for, so that future readers can benefit more from your question – alter_igel Dec 13 '19 at 21:12
-
1@AdamAlrefai To count how many times a single element exists, there is [`std::count`](https://en.cppreference.com/w/cpp/algorithm/count). If you want to count how many times *each* element occurs, see [this question](https://stackoverflow.com/questions/28511557/how-do-i-count-the-number-of-occurrences-in-an-array). Take a moment to look at [this list of standard c++ algorithms](https://en.cppreference.com/w/cpp/algorithm). – François Andrieux Dec 13 '19 at 21:12