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]

 

 

posted on 2013-05-19 13:01  NULL00  阅读(395)  评论(0编辑  收藏  举报

导航