zoukankan      html  css  js  c++  java
  • c++第十五天

    《c++ primer, 5E》

    第94页到第99页,笔记:

    1、迭代器(iterator):一种比下标访问更通用的访问容器中元素的机制

    (并不是所有标准库容器都支持下标访问,<运算符)

    我们认定某个类型是迭代器,当且仅当它支持一套操作,

    这套操作使得我们能访问容器的元素或者从某个元素移动到另外一个元素。


    2、有迭代器的类型都拥有名为begin和end成员

    3、一般来说,我们不在意迭代器的准确类型到底是什么

    4、迭代器支持的一些运算。(与指针类似,例如iter->mem:解引用iter并获取该元素

    名为mem的成员,等价于(*iter).mem)

    5、养成使用迭代器和!=的习惯(而非<运算符),就不用太在意用的到底是哪种容器类型。

    6、如果对象只需读操作而无须写操作的话最好使用常量类型(比如 const_iterator)。可以通过cbegin和cend返回该类型迭代器。

    7、vector对象的一个限制:任何一种可能改变vector对象容量的操作,比如push_back,都会使该vector对象的迭代器失效。

    8、小结:重点掌握利用迭代器遍历元素的方法,结合解引用和成员访问操作(*it以及it->)

    课后练习:

    练习3.21

    int:

    #include<iostream>
    using std::cout;
    using std::endl;
    #include<vector>
    using std::vector;
    int main()
    {
        // 定义一个vector对象
        vector<int> v5(10 ,42);
        // 输出vector对象的容量和具体内容
        int size = v5.end() - v5.begin();
        cout << size << endl;
        // 使用迭代器和!=遍历元素
        for(auto it = v5.begin(); it != v5.end(); ++it){
            cout << *it << endl;
        }
        return 0;
    }

     string:

    #include<iostream>
    using std::cout;
    using std::endl;
    #include<vector>
    using std::vector;
    #include<string>
    using std::string;
    int main()
    {
        // 定义一个vector对象
        vector<string> v5(10 , "hi");
        // 输出vector对象的容量和具体内容
        int size = v5.end() - v5.begin();
        cout << size << endl;
        // 使用迭代器和!=遍历元素
        for(auto it = v5.begin(); it != v5.end(); ++it){
            cout << *it << endl;
        }
        return 0;
    }

    练习3.22

    #include<iostream>
    using std::cout;
    using std::endl;
    #include<vector>
    using std::vector;
    #include<string>
    using std::string;
    int main()
    {
        // 定义一个字符串向量
        vector<string> text{"Hello", "", "world!"};
        // 把text的第一段全都改成大写形式,然后再输出它
        for(auto it = text.begin();
        it != text.end() && !(*it).empty(); ++it){
            for(auto &ch: *it){
                ch = toupper(ch);
            }
            cout << *it << endl;
        }
        return 0;
    }

    练习3.23

    #include<iostream>
    using std::cout;
    using std::endl;
    #include<vector>
    using std::vector;
    #include<string>
    using std::string;
    int main()
    {
        // 创建一个含有10个整数的vector对象
        vector<int> ivec(10 , 42);
        // 使用迭代器将所有元素的值都变成原来的两倍。输出vector对象的内容
        for(auto it = ivec.begin(); it != ivec.end(); ++it){
            *it *= 2;
            cout << *it << endl;
        }
        return 0;
    }

     

    遇到的问题:

    1、const_iterator哪里和常量指针差不多了?

  • 相关阅读:
    BIO、NIO、AIO有什么区别?
    java中IO流有哪些?
    List、Map、Set 三个接口,存取元素时,各有什么特点?
    Vector、ArrayList、LinkedList 的存储性能和特性?
    Java.util.Map的常用实现类有哪些?
    Java自学指南六、查一手资料
    虚拟机中Lvs配置
    m2014-software->Word2010发布博客文章至Cnblogs
    m2014-c->c模拟java的hashmap容器类
    m2014_c->c语言容器类工具列
  • 原文地址:https://www.cnblogs.com/xkxf/p/6396186.html
Copyright © 2011-2022 走看看