C++ vector 和 array

#include <array>
#include <deque>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main(int argc, const char *argv[]) {
    list<string> authors = {"a", "b", "c", "d"};
    deque<string> authorsList(authors.begin(), authors.end());
    for (auto obj : authorsList) {
        cout << "authorsList: " << obj << endl;
    }
    array<int, 18> a2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
    auto beg = a2.begin(), end = a2.end(), begin = a2.begin();
    int sought = 11;
    auto mid   = a2.begin() + (end - beg) / 2; //二分查找 mid=beg+(end-beg)/2 與 mid=(beg+end)/2 因爲假設beg和end都很大,超過了int的一半,那麼beg + end可能會溢出
    while (mid != end && *mid != sought) {
        if (sought < *mid)
            end = mid;
        else
            beg = mid;
        mid = beg + (end - beg) / 2;
        cout << " mid = " << *mid << endl;
    }
    while (begin != a2.end() - 1 && *begin >= 0) {
        cout << " beg1 : " << *begin++ << ", ";
    }
    cout << endl;
    vector<string> v = {"suqsi", "simba", "frollo", "scar"};
    for (auto obj : v) {
        cout << "  >>" << obj << endl;
    }
    cout << v.capacity() << "  capacity  " << v.size() << "reserve " << endl;
    v.insert(v.begin(), {"this", "is"});
    auto begin2 = v.begin();
    while (begin2 != v.end() - 1) {
        cout << " beg2 : " << *begin2++ << ", ";
    }
    cout << endl;
    const char *ss = "0123456789";
    // s1=4,ss爲字符指針在內存中佔用4個字節
    int s1 = sizeof(ss);
    // s2=10,計算字符串ss的長度
    long s2    = strlen(ss);
    char str[] = "abced";
    // a爲6(1*6),字符數組str包含6個元素(a,b,c,d,e,\0),每個元素佔用1個字節
    long a = sizeof(str);
    // len爲5,不包含"\0",
    long len = strlen(str);
    // str[0]是字符元素a,所以b=1
    int b = sizeof(str[0]);
    cout << "s1 sizeof " << s1 << " strlen " << s2 << endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章