STL array的begin方法(4)

本文深入解析了C++中array容器的begin函数,包括其作用、参数、返回值、注意事项及使用示例。了解如何通过begin函数获取array容器的第一个元素,并在不同情况下正确使用迭代器。

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

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

std::array::begin

      iterator begin() noexcept;
const_iterator begin() const noexcept;
Return iterator to beginning
Returns an iterator pointing to the first element in the array container.

返回一个指向array容器第一个元素的iterator.


Notice that, unlike member array::front, which returns a reference to the first element, this function returns a random access iterator pointing to it.

注意和array::front不同,front是返回一个指向第一个元素的引用,这个函数是返回一个指向第一个元素的随机访问迭代器。


In zero-sized arrays, this function returns the same as array::end, but the returned iterator should not be dereferenced.

如果array大小为0,该函数的返回值和end是一样的,但是他们都不应该被解除引用。

例子:

#include <iostream>
#include <array>
using namespace std;
void end1(){
    array<double,0> ad;
    auto ib=ad.begin();
    cout<<"ad.begin()="<<*ib<<endl;
}
运行结果:



Parameters

none

Return Value

An iterator to the beginning of the sequence.

返回一个指向第一个元素的迭代器。


If the array object is const-qualified, the function returns a const_iterator. Otherwise, it returns an iterator.

如果array对象是const属性,那么返回一个const_itertor,否则,返回一个普通的iterator.


Member types iterator and const_iterator are random access iterator types (pointing to an element and to a const element, respectively).

iterator和const_iterator都是随机访问迭代器。

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// array::begin example
#include <iostream>
#include <array>

int main ()
{
  std::array<int,5> myarray = { 2, 16, 77, 34, 50 };

  std::cout << "myarray contains:";
  for ( auto it = myarray.begin(); it != myarray.end(); ++it )
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}


Output:
myarray contains: 2 16 77 34 50

Complexity

Constant.

Iterator validity

No changes.

Data races

No contained elements are accessed by the call, but the iterator returned can be used to access or modify elements. Concurrently accessing or modifying different elements is safe.

容器的元素不会被访问,但是返回的迭代器可以被用来访问以及修改元素,并且这些操作都是安全的。


Exception safety

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

该成员方法不会抛出异常。

The copy construction or assignment of the returned iterator is also guaranteed to never throw.

通过复制构造器以及赋值构造获得的该iterator也保证不会抛出异常。


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

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

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

2014-8-26

于GDUT


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





### C++ STL `array` 容器使用方法 #### 创建和初始化 `array` `std::array` 是一个内存连续且固定长度的数组数据结构,其本质是对原生数组的直接封装[^1]。创建并初始化一个 `std::array` 可以像下面这样: ```cpp #include <array> #include <iostream> int main() { std::array<int, 5> numbers = {10, 20, 30, 40, 50}; // 输出数组元素 for (const auto& num : numbers) { std::cout << num << " "; } } ``` 这段代码展示了如何声明一个含有五个整数的 `std::array` 并对其进行遍历输出。 #### 获取第一个元素地址 为了能够操作底层的数据,`array` 容器提供了 `data()` 成员函数来获取指向容器首元素的指针[^3]。例如: ```cpp #include <array> #include <iostream> int main() { std::array<int, 5> words{1, 2, 3, 4, 5}; // 打印第二个元素(索引为1) std::cout << *(words.data() + 1); } ``` 这里展示的是通过指针算术运算访问特定位置上的元素的方法。 #### 迭代器支持 如同其他标准模板库(STL)容器一样,`std::array` 支持迭代器用于遍历其中的内容。可以利用范围for循环或者传统的基于迭代器的方式来进行遍历[^2]: ```cpp #include <array> #include <iostream> int main() { std::array<std::string, 3> strArray{"hello", "world", "!"}; // 使用范围for循环打印字符串数组 for (const auto& s : strArray) { std::cout << s; } // 或者使用迭代器 for (auto it = strArray.begin(); it != strArray.end(); ++it) { std::cout << *it; } } ``` 此部分说明了两种不同的遍历方式——一种是现代风格的范围for循环;另一种则是经典的迭代器模式。 #### 性能优势与安全性改进 相较于传统C风格数组而言,尽管 `std::array` 不具备动态调整尺寸的能力,但在某些方面具有更好的性能表现以及更高的安全性保障[^4]。这是因为编译期就能确定大小,并且提供了一系列有用的接口使得编程更加方便可靠。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值