STL array的max_size方法(14)

本文深入解析了C++模板类array的max_size函数,详细介绍了其作用、参数、返回值、复杂度及使用示例,强调了size和max_size的一致性,并提供了输出截图和代码演示。

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

原文地址:http://www.cplusplus.com/reference/array/array/max_size/
public member function
<array>

std::array::max_size

constexpr size_type max_size() noexcept;
Return maximum size
Returns the maximum number of elements that the array container can hold.

返回array容器所能存放的元素的最大数目。


The max_size of an array object, just like its size, is always equal to the second template parameter used to instantiate the array template class.

对于array的max_size,和size一样大一般都是等于实例化时的第二个模版参数。(和vector的很不一样

例子:

#include <iostream>
#include <array>
using namespace std;
int main(){
	array<int,5> ai;
	cout<<"ai.size="<<ai.size()<<endl;
	cout<<"ai.max_size="<<ai.max_size()<<endl;


}
运行截图:



Parameters

none

Return Value

The maximum number of elements the object can hold as content.

This is a constexpr.

返回array所能存放元素的最大数目,

这是一个常量。


Member type size_type is an alias of the unsigned integral type size_t.

Example

1
2
3
4
5
6
7
8
9
10
11
12
// array::max_size
#include <iostream>
#include <array>

int main ()
{
  std::array<int,10> myints;
  std::cout << "size of myints: " << myints.size() << '\n';
  std::cout << "max_size of myints: " << myints.max_size() << '\n';

  return 0;
}


Output:
size of myints: 10
max_size of myints: 10
size and max_size of an array object always match.

array的size和max_size一般都是相等的。


Complexity

Constant.

Iterator validity

No changes.

Data races

The container is accessed.
No contained elements are accessed: concurrently accessing or modifying them is safe.

容器将被访问。

容器内的元素不会被访问,同时访问以及修改他们都是安全的。

Exception safety

No-throw guarantee: this member function never throws exceptions.

该方法不会抛出异常。



——————————————————————————————————————————————————————————————————

//总结的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.youkuaiyun.com/qq844352155
author:天下无双

Email:coderguang@gmail.com

2014-8-30

于GDUT

——————————————————————————————————————————————————————————————————









C++ STL中的array是一个固定大小的数组容器,它提供了一组功能完备的操作函数,用于管理和操作数组。 array与C++内置数组相比,具有以下优势: - 通过array可以方便地获取数组的大小(通过size()函数)和最大容量(通过max_size()函数)。- array提供了一系列的成员函数,如at()、front()、back()等,用于访问和操作数组元素。 - 支持迭代器,可以使用迭代器遍历和修改数组的元素。 - array的大小是固定的,不能动态改变。 以下是一个使用array的简单示例: ```cpp #include <iostream> #include <array> int main() { std::array<int, 5> arr = {1, 2, 3, 4, 5}; std::cout << "Array size: " << arr.size() << std::endl; std::cout << "Array elements: "; for (const auto& element : arr) { std::cout << element << " "; } std::cout << std::endl; arr[2] = 10; std::cout << "Modified array elements: "; for (const auto& element : arr) { std::cout << element << " "; } std::cout << std::endl; return 0; } ``` 上述示例中,我们创建了一个包含5个整数的array容器arr,并初始化为{1, 2, 3, 4, 5}。通过arr.size()可以获取数组的大小,输出结果为5。然后我们使用for循环和迭代器遍历数组的所有元素,并输出到控制台。 接着我们修改数组的第三个元素为10,然后再次遍历数组并输出修改后的结果。 array是一个非常方便和实用的容器,可以用于替代传统的C风格数组,并提供更多的功能和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值