首页 > 解决方案 > 这里使用了哪个 std::vector 构造函数?

问题描述

我有一些代码(它来自https://vulkan-tutorial.com/Drawing_a_triangle/Setup/Validation_layers):

uint32_t glfwExtensionCount = 0;
const char** glfwExtensions;
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);

std::vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount); // which constructor is used here?

一切都可以编译并且可以工作,但是我不知道向量构造函数中发生了什么。

标签: c++stdvector

解决方案


那是

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

https://en.cppreference.com/w/cpp/container/vector/vector中的#4 )。它从一系列输入迭代器构造一个向量。

C++ 迭代器被设计为与指针兼容,即指针是有效的迭代器。


推荐阅读