Revizia anterioară Revizia următoare
C++ compiler upgrades on infoarena
We now have the --std=c++0x compiler option enabled on infoarena. We also updated our g++ compiler to version 4.8.
What does it mean?
C++ users can now use a bunch of cool features, some of which are described below briefly. Keep in mind that these features are not yet available at OJI, ONI, etc., so don't use them at any of these competitions unless they are allowed explicitly by the regulations.
1. auto
You can now let the compiler guess the type of some variable:
auto a = 45;
auto b = 4.5;
auto c = vector<int>(10);
auto can also be used with const auto or const auto&. It works in the same way other types would work. In most cases, auto cannot be used in function signatures.
2. range-based for loops
In C++11, you can iterate over each element in a list with less code:
int array[5] = {1, 2, 3, 4, 5};
for (int x: array) {
cout << x << endl;
}
If you want to modify the elements in the list, you need to get a reference to the current element like this:
double array[5] = {1.5, 2.7, 3.9};
for (auto &x: array) {
x *= 2;
}
Note: This code compiles without using &, but the original array is not modified unless a reference is used.
3. initializer lists
Simple one line initializations with lists of constant values:
vector<pair<int, int>> dirs = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
Note that since C++11 you no longer need to introduce a space between closing right angle brackets (>>).
unordered_set, unordered_map
These are hash-based implementations of the STL set and map containers, meaning that most operations have average time complexity O(1).