设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 数据 创业者 手机
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

C++ STL begin 和end 函数用法

发布时间:2022-07-16 11:32 所属栏目:51 来源:互联网
导读:在前面章节中,我们已经对 C++ STL标准库提供的所有容器做了系统的讲解。读者可能已经注意到,无论是序列式容器还是关联式容器(包括哈希容器),不仅模板类内部提供有 begin() 和 end() 成员方法,C++ STL 标准库中还提供有同名且具有相同功能的 begin() 和
  在前面章节中,我们已经对 C++ STL标准库提供的所有容器做了系统的讲解。读者可能已经注意到,无论是序列式容器还是关联式容器(包括哈希容器),不仅模板类内部提供有 begin() 和 end() 成员方法,C++ STL 标准库中还提供有同名且具有相同功能的 begin() 和 end() 函数。
 
  首先需要说明的是,begin() 和 end() 是以函数模板的形式定义的,但它们的模板并没有位于某一个头文件中,而是很多头文件中都有它们的定义。
  C++ STL 标准库中,包含 begin() 和 end() 函数模板的头文件包括:<iterator>, <array>, <deque>, <forward_list>, <list>, <map>, <regex>(正则表达式的头文件), <set>, <string>, <unordered_map>, <unordered_set> 以及 <vector>。
 
  不仅如此,begin() 和 end() 都位于 std 命名空间中。因此,在使用这 2 个函数之前,程序中应引入容纳它们函数模板的头文件以及 std 命名空间。
 
  在实际的使用场景中,begin() 和 end() 函数往往会一起使用的。根据作用对象的不同,begin() 和 end() 函数可细分为以下 2 个功能。
  1) begin()和end()参数为容器
  当将某个具体容器(比如 cont)作为参数分别传给 begin() 和 end() 函数时,其中 begin() 底层会执行 cont.begin() 语句,而 end() 底层会执行 cont.end() 语句,它们最终会将得到的迭代器作为函数的返回值反馈回来。
 
  当作用对象为容器时,end() 和 begin() 函数的语法格式是完全一样的,这里以 begin() 函数为例,有以下 2 种格式:
  //① 非 const 修改的容器作为参数,begin() 函数返回的为非 const 类型的迭代器
  template <class Container>
      auto begin (Container& cont)
  //② 传入 const 修饰的容器,begin() 函数返回的为 const 类型的迭代器
  template <class Container>
      auto begin (const Container& cont)
 
  其中,cont 表示指定的容器;同时,函数会返回一个有特定指向的迭代器,且此迭代器的类型也取决于 cont 容器。
  以上 2 种格式的区别仅在与传入的容器是否有 const 修饰,即如果有,则通过该函数获得的迭代器也有 const 修饰(不能用于修改容器中存储的数据);反之就没有。
 
 
  举个例子:
  #include <iostream>     // std::cout
  #include <vector>       // std::vector, std::begin, std::end
  using namespace std;
  int main() {
      //创建并初始化 vector 容器
      std::vector<int> myvector{ 1,2,3,4,5 };
      //调用 begin() 和 end() 函数遍历 myvector 容器
      for (auto it = begin(myvector); it != end(myvector); ++it)
          cout << *it << ' ';
      return 0;
  }
  程序执行结果为:
  1 2 3 4 5
 
  程序第 8 行中,begin(myvector) 等同于执行 myvector.begin(),而 end(myvector) 也等同于执行 myvector.end()。
  2) begin()和end()参数为数组
  除了可以将指定容器作为参数传给 begin() 和 end() 之外,还可以指定数组作为参数传给它们。
 
  将指定数组传给 begin() 函数,其会返回一个指向该数组首个元素的指针;将指定数组传给 end() 函数,其会返回一个指向数组中最后一个元素之后位置的指针。
 
  同样,数组作为参数时,end() 函数的语法格式和 begin() 函数也完全一样,这里仅给出了 begin() 函数的语法格式:
  template <class T, size_t N>
    T* begin (T(&arr)[N]);
 
  其中 T 为数组中存储元素的类型,N 为数组的长度;(&arr)[N] 表示以引用的方式传递数组作为参数。
 
  举个例子:
  #include <iostream>     // std::cout
  #include <vector>       // std::vector, std::begin, std::end
  using namespace std;
  int main() {
      //定义一个普通数组
      int arr[] = { 1,2,3,4,5 };
      //创建一个空 vector 容器
      vector<int> myvector;
      //将数组中的元素添加到 myvector 容器中存储
      for (int *it = begin(arr); it != end(arr); ++it)
          myvector.push_back(*it);
      //输出 myvector 容器中存储的元素
      for (auto it = myvector.begin(); it != myvector.end(); ++it)
          cout << *it << ' ';
      return 0;
  }
  程序执行结果为:
  1 2 3 4 5
 
  注意程序中第 10 行,这里用整数指针 it 接收 begin(arr) 的返回值,同时该循环会一直循环到 it 指向 arr 数组中最后一个元素之后的位置。

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读