gpt4 book ai didi

c++ - 当第一个 itr 在最后一个之后出现时,std::vector 范围构造函数的官方行为是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:16:17 26 4
gpt4 key购买 nike

假设您有一个有效的起点:

std::vector<UINT32> host = {1,2,3,4,5};

当您尝试使用迭代器构造另一个 vector 时:

std::vector<UINT32> client(host.begin(),host.end());

// client.size() is 5. Elements begin -> end look just like host.

但是如果迭代器是反向的呢?如果开始在结束之后怎么办?

std::vector<UINT32> backwardsClient(host.end(), host.begin());

// What happens?

最佳答案

这将是未定义的行为。

看标准:

N3690 23.3.7.2[vector.cons]

template <class InputIterator>
vector(InputIterator first, InputIterator last,
const Allocator& = Allocator());

9. Effects: Constructs a vector equal to the range [first,last), using the specified allocator.

它说范围必须是[first, last),但标准没有提到如果不是这种情况会发生什么。因此,这是未定义的行为。

关于c++ - 当第一个 itr 在最后一个之后出现时,std::vector 范围构造函数的官方行为是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21786970/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com