C++11 中的initialize_list

本文详细介绍了C++中的initializer_list模板类,包括其定义、使用方法及与其他容器类的交互方式。通过具体示例展示了如何利用initializer_list进行初始化,并解释了在不同场景下initializer_list的行为差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这就是一个简单的模板类,不过在C++中有了特殊的语法支持,定义的时候使用如下的格式:

initialize_list<double> dl = {1.1, 1.2};

或者:

initialize_list<double> dl {1.1, 1.2};

还有就是有一些容器类也会有构造函数是以initial_list类为参数的。

template<class _Elem>
class initializer_list
{ // list of pointers to elements
public:
typedef _Elem value_type;
typedef const _Elem& reference;
typedef const _Elem& const_reference;
typedef size_t size_type;

typedef const _Elem* iterator;
typedef const _Elem* const_iterator;

initializer_list() _NOEXCEPT
: _First(0), _Last(0)
{ // empty list
}

initializer_list(const _Elem *_First_arg,
const _Elem *_Last_arg) _NOEXCEPT
: _First(_First_arg), _Last(_Last_arg)
{ // construct with pointers
}

const _Elem *begin() const _NOEXCEPT
{ // get beginning of list
return (_First);
}

const _Elem *end() const _NOEXCEPT
{ // get end of list
return (_Last);
}

size_t size() const _NOEXCEPT
{ // get length of list
return ((size_t)(_Last - _First));
}

private:
const _Elem *_First;
const _Elem *_Last;
};

从以上定义来看,initialize_list作为一个模板类,不仅仅是用作初始化容器类,而且本身也可以单独用作容器使用,只是比较寒碜,只提供了begin(), end(), size()三个方法。

 

所以,在书籍中,提到initalize_list,你就可以认为,它说的就是{1.1, 1.2, 1.3}一类的东西。

还有就是当以initialize_list为参数的时候,要判断实际的函数调用过程:

std::vector<int> vi(10);//case A : 10 unintialized elements

std::vector<int> vi({10});// case B: 1 element set to 10

std::vector<double> payments{45.99, 39.23, 10.11};

转载于:https://www.cnblogs.com/hustxujinkang/p/4130104.html

### C++ STL List Usage and Examples In the context of C++, `std::list` is a container that supports constant time insertions and deletions from anywhere within the sequence. This makes it particularly useful when frequent insertion or deletion operations are required. #### Declaration and Initialization A list can be declared using different methods: ```cpp #include <iostream> #include <list> int main() { std::list<int> my_list = {1, 2, 3}; // Initialize with values // Add elements to the end of the list my_list.push_back(4); // Print all elements in the list for (auto& elem : my_list) { std::cout << elem << " "; } } ``` #### Common Operations on Lists Adding an element at any position involves specifying where exactly one wants to add this new item: ```cpp // Inserting before specific iterator location my_list.insert(my_list.begin(), 0); // Inserts '0' as first element ``` Removing items also has multiple options available depending upon what needs removal – either by value or through iterators pointing towards those positions which need erasing: ```cpp // Remove all occurrences of a particular value my_list.remove(2); // Erase single occurrence pointed by iterator auto pos = my_list.begin(); pos++; // Move past beginning my_list.erase(pos); ``` Checking whether two lists contain identical sequences without considering their order might involve sorting both collections beforehand followed by comparison via equality operator (`==`) provided they support such operation directly out-of-the-box like so: ```cpp if (sorted_copy_of_my_list == another_sorted_list) { // Both have same contents regardless of original ordering. } ``` For more advanced manipulations including merging sorted ranges into destination containers while preserving relative orders among equal keys during merge process etc., refer standard library documentation regarding algorithms operating over bidirectional iterators since these apply equally well here too due to nature how linked structures internally manage memory allocation/deallocation patterns under hood[^1].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值