StopIteration
is a special exception in Python which is thrown by iterators when you try to get a new element but there aren't any more. That is the way how Python detects when to stop a loop like for x in iterator
. That basically translates in Python to:
while True:
try: value = next(iterator)
except StopIteration: break
# do something...
However, iterators in the STL of C++ are much different. I am not sure at what level you want to see a C++ translation. If you have similar iterators like in Python, you might be able to translate the code 1:1. But a STL iterator does not behave this way; it does not throw exceptions in the case you try to read a next element when there is none. The behavior is rather undefined and most likely will result in a crash. In STL for all containers, there is always a special end-iterator (which is outside of the container!) and you check wether your iterator is equal to the end-iterator. If that is the case, you know that you must not read any further element. This mostly looks like this:
auto iterator = mylist.begin(); // get the iterator at the start of mylist
while(iterator != mylist.end()) { // check if our iterator is not equal to the end-iterator
auto value = *iterator; // get the value of the iterator
// do something with value
++iterator; // advances the iterator by one
}
Note that next()
in Python does two things: If there is a value, it returns the current value and advances the iterator by one. Otherwise, it raises the StopIteration
exception. In C++, the current value is returned via *iterator
and the iterator is advanced by ++iterator
. Also, in C++, there is no exception, so you have to do the check yourself (that is iterator != mylist.end()
).
A shorter and more common way to write this in C++ is:
for(auto iterator = mylist.begin(); iterator != mylist.end(); ++iterator) {
auto value = *iterator; // get the value of the iterator
// do something with value
}
Or, even more short:
for(auto value : mylist) {
// do something with value
}
The code works like this for all STL containers like list
, vector
, deque
, set
, multiset
. A vector
is mostly just an array inside. But the C-array (e.g. int numbers[N];
) will not work for the first two code examples, because it is not an object and does not have the begin()
and end()
functions. However, the last example will also work in that case.
If you write an own container class, you should also implement some begin()
and end()
functions which makes the above code work like this.
Note that file reading in C++ is again different. In Python, when you use for x in myfile
, it automatically generates an iterator over the lines of the file and you are using that iterator. But of course there are also other ways in Python to read a file, e.g. just myfile.read()
. The latter has a direct equivalent in C, namely fread()
(or also simply read()
, but you should rather use fread()
because it is more save and you get caching). But there are so many different ways in C/C++ to read a file that I will not list all possibilities here.
sys.exit()
is mostly equivalent to the C-function exit()
. But you should better just return
from all the functions and at the end return
from main()
so that your application exits.