【C++】Essential c++ 第三章学习笔记
发布日期:2021-05-08 14:57:16 浏览次数:20 分类:精选文章

本文共 4131 字,大约阅读时间需要 13 分钟。

Essential C++ 第三章内容概述

本章主要介绍如何使用容器,并探讨如何脱离变量类型和容器类型,通过泛型算法实现功能。


1. 前置知识

1.1 下标操作的实质

数组的内存存储方式是一段连续的空间,按元素大小分割而成。数组下标操作实际上是地址偏移和提领运算的组合。例如,a[2]等价于* (a + 2),但需要考虑指针类型的偏移。

1.2 迭代器(泛型指针)

迭代器是一种抽象的地址偏移操作,用于处理内存表结构。它允许在非连续内存存储的容器(如list)中高效地访问元素,解决了传统下标操作的局限性。

1.3 函数对象

函数对象是一种类,通过重载()操作符实现功能。它的优势在于可以实例化、携带数据和状态,支持在线编译,提升运行效率。

函数对象的定义与使用

template
class myOutput {public: void operator()(inputiterator first, inputiterator last) { for (; first != last; ++first) { cout << (*first) << endl; } }};

使用方法

  • 定义并实例化函数对象:
  • myOutput
    > a;a(ivec.begin(), ivec.end());
    1. 匿名调用:
    2. myOutput
      >()(ivec.begin(), ivec.end());

      1.4 绑定适配器

      绑定适配器用于将二元运算转换为一元操作。例如,bind2nd(greater<int>, 5)用于查找大于5的元素。


      2. 容器

      容器是泛型算法的主要操作对象,主要包括顺序容器和关联容器。

      2.1 容器的共性操作

      定义方法

    3. 初始化空容器:
    4. vector
      a;
      1. 指定大小:
      2. vector
        a(10);
        1. 指定大小和默认值:
        2. vector
          a(10, 0);
          1. 拷贝部分容器:
          2. vector
            a(ivec.begin(), ivec.end());vector
            a(ivec);

            插入操作

            • insert(pos, val):在指定位置插入一个元素。
            • insert(pos, count, val):在指定位置插入多个元素。
            • insert(pos, start, end):插入一段元素序列。

            删除操作

            • erase(pos):删除指定位置的元素。
            • erase(start, end):删除指定范围内的元素。

            特殊操作

            • push_back()pop_back():在末尾/开头插入/删除元素。
            • front()back():获取首尾元素。

            3. 泛型算法

            泛型算法通过抽象操作符号,脱离变量和容器类型,实现统一操作。

            3.1 常用泛型算法

          3. 查找元素

            • find(): 在无序容器中查找元素。
            • binary_search(): 在有序容器中高效查找。
          4. 统计元素

            • count(): 统计元素出现次数。
          5. 查找子序列

            • search(): 检查是否存在子序列。
          6. 3.2 应用举例

            #include 
            #include
            #include
            #include
            #include
            using namespace std;template
            outputiterator filter(inputiterator first, inputiterator last, outputiterator at, valtype val, comp pred) { while ((first = find_if(first, last, bind2nd(pred, val))) != last) { *at++ = *first++; } return at;}int main() { vector
            ivec1 = {1, 8, 3, 3, 4, 4, 5, 10, 4, 7}; // 方法一:逐个查找 vector
            dst_ivec1; auto it = find_if(ivec1.begin(), ivec1.end(), bind2nd(greater
            (), 5)); while (it != ivec1.end()) { dst_ivec1.push_back(*it); ++it; } // 方法二:排序后删除不需要的部分 vector
            local_vec(ivec1); sort(local_vec.begin(), local_vec.end()); auto iter = find_if(local_vec.begin(), local_vec.end(), bind2nd(greater
            (), 5)); local_vec.erase(local_vec.begin(), iter); return 0;}

            4. 容器设计不需要考虑容量大小的设计技巧

            4.1 iterator inserter

            iterator inserter用于替代赋值符号,支持动态容器大小管理。常见类型包括:

            • back_inserter(vec): 用push_back代替赋值符号。
            • inserter(vec, vec.end()): 在指定位置插入元素。
            • front_inserter(lst): 只适用于listdeque,用push_front代替赋值符号。

            4.2 使用示例

            #include 
            #include
            #include
            #include
            #include
            using namespace std;int main() { vector
            words; istream_iterator
            it(cin); istream_iterator
            eof; // 将输入内容存储到向量 copy(it, eof, back_inserter(words)); // 排序 sort(words.begin(), words.end()); // 输出结果 ostream_iterator
            os(cout, " "); copy(words.begin(), words.end(), os); return 0;}

            5. 来自标准输入输出和文件输入输出的迭代器操作

            通过迭代器操作,可以高效地读取和写入数据。

            5.1 标准输入输出

            #include 
            #include
            #include
            #include
            #include
            using namespace std;int main() { vector
            words; istream_iterator
            it(cin); istream_iterator
            eof; // 将输入内容存储到向量 copy(it, eof, back_inserter(words)); // 排序 sort(words.begin(), words.end()); // 输出结果 ostream_iterator
            os(cout, " "); copy(words.begin(), words.end(), os); return 0;}

            5.2 文件输入输出

            #include 
            #include
            #include
            #include
            #include
            #include
            using namespace std;int main() { ifstream in_file("1.txt"); ofstream out_file("2.txt"); vector
            words; istream_iterator
            it(in_file); istream_iterator
            eof; // 将输入内容存储到向量 copy(it, eof, back_inserter(words)); // 排序 sort(words.begin(), words.end()); // 输出结果 ostream_iterator
            os(out_file, " "); copy(words.begin(), words.end(), os); return 0;}

            本章内容涵盖了容器的使用、泛型算法的应用以及如何通过迭代器操作高效处理数据。

    上一篇:【C++】Essential c++ 第四章学习笔记
    下一篇:【C++】Essential C++ 第二章学习笔记

    发表评论

    最新留言

    表示我来过!
    [***.240.166.169]2025年04月12日 18时11分08秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章