std::vector 的那些构造函数示例

本文详细介绍了C++标准库中的std::vector类的构造函数,包括不同版本的参数、noexcept特性以及示例代码,展示了如何使用这些构造函数创建和初始化vector容器。
摘要由CSDN通过智能技术生成

0,构造函数列表

std::vector<T,Allocator>::vector - cppreference.com

vector();
	(until C++17)
vector() noexcept(noexcept(Allocator()));
	(since C++17)
(until C++20)
constexpr vector() noexcept(noexcept(Allocator()));
	(since C++20)
	(2) 	
explicit vector( const Allocator& alloc );
	(until C++17)
explicit vector( const Allocator& alloc ) noexcept;
	(since C++17)
(until C++20)
constexpr explicit vector( const Allocator& alloc ) noexcept;
	(since C++20)
	(3) 	
explicit vector( size_type count,

                 const T& value = T(),
                 const Allocator& alloc = Allocator() );
	(until C++11)
vector( size_type count,

                 const T& value,
                 const Allocator& alloc = Allocator() );
	(since C++11)
(until C++20)
constexpr vector( size_type count,

                  const T& value,
                  const Allocator& alloc = Allocator() );
	(since C++20)
	(4) 	
explicit vector( size_type count );
	(since C++11)
(until C++14)
explicit vector( size_type count,
                 const Allocator& alloc = Allocator() );
	(since C++14)
(until C++20)
constexpr explicit vector( size_type count,
                           const Allocator& alloc = Allocator() );
	(since C++20)
	(5) 	
template< class InputIt >

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

constexpr vector( InputIt first, InputIt last,
                  const Allocator& alloc = Allocator() );
	(since C++20)
	(6) 	
vector( const vector& other );
	(until C++20)
constexpr vector( const vector& other );
	(since C++20)
	(7) 	
vector( const vector& other, const Allocator& alloc );
	(since C++11)
(until C++20)
constexpr vector( const vector& other, const Allocator& alloc );
	(since C++20)
	(8) 	
vector( vector&& other );
	(since C++11)
(until C++17)
vector( vector&& other ) noexcept;
	(since C++17)
(until C++20)
constexpr vector( vector&& other ) noexcept;
	(since C++20)
	(9) 	
vector( vector&& other, const Allocator& alloc );
	(since C++11)
(until C++20)
constexpr vector( vector&& other, const Allocator& alloc );
	(since C++20)
	(10) 	
vector( std::initializer_list<T> init,
        const Allocator& alloc = Allocator() );
	(since C++11)
(until C++20)
constexpr vector( std::initializer_list<T> init,
                  const Allocator& alloc = Allocator() );
	(since C++20)
template< container-compatible-range<T> R >

constexpr vector( std::from_range_t, R&& rg,
                  const Allocator& alloc = Allocator() );
	(11) 	(since C++23)

1,ex01

template< class InputIt >

constexpr vector( InputIt first, InputIt last,
                  const Allocator& alloc = Allocator() );

#include <vector>
#include <iostream>


using namespace std;

int main()
{
	char A[12] ={'a','b','c','d','e','f','g','h','i','j','k','l'};
	cout<<A<<endl;
	vector<char> B(A+2,A+4);
	cout<< B.size()<<endl;
	for(int i=0; i<B.size(); i++)
		cout<<B[i]<<endl;	

	return 0;
}

Makefile:

hello:

.PHONY: clean
clean:
	${RM} hello

ex02

ex03

ex04

ex05

未完待续

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`std::priority_queue` 类有几个不同的构造函数,可以根据需要选择不同的方式来创建优先队列。下面是一些常用的构造函数: 1. 默认构造函数: ```cpp std::priority_queue<T> pq; ``` 这将创建一个空的优先队列,其中元素类型为 `T`,使用默认的比较函数 `std::less<T>` 进行元素比较。 2. 指定比较函数的构造函数: ```cpp std::priority_queue<T, Container, Compare> pq; ``` 这将创建一个空的优先队列,其中元素类型为 `T`,使用指定的比较函数 `Compare` 进行元素比较。`Container` 是一个可选的容器类型,默认为 `std::vector<T>`。 3. 使用范围构造函数: ```cpp template <class InputIterator> std::priority_queue(InputIterator first, InputIterator last); ``` 这将创建一个优先队列,并使用迭代器范围 `[first, last)` 中的元素进行初始化。这意味着可以通过提供一个容器的起始和结束迭代器来构造优先队列。 以下是一些使用示例: ```cpp std::priority_queue<int> pq1; // 默认构造函数,元素类型为 int std::priority_queue<int, std::vector<int>, std::greater<int>> pq2; // 元素类型为 int,按照从小到大排序 std::priority_queue<double, std::deque<double>> pq3; // 元素类型为 double,使用 std::deque 作为容器 std::vector<int> nums = {3, 1, 4, 1, 5}; std::priority_queue<int> pq4(nums.begin(), nums.end()); // 使用范围构造函数初始化优先队列 ``` 希望这些构造函数的说明对你有所帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值