在C++11之前,对于一个值或者一个对象的初始化有很多种方法,我们会用到()、{ }、= 来进行初始化的工作,例如:
int a = 0;
int array[5] = {1,2,3,4,5};
vector<int> first;//初始化一个空的vector
vector<int> second(5,10);//初始化5个元素的vector对象,并全部赋值为10
vector<int> third(second.begin(),second.end());//利用second进行初始化
vector<int> fourth(third);//利用拷贝构造对third进行初始化
可以看到这些初始化的方法虽然多,对Value或Object的初始化缺乏统一的方法,而且对一些特殊的初始化操作不支持,比如:我想要对vector容器的进行初始化时传入1,2,3,4,5这些数字,那么该怎么做呢?我们可能会想到先初始化一个数组传入这些数字,利用数组给vector进行初始化,这个步骤要分为两步去做,太麻烦了。现在C++11引入了一致性的初始化方法,非常灵活和方便,对初始化操作进行了统一的处理。
int a{};//这种初始化的方式就比较友好,{}中为空,a会被默认的初始化为0
int array[]{0,1,2,3,4,5};
vector<int> v{1,2,3,4,5};
vector<string> cities{"BeiJing","ShangHai","GuangZhou","ShenZhen"};
complex<double> com{4.0,3.0};//complex是数学中的复数,第一个和第二个分别代表实数位和虚数位
使用{ }进行初始化,实际上是利用了一个事实:编译器看到{t1,t2…tn}时便做出一个initializer_list,它关联到一个array<T,n>。调用函数时该array内的元素可以被编译器分解逐一传递给函数。但是如果函数参数是一个initializer_list,这“包”数据(即{t1,t2…tn})将整体传入到函数中。
例如:上例中cities,{ }会形成一个initializer_list,背后有个array<string,4>。调用vector构造函数时,编译器找到了一个initializer_list 构造函数来接受initializer_list,而对于com来说,{ }会形成一个initializer_list,背后有一个array<double,2>,但是complex类中没有initializer_listctor,所以编译器会将array内的元素拆解开来传递给ctor。事实上,STL中所有的容器都有此类构造函数。
vector (initializer_list<value_type> il,const allocator_type& alloc = allocator_type());
list (initializer_list<value_type> il,const allocator_type& alloc = allocator_type());
deque (initializer_list<value_type> il,const allocator_type& alloc = allocator_type());
map (initializer_list<value_type> il,const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
set (initializer_list<value_type> il,const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
//上述为常见容器中含inisializer_list<T>的构造函数
以下为GCC中initializer_list源码:
// std::initializer_list support -*- C++ -*-
// Copyright (C) 2008-2020 Free Software Foundation, Inc.
//
// This file is part of GCC.
//
// GCC is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3, or (at your option)
// any later version.
//
// GCC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file initializer_list
* This is a Standard C++ Library header.
*/
#ifndef _INITIALIZER_LIST
#define _INITIALIZER_LIST
#pragma GCC system_header
#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else // C++0x
#pragma GCC visibility push(default)
#include <bits/c++config.h>
namespace std
{
/// initializer_list
template<class _E>
class initializer_list
{
public:
typedef _E value_type;
typedef const _E& reference;
typedef const _E& const_reference;
typedef size_t size_type;
typedef const _E* iterator;
typedef const _E* const_iterator;
private:
iterator _M_array;
size_type _M_len;
// The compiler can call a private constructor.
constexpr initializer_list(const_iterator __a, size_type __l)
: _M_array(__a), _M_len(__l) { }
public:
constexpr initializer_list() noexcept
: _M_array(0), _M_len(0) { }
// Number of elements.
constexpr size_type
size() const noexcept { return _M_len; }
// First element.
constexpr const_iterator
begin() const noexcept { return _M_array; }
// One past the last element.
constexpr const_iterator
end() const noexcept { return begin() + size(); }
};
/**
* @brief Return an iterator pointing to the first element of
* the initializer_list.
* @param __ils Initializer list.
* @relates initializer_list
*/
template<class _Tp>
constexpr const _Tp*
begin(initializer_list<_Tp> __ils) noexcept
{ return __ils.begin(); }
/**
* @brief Return an iterator pointing to one past the last element
* of the initializer_list.
* @param __ils Initializer list.
* @relates initializer_list
*/
template<class _Tp>
constexpr const _Tp*
end(initializer_list<_Tp> __ils) noexcept
{ return __ils.end(); }
}
#pragma GCC visibility pop
#endif // C++11
#endif // _INITIALIZER_LIST
使用{}给变量初始化:
int a;//只是被定义没有被初始化
int b{};//{}为空,默认被初始化为0
char* p;//只是被定义没有初始化
char* q{};//{}为空,默认初始化为NULL
int x {5.0};//ERROR,VS2017报告错误,double转int需要进行收缩转换
double y {5};//可行
char a(65);//会对应ASCII码自动进行转换为A
char a {65};//ERROR,不能进行转换
//注意,使用{}时不能进行收缩转换
使用Initializer_list进行构造:
vector(initializer_list<T> initlist){
size_t size = initlist.size();
reserve(size);
_end = _begin;
for(size_t i=0; i<size; ++i){
cout << "initlist = " << *(initlist.begin() + i) << endl;
*_end = *(initlist.begin() + i);
_end++;
}
}