// C++98
map<int,string>::iterator i = m.begin();
// C++11
auto i = begin(m);
// C++98
binder2nd< greater<int> > x = bind2nd( greater<int>(), 42 );
// C++11
auto x = [](int i) { return i > 42; };
// C++98
widget* pw = new widget();
:::
delete pw;
// C++11
auto pw = make_shared<widget>();
// C++11
class gadget;
class widget {
private:
shared_ptr<gadget> g;
};
class gadget {
private:
weak_ptr<widget> w;
};
// C++11 Pimpl Idiom
class widget {
:::
private:
class impl;
unique_ptr<impl> pimpl;
};
// .cpp
class impl {
:::
};
widget::widget()
: pimpl( new impl() )
{
}
// C++11
class node {
vector< unique_ptr<node> > children;
node* parent;
public:
:::
};
// C++98
int* p = 0;
// C++11
int* p = nullptr;
// C++98
for( vector<double>::iterator i = v.begin(); i != v.end(); ++i ) {
total += *i;
}
// C++11
for( auto d : v ) {
total += d;
}
vector<int> v;
int a[100];
// C++98
sort( v.begin(), v.end() );
sort( &a[0], &a[0] + sizeof(a)/sizeof(a[0]) );
// C++11
sort( begin(v), end(v) );
sort( begin(a), end(a) );
// C++98: ( std::find_if )
vector<int>::iterator i = v.begin(); // i
for( ; i != v.end(); ++i ) {
if( *i > x && *i < y ) break;
}
// C++11: std::find_if
auto i = find_if( begin(v), end(v), [=](int i) { return i > x && i < y; } );
</
source>
- , ? β , , , , , .. .
<source>// C#
lock( mut_x ) {
... use x ...
}
// C++11 without lambdas: already nice, and more flexible (e.g., can use timeouts, other options)
{
lock_guard<mutex> hold { mut_x };
... use x ...
}
// C++11 with lambdas, and a helper algorithm: C# syntax in C++
// Algorithm: template<typename T> void lock( T& t, F f ) { lock_guard hold(t); f(); }
lock( mut_x, [&]{
... use x ...
});
// C++98:
vector<int>* make_big_vector(); // 1: : , delete
:::
vector<int>* result = make_big_vector();
void make_big_vector( vector<int>& out ); // 2: : ,
:::
vector<int> result;
make_big_vector( result );
// C++11:
vector<int> make_big_vector();
:::
vector<int> result = make_big_vector();
// C++98 or C++11
int a = 42; // ,
// C++ 11
auto x = begin(v); //
// C++98
rectangle w( origin(), extents() ); // oops, declares a function, if origin and extents are types
complex<double> c( 2.71828, 3.14159 );
int a[] = { 1, 2, 3, 4 };
vector<int> v;
for( int i = 1; i <= 4; ++i ) v.push_back(i);
// C++11
rectangle w = { origin(), extent() }; // = is optional but I personally prefer it
complex<double> c = { 2.71828, 3.14159 }; // = is optional but I personally prefer it
int a[] = { 1, 2, 3, 4 };
vector<int> v = { 1, 2, 3, 4 };
// C++98
X::X( /*...*/ ) : mem1(init1), mem2(init2, init3) { /*...*/ }
// C++11
X::X( /*...*/ ) : mem1{init1}, mem2{init2, init3} { /*...*/ }
// C++98
draw_rect( rectangle( myobj.origin, selection.extents ) );
// C++11
draw_rect( { myobj.origin, selection.extents } );
Source: https://habr.com/ru/post/131478/
All Articles