类对象初始化和Initializer_list的

在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++;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
initializer_list是C++11标准引入的一种数据结构,用于表示一个初始化列表。它可以用于函数参数、构造函数、赋值等场合。 初始化列表是由一系列用逗号隔开的值组成的,可以用花括号括起来。例如,{1, 2, 3}就是一个初始化列表,其中包含了三个整数1、2、3。 initializer_list的定义如下: ```cpp template<class T> class initializer_list { public: typedef const T* iterator; typedef const T& reference; typedef size_t size_type; initializer_list() noexcept; initializer_list(const T* p, size_t n) noexcept; size_t size() const noexcept; const T* begin() const noexcept; const T* end() const noexcept; }; ``` 使用initializer_list需要包含头文件<initializer_list>。 可以通过以下方式初始化initializer_list: ```cpp std::initializer_list<int> il = {1, 2, 3}; ``` 在函数参数中使用initializer_list可以方便地传递一组值: ```cpp void foo(std::initializer_list<int> il) { for (auto it = il.begin(); it != il.end(); ++it) { std::cout << *it << std::endl; } } foo({1, 2, 3}); ``` 在类的构造函数中使用initializer_list可以方便地对成员变量初始化: ```cpp class MyClass { public: MyClass(std::initializer_list<int> il) { for (auto it = il.begin(); it != il.end(); ++it) { vec.push_back(*it); } } private: std::vector<int> vec; }; MyClass mc = {1, 2, 3}; ``` 总之,initializer_list是一个十分方便的工具,可以用于各种场合,使代码更加简洁、清晰。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chls

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值