C++ std lib study1

来源:互联网 发布:淘宝餐饮制售合法吗 编辑:程序博客网 时间:2024/05/20 11:20

1.the new C++ 11 features.

This books covers C++11, which long had the working title “C++0x,” with the expectation that it   would be done no later than 2009.1

C++11  , C++0x , the same meanings.


The requirement to put a space between two closing template expressions has gone:
vector<list<int> >; // OK in each C++ version
vector<list<int>>; // OK since C++11


nullptr and std::nullptr_t
C++11 lets you use nullptr instead of 0 or NULL to specify that a pointer refers to no value (which differs from having an undefined value). This new feature especially helps to avoid mistakes that occurred when a null pointer was interpreted as an integral value. For example:
void f(int);
void f(void*);
f(0); // calls f(int)
f(NULL); // calls f(int) if NULL is 0, ambiguous otherwise
f(nullptr); // calls f(void*)
nullptr is a new keyword.


Automatic Type Deduction with auto

auto i = 42; // i has type int
double f();
auto d = f(); // d has type double
The type of a variable declared with auto is deduced from its initializer. Thus, an initialization is  required:
auto i; // ERROR: can’t dedulce the type of i
Additional qualifiers are allowed. For example:
static auto vat = 0.19;
Using auto is especially useful where the type is a pretty long and/or complicated expression. For   example:
vector<string> v;
...
auto pos = v.begin(); // pos has type vector<string>::iterator
auto l = [] (int x) -> bool { // l has the type of a lambda
..., // taking an int and returning a bool
};
The latter is an object, representing a lambda,


Uniform Initialization and Initializer Lists

Before C++11,  Initialization could happen with parentheses, braces, and/or  assignment operators.  (其实只要区别开,就不会confuse lar)
For this reason, C++11 introduced the concept of uniform initialization, which means that for  any initialization, you can use one common syntax. This syntax uses braces, so the following is   possible now:   
int values[] { 1, 2, 3 };
std::vector<int> v { 2, 3, 5, 7, 11, 13, 17 };
std::vector<std::string> cities {
"Berlin", "New York", "London", "Braunschweig", "Cairo", "Cologne"
};
std::complex<double> c{4.0,3.0}; // equivalent to c(4.0,3.0)

注意种所谓通用的初始化方式没有= between the variable name and the intial list.,

seems VS2010, still not implement this feature.


An initializer list forces so-called value initialization, which means that even local variables of fundamental data types, which usually have an undefined initial value, are initialized by zero (or nullptr, if it is a pointer): , 也就是他强迫你用了value initialization.


int i; // i has undefined value
int j{}; // j is initialized by 0
int* p; // p has undefined value
int* q{}; // q is initialized by nullptr


Note, however, that narrowing initializations — those that reduce precision or where the supplied  value gets modified— are not possible with braces. For example:
int x1(5.3); // OK, but OUCH: x1 becomes 5
int x2 = 5.3; // OK, but OUCH: x2 becomes 5
int x3{5.0}; // ERROR: narrowing
int x4 = {5.3}; // ERROR: narrowing
char c1{7}; // OK: even though 7 is an int, this is not narrowing
char c2{99999}; // ERROR: narrowing (if 99999 doesn’t fit into a char)
std::vector<int> v1 { 1, 2, 4, 5 }; // OK
std::vector<int> v2 { 1, 2.3, 4, 5.6 }; // ERROR: narrowing doubles to ints


C++11 introduces a new form of for loop, //VS2010 also not support

for ( int i : { 2, 3, 5, 7, 9, 13, 17, 19 } ) {
std::cout << i << std::endl;
}
To multiply each element elem of a vector vec by 3 you can program as follows:
std::vector<double> vec;
...
for ( auto& elem : vec ) {
elem *= 3;
}
Here, declaring elem as a reference is important because otherwise the statements in the body of the  for loop act on a local copy of the elements in the vector (which sometimes also might be useful).


This means that to avoid calling the copy constructor and the destructor for each element, you  should usually declare the current element to be a constant reference. Thus, a generic function to   print all elements of a collection should be implemented as follows:
template <typename T>
void printElements (const T& coll)
{
for (const auto& elem : coll) {
std::cout << elem << std::endl;
}
}


Here, the range-based for statement is equivalent to the following:
{
for (auto _pos=coll.begin(); _pos != coll.end(); ++_pos ) {
const auto& elem = *_pos;
std::cout << elem << std::endl;
}
}









原创粉丝点击