zoukankan      html  css  js  c++  java
  • C++ Primer 读书笔记 第三章

    1. std::string

        size()函数返回值为string::size_type,用下标时,也用string::size_type作为index的类型

    #include <iostream>
    #include <cstdio>
    #include <string>
    using namespace std;
    
    int main()
    {
        string s = "abc";
        cout << s << endl;
        cin >> s;
        cout << s << endl;
        string line;
        while (getline(cin, line)) {
            if (line.empty())
                break;
            cout << line << endl;
        }
        string::size_type len = s.size();
        cout << len << endl;
        s[0] = toupper(s[0]);
        cout << s << endl;
        string s1;
        cout << s1[0] << endl;
        /* string str = "hello" + "world"; */ //error: two string literals addition is invalid
        return 0;
    }

    2. std::vector

        用push_back()函数插入元素,用下标不能用来插入元素,但是map可以,如map[key] = value就相当于把元素插入了。

    // an iterator that cannot write elements
    vector<int>::const_iterator
    
    // an iterator whose value cannot change
    const vector<int>::iterator //不能做it++

        再就是iterators在vector插入了新的元素后就会失效,但是std::list不会。

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
        vector<int> vec(10, 9);
        vector<int>::iterator it = vec.begin();
        it++;
        cout << *it << endl;
        vec.push_back(11);
        cout << *it << endl;
        return 0;
    }
    #include <list>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        list<int> mylist;
        mylist.push_back(1);
        mylist.push_back(2);
        mylist.push_back(3);
        mylist.push_back(4);
        list<int>::iterator it = mylist.begin();
        it++;
        cout << *it << endl;
        mylist.push_back(5);
        cout << *it << endl;
        return 0;
    }

    3. std::bitset

        一种是用unsigned value进行初始化,一种是用字符串初始化,比如“0011”,那么bit[0] = str[3]

  • 相关阅读:
    【BZOJ1014】【JSOI2008】火星人prefix
    [agc011e]increasing numbers
    NOIp2018模拟赛四十一
    拉格朗日插值&&快速插值
    NOIp2018模拟赛四十
    (2016北京集训十四)【xsy1557】task
    (2016北京集训十四)【xsy1556】股神小D
    数据泵导入ORA-39082报错解决
    OracleDBA职责—备份与恢复技术—概念
    OracleDBA职责—备份与恢复技术—RMAN3
  • 原文地址:https://www.cnblogs.com/null00/p/3086727.html
Copyright © 2011-2022 走看看