C++ 03:
for (vector::iterator itr = v.begin(); itr!=v.end(); ++ itr)
cout << (*itr);
C++ 11:
for (auto i: v) { // works on any class that has begin() and end()
cout << i ; // readonly access
}
for (auto& i: v) {
i++; // changes the values in v
} // and also avoids copy construction
Auto Syntax:
auto x = begin(v); // Same as: int x = v.begin();
int arr[4] = {3, 2, 4, 5};
auto y = begin(arr); // y == 3
auto z = end(arr); // z == 5
// How this worked? Because begin() and end() are defined for array.
// Adapt your code to third party library by defining begin() and end() for their containers.
for (vector
cout << (*itr);
C++ 11:
for (auto i: v) { // works on any class that has begin() and end()
cout << i ; // readonly access
}
for (auto& i: v) {
i++; // changes the values in v
} // and also avoids copy construction
Auto Syntax:
auto x = begin(v); // Same as: int x = v.begin();
int arr[4] = {3, 2, 4, 5};
auto y = begin(arr); // y == 3
auto z = end(arr); // z == 5
// How this worked? Because begin() and end() are defined for array.
// Adapt your code to third party library by defining begin() and end() for their containers.
Comments
Post a Comment