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

    《c++ primer, 5E》

    第91页到第94页,笔记:

    1、vector支持的操作。

    v.empty()、v.size()、v.push_back(t)、v[n]

    2、试图通过下标访问不存在vector元素不会被编译器发现,

    而是在运行时产生不可预知的值。

    3、确保下标合法的有效手段:尽可能使用范围for

    4、vector的小结:vector的初始化、vector支持的操作、vector的遍历与随机访问

    遇到的问题:

     

    课后练习:

    练习3.16

    正确。

    并且验证 (10, "hi")和{10 , "hi"}等效,

    而{10} 和(10)的效果取决vector元素的类型。

     

    练习3.17

    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    #include<vector>
    using std::vector;
    #include<string>
    using std::string;
    int main()
    {
        string word;
        vector<string> svec;
        while(cin >> word){
            svec.push_back(word);
        }
        for(auto &word: svec){
            for(auto &ch: word){
                ch = toupper(ch);
            }
            cout << word << endl;
        }
        return 0;
    }

    练习3.18

    不合法。非法访问。 改为 ivec.push_back(42);

     

    练习3.19

    反正最好的是 vector<int> v(10 , 42);

    练习3.20

    1

    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    #include<vector>
    using std::vector;
    #include<string>
    using std::string;
    int main()
    {
        int temp;
        vector<int> v;
        while(cin >> temp){
            v.push_back(temp);
        }
        //随机访问
        for(decltype(v.size()) index = 0;
            index != v.size()-1; ++index)
            cout << v[index]+ v[index+1] << endl;
        return 0;
    }

    2

    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    #include<vector>
    using std::vector;
    #include<string>
    using std::string;
    int main()
    {
        int temp;
        vector<int> v;
        while(cin >> temp){
            v.push_back(temp);
        }
        //随机访问
        decltype(v.size()) head = 0;
        auto tail = v.size()-1;    
        for(;
        head <= tail; ++head, --tail){
            cout << v[head]+v[tail] << endl;
        }
        return 0;
    }
  • 相关阅读:
    各版本mysql乱码的问题解决
    Centos+apache2.4.2+mysql5.5+php5.3.10+memcache+sphinx配置全程
    Mysql 优化
    Mysql 表优化
    Mysql 索引优化
    python整合连续数字的练习,包含itertoolsgroupby用法
    MySQL INTO OUTFILE/INFILE导出导入数据
    python lambda使用if
    mysql小知识
    Python利用urllib2抓取网页返回乱码的问题
  • 原文地址:https://www.cnblogs.com/xkxf/p/6392499.html
Copyright © 2011-2022 走看看