电子书名:The C++ Standard Library A Tutorial and Reference (2nd Edition)
直入主题!
1、语法清除
在两个模板的关闭中间的空格可以不写了。
vector<list<int> >;//OK in each C++ version
vector<list<int>>; //OK since C++11
用nullptr表示空指针。
在C++中提供的头文件如下,C++也可以用‘0’来表示空指针,但是0也是int型,当有重载时会产生问题,所以引入nullptr来表示空指针。
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
void f(int);
void f(void*);
f(0); //calls f(int)
f(NULL); //calls f(int) ifNULLis 0, ambiguous otherwise
f(nullptr); //calls f(void*)
2、auto
auto:自动的,顾名思义,可以自动地成为正确的类型,
条件是变量要初始化
,否则auto是不知道变量是什么类型的。
auto i = 42;// ihas type int
double f();
auto d = f(); // d has type double
auto i;//ERROR: can’t dedulce the type ofi
其他限定符是允许使用的,如下
static auto vat = 0.19;
可以使用大括号来对变量进行初始化。
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)
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
但是用大括号不能降低变量的精度,例如从float到int是不行的
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 (if99999 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还提供了一个类模板std::initializer_list<>来支持值列表的初始化
void print (std::initializer_list<int> vals)
{
for (auto p=vals.begin(); p!=vals.end(); ++p)
{ // process a list ofvalues
std::cout << *p << "\n";
}
}
print ({12,3,5,7,11,13,17}); // pass a list ofvalues to print()
class P
{
public:
P(int,int);
P(std::initializer_list<int>);
};
P p(77,5); // calls P::P(int,int)
P q{77,5}; // calls P::P(initializer_list)
P r{77,5,42}; // calls P::P(initializer_list)
P s = {77,5}; // calls P::P(initializer_list)
class P
{
public:
P(int a, int b) {
...
}
explicit P(int a, int b, int c) {
...
}
};
P x(77,5); // OK
P y{77,5}; // OK
P z {77,5,42}; // OK
P v = {77,5}; // OK (implicit type conversion allowed)
P w = {77,5,42}; // ERROR due to explicit (no implicit type conversion allowed)
void fp(const P&);
fp({47,11}); //OK, implicit conversion of{47,11} into P
fp({47,11,3}); //ERROR due to explicit
fp(P{47,11}); //OK, explicit conversion of{47,11} into P
fp(P{47,11,3}); //OK, explicit conversion of{47,11,3} into P
4、类似foreach的for循环
for ( int i : { 2, 3, 5, 7, 9, 13, 17, 19 } ) {
std::cout << i << std::endl;
}
template <typename T>
void printElements (const T& coll)
{
for (const auto& elem : coll) {
std::cout << elem << std::endl;
}
}
class C
{
public:
explicit C(const std::string& s); //explicit(!) type conversion from strings
...
};
std::vector<std::string> vs;
for (const C& elem : vs) { //ERROR, no conversion from string to C defined
std::cout << elem << std::endl;
}
5、R,L,u,U等编码字符串文字
R:可以让字符串中省略转义字符
原来:
"\\\\n"
现在:
R"(\\n)"
"a\\\n b\\nc()\"\n "
现在
R"nc(a\
b\nc()"
)nc";
• u8 defines a UTF-8 encoding. A UTF-8 string literal is initialized with the given characters as
encoded in UTF-8. The characters have type const char.
• u defines a string literal with characters of type char16_t.
• U defines a string literal with characters of type char32_t.
• L defines a wide string literal with characters of type wchar_t.
6、常量表达式
以下内容自 http://blog.csdn.net/csxiaoshui/article/details/39473419转载
常量表达式必须在编译期间计算出它的值并且 它的值不可以被改变。
在C++11中,我们可以通过声明constexpr让编译器确定一个变量是不是常量表达式,声明为constexpr的变量是const类型的变量,它必须由常量表达式来初始化。
例如:
在C++11中,我们可以声明某些函数是常量表达式,这样的函数必须在编译期间计算出它们的值,这样的函数必须满足以下条件:
- 返回值和参数必须是Literal类型
- 函数体必须只包含一个return语句
- 函数提可以包含其他的语句,但是这些语句不能在运行期起作用
- 函数可以不返回常量,但是在调用的时候实参必须传入常量表达式
例如:
定义为constexpr类型的函数隐式的都为内联函数(inline)