Diferente pentru blog/cpp11 intre reviziile #52 si #41

Nu exista diferente intre titluri.

Diferente intre continut:

We now have the $--std=c++0x$ compiler option enabled on infoarena. We also updated our $g++$ compiler to $4.8$.
We now have the $--std=c++0x$ compiler option enabled on infoarena. We also updated our $g++$ compiler to 4.8.
h2. What does it mean?
$C++$ users can now use a bunch of cool features, some of which are briefly described below. 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.
$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.
h3. 1. auto
You can now let the compiler infer the type of your variables with $auto$:
You can now let the compiler guess the type of some variable:
== code(cpp) |
auto a = 45;
auto c = vector<int>(10);
==
$auto$ can also be used with $const auto$ or $const auto&$. In most cases, $auto$ cannot be used in function signatures.
$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.
h3. 2. range-based for loops
In $C++11$, you can write less code to iterate over every element in a list of elements:
In $C++11$, you can iterate over each element in a list with less code:
== code(cpp) |
int array[5] = {1, 2, 3, 4, 5};
}
==
If you want to modify the elements in the list, you need to get a reference to the current element:
If you want to modify the elements in the list, you need to get a reference to the current element like this:
== code(cpp) |
double array[5] = {1.5, 2.7, 3.9};
vector<pair<int, int>> dirs = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
==
Note that in $C++11$ you no longer need to introduce a space between closing right angle brackets $(>>)$.
Note that since $C++11$ you no longer need to introduce a space between closing right angle brackets $(>>)$.
h3. 4. unordered_set, unordered_map
cout << "Anna is " << age["anna"] << " years old." << endl;
==
Using STL vectors, pairs or user defined objects as keys is a little trickier because you also need to provide a hash function.
Using STL vectors, pairs or user defined objects as keys is a little trickier because you also need to provide the hash function.
== code(cpp) |
#include <unordered_set>
vector<int> a = {5, 3, 1, 3};
vector<int> b = {6, 1, 7, 2};
vector<int> ind = {0, 1, 2, 3};
 
sort(ind.begin(), ind.end(), [&a, &b](int x, int y) -> bool {
  return a[x] < a[y] || (a[x] == a[y] && b[x] < b[y]);
});
 
for (int i: ind) {
  cout << a[i] << " " << b[i] << endl;
}
==
(Often, a cleaner alternative to using lambda functions for sorting is to define a class holding all the data for each entry and to implement the $<$ operator.)
 
_Let us know in the comments section what are the $C++11$ features that you use for programming contests. Code snippets illustrating your favorite tricks are welcome._
(Often, a cleaner alternative to using lambda functions for sorting is to define a class holding all the data needed and to provide an implementation for the $<$ operator.)

Diferente intre securitate:

protected
private

Diferente intre topic forum:

8918