C++ Primer筆記(一):字符串、向量和數組

3.1 命名空間

  • using namespace::name;
  • using namespace::std
  • using std::cin
    ……
    頭文件不該該包含using

3.2 類型string

getline(cin, str);
str.empty();
str.size();

size()返回類型爲UINT,不能與int比較,當int爲負時出錯
string對象只能和string對象或字面值相加ios

循環處理spa

for(auto c : str)
{}

for(auto &c : str)
{}

習題3.2.3code

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    string str;

    cout << "3.6" << endl;
    str = "aruew32413aefjewoiroqw1231./awrfwer";
    cout << str << endl;
    for (auto &i : str)
    {
        i = 'X';
    }
    cout << str << endl;

    cout << "3.7" << endl;
    str = "aruew32413aefjewoiroqw1231./awrfwer";
    cout << str << endl;
    for (auto i : str)
    {
        i = 'X';
    }
    cout << str << endl;
    
    cout << "3.10:Enter a string including punctuation." << endl;
    getline(cin, str);
    for (auto i : str)
        if (!ispunct(i)) cout << i;
    cout << endl;

    cout << "3.11" << endl;
    const string s = "hello";
    for (auto &i : s)
    {   
        cout << typeid(i).name() << endl;
        break;
    }
    
    return 0;
}

待續……對象

相關文章
相關標籤/搜索