1. 程式人生 > >C++ primer 讀書筆記二

C++ primer 讀書筆記二

第三章字串 向量 陣列

string 初始化

  • 初始化型別:拷貝初始化、直接初始化

    使用=初始化即為,拷貝初始化。

  • char 陣列與string

    char 陣列如果沒有\0截止符 將引發讀寫越界的問題
    string 可以會在初始化的時候自動加入截止符

#include "a.hpp"
#include<iostream>
using std::string;
using std::cout;
using  std::endl;

int main()
{
    const char ca[] = {'h','e','l','l','o'};
    string
str("liu chuan wu"); const char *p = ca; while(*p) { cout<<*p; ++p; } cout<<endl; }

輸出結果:helloliu chuan wu
沒有截止符引發的越界讀寫。