std::array中的std::get<n>()
模板函数std::get<n>()是一个辅助函数,它能够获取到容器的第 n 个元素。模板参数的实参必须是一个在编译时可以确定的常量表达式,编译时会对它检查。
get<n>()模板提供了一种不需要在运行时检查,但能用安全的索引值访问元素的方法。
在std::array中,提供了2种访问元素的方法:[]和at()
#include <iostream>
#include <array> int main()
{
std::array<int, > arr {, , , , };
std::cout << "5th element is : " << arr[] << std::endl;
return ;
}
[]不会检查索引值是否越界,一旦越界将会有未定义的结果(crash或未知的值)
#include <iostream>
#include <array> int main()
{
std::array<int, > arr {, , , , };
std::cout << "5th element is : " << arr.at() << std::endl;
return ;
}
at()会检查,一旦越界,将抛出std::out_of_range的异常
terminate called after throwing an instance of 'std::out_of_range'
what(): array::at: __n (which is ) >= _Nm (which is )
Aborted (core dumped)
注:实际上,在头文件 <array> 中,通过重载全局函数 std::get 使得通过 std::get 访问 array 中的元素时,array 容器就像一个多元组。
|
template<size_t I, class T, size_t N > T& get( array<T,N>& a ); |
(1) | (C++11 起) |
|
template<size_t I, class T, size_t N > T&& get( array<T,N>&& a ); |
(2) | (C++11 起) |
|
template<size_t I, class T, size_t N > const T& get( const array<T,N>& a ); |
(3) | (C++11 起) |
从数组中提取Ithelement元素.
I[0, N)范围内的值必须是一个整数。这是在编译时执行,而不是at()或operator[]().
std::array中的std::get<n>()的更多相关文章
- 将std::array转换成std::tuple
template<typename Array, std::size_t... Index> decltype(auto) array2tuple_impl(const Array& ...
- c++编译错误C2971:"std::array":array_size:包含非静态存储不能用作废类型参数;参见“std::array”的声明
在Qt5中这段代码编写有两种方式:一个编译成功,一个失败 成功版本: static constexpr size_t block_size = 0x2000;//8KB static constexp ...
- C++ Arrays, std::array, std::vector 总结
原文来自: https://shendrick.net/Coding%20Tips/2015/03/15/cpparrayvsvector.html @Seth Hendrick Original a ...
- C++语言中std::array的神奇用法总结,你需要知道!
摘要:在这篇文章里,将从各个角度介绍下std::array的用法,希望能带来一些启发. td::array是在C++11标准中增加的STL容器,它的设计目的是提供与原生数组类似的功能与性能.也正因此, ...
- 一文带你详细介绍c++中的std::move函数
前言 在探讨c++11中的Move函数前,先介绍两个概念(左值和右值) 左值和右值 首先区分左值和右值 左值是表达式结束后依然存在的持久对象(代表一个在内存中占有确定位置的对象) 右值是表达式结束时不 ...
- C++ std::array
std::array template < class T, size_t N > class array; Code Example #include <iostream> ...
- C++中的std详解
以下内容为:本人看C++视频教程-范磊主讲(2.91G)视频学习笔记. 与大家分享下,希望可以帮助大家学习c++! 引例: #include<iostream> int main() { ...
- 源码阅读笔记 - 1 MSVC2015中的std::sort
大约寒假开始的时候我就已经把std::sort的源码阅读完毕并理解其中的做法了,到了寒假结尾,姑且把它写出来 这是我的第一篇源码阅读笔记,以后会发更多的,包括算法和库实现,源码会按照我自己的代码风格格 ...
- VC++ 中使用 std::string 转换字符串编码
目录 第1章说明 1 1.1 代码 1 1.2 使用 4 第1章说明 VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量. 1.1 代码 函数声明如下 ...
随机推荐
- codeforces479E
Riding in a Lift CodeForces - 479E Imagine that you are in a building that has exactly n floors. You ...
- OTZ%%%子谦。大佬
又上了节课...俩题 计算系数 组合数问题... 要不是大佬指点就只能阶乘暴力算了 (主要还是我忘了杨辉三角) 杨辉三角与组合数C有着千丝万缕的联系,在计算,使用方面相当方便. 先说计算系数 计 ...
- <Android基础>(三) UI开发 Part 3 RecyclerView
RecyclerView 1)RecyclerView的基本用法 2)横向滚动和瀑布流滚动 3)注册点击事件 3.6 强大的滚动控件 RecyclerView ListView缺点: 1.不使用技巧优 ...
- 【NOIP2013模拟】终极武器(经典分析+二分区间)
No.2. [NOIP2013模拟]终极武器 题意: 给定你一些区间,然后让你找出\(1\sim 9\)中的等价类数字. 也就是说在任何一个区间里的任何一个数,把其中后\(k\)位中的某一位换成等价类 ...
- Gym - 101350E Competitive Seagulls (博弈)
There are two seagulls playing a very peculiar game. First they line up N unit squares in a line, al ...
- Python 文件读取
1. 最基本的读文件方法: # File: readline-example-1.py file = open("sample.txt") while 1: line = file ...
- crm 添加用户 编辑用户 公户和私户的展示,公户和私户的转化
1.添加用户 和编辑可以写在一起 urls.py url(r'^customer_add/', customer.customer_change, name='customer_add'), url( ...
- Beamer 中的页面链接
\documentclass[]{beamer} \usetheme{Madrid} \usenavigationsymbolstemplate{} \title{Main Title} \autho ...
- Luogu P4321 随机漫游
期望DP要倒着推 Luogu P4321 题意 LOJ #2542 不一定是树,询问点不一定均为1 $Solution$ 设计一个巧妙的DP状态 设$ F(S,x)$表示当前在点$ x$已经走遍了$ ...
- JAVA进阶5
间歇性混吃等死,持续性踌躇满志系列-------------第5天 1.IDEA常用快捷键 2.简单方法的使用 package cn.intcast.day05.demo01; public clas ...