天天看點

c++ primer學習筆記(3)-字元串操作

1.命名空間的using聲明

#include <iostream>

// using declarations for names from the standard library
using std::cin;
using std::cout;
using std::endl;
int main()
{
    cout << "Enter two numbers:" << endl;

    int v1, v2;
    cin >> v1 >> v2;

    cout << "The sum of " << v1 
         << " and " << v2
         << " is " << v1 + v2 << endl;

    return 0;
}
      

必須為每個用到的名字using聲明

2.标準庫string類型

(1)初始化 4個構造函數

簡單字元串讀寫

#include <string>
#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::string;

// Note: #include and using declarations must be added to compile this code
int main()
{
    string s;          // empty string
    cin >> s;          // read whitespace-separated string into s
    cout << s << endl; // write s to the output
    return 0;
}      

2.1 string對象的操作

size() 傳回字元個數和empty()判斷是是否為空字元

#include <string>
#include <iostream>

using std::cout;
using std::endl;
int main()
{
    string st("The expense of spirit\n");
    cout << "The size of " << st << "is " << st.size()
         << " characters, including the newline" << endl;
    return 0;
}
      

注意:size()方法傳回是size_type類型

2.2 string關系操作符

#include <string>
using std::string;
#include <iostream>
using std::cout; using std::endl;

int main()
{
    string substr = "Hello";
    string phrase = "Hello World";
    string slang  = "Hiya";

    if (substr < phrase) cout << "substr is smaller" << endl;
    if (slang > substr) cout << "slang is greater" << endl;
    if (slang > phrase) cout << "slang is greater" << endl;

    return 0;
}
      

輸出:

c++ primer學習筆記(3)-字元串操作

比較規則:

(1).前面字元相同,按字元長度來算

(2)字元不同,則比較第一個不比對的字元.

2.3字元串的相加

(1)string對象相加

string s1("hello, ");
string s2("world\n");

string s3 = s1 + s2;   // s3 is hello, world\n      

産生新的字元串s3

(2)和字元串子面值連接配接

string s1("hello");
string s2("world");

string s3 = s1 + ", " + s2 + "\n";      

(3)以下是錯誤的(字元串面值不得相加)

string a="xx"+"xx";

string b=”xx”+”xx”+xx;

以下做法是正确的,string對象+ 字元串面值 再+字元串面值

string c=xx+"xx”+"xx”;

2.4從string對象擷取字元

string str("some string");

for (string::size_type ix = 0; ix != str.size(); ++ix)
    cout << str[ix] << endl;

for (string::size_type ix = 0; ix != str.size(); ++ix)
    str[ix] = '*';

cout << str << endl;      

以上利用字元串索引,str[ix],ix稱為索引或下标.

2.5 string對象字元的處理(即sting的輔助類)

位于cctype頭檔案中

#include <string>
#include <iostream>
#include <cctype>
using std::string;
using std::isupper; using std::toupper;
using std::islower; using std::tolower;
using std::isalpha; using std::isspace;
using std::cout; using std::endl;

int main()
{
    
    string s("Hello World!!!");
    string::size_type punct_cnt = 0; 
    
    // count number of punctuation characters in s
    for (string::size_type index = 0; index != s.size(); ++index)
        if (ispunct(s[index]))
            ++punct_cnt;

    cout << punct_cnt 
         << " punctuation characters in " << s << endl;

{
    // convert s to lowercase
    for (string::size_type index = 0; index != s.size(); ++index) 
        s[index] = tolower(s[index]);

    cout << s << endl;
}
    return 0;
}
      

3.vertor(數組的進階類型,c#的泛型…)

是一種類型的對象集合,稱為容器,又稱類模闆…,這裡先熟悉用法.

3.1定義和初始化

即構造函數,了解構造函數的用法就好.

3.2對象操作

#include <iostream>

#include <vector>

using std::cout;
using std::endl;
using std::vector;


int main()
{
    // empty vector 
    vector<int> ivec;   
    int val;
    // give each element a new value
    for (vector<int>::size_type ix = 0;
                                ix != 10; ++ix)
          ivec.push_back(ix);

    cout << "ivec.size: " << ivec.size() << endl;  // prints 10

    // reset the elements in the vector to zero
    for (vector<int>::size_type ix = 0; ix != ivec.size(); ++ix)
        ivec[ix] = ix;

    // is there anything to print?
    if (ivec.empty() == false) {
        cout << "vector contents: " << endl;
        // print each element separated by a newline
        for (vector<int>::size_type ix = 0; 
                          ix != ivec.size(); ++ix)
            cout << ivec[ix] << endl;
     }
     return 0;
}      

4.3疊代器

#include <vector>
#include <string>
#include <iostream>

using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
    vector<int> ivec(10);

    // reset all the elements in ivec to 0
    for (vector<int>::size_type ix = 0; ix != ivec.size(); ++ix)
            ivec[ix] = 0;

    // print what we've got so far: should print 10 0's
    for (vector<int>::size_type ix = 0; ix != ivec.size(); ++ix)
            cout << ivec[ix] << " ";
    cout << endl;
    
    // equivalent loop using iterators to reset all the elements in ivec to 0
    for (vector<int>::iterator iter = ivec.bbegin();
                               iter != ivec.end(); ++iter)
        *iter = 0;  // set element to which iter refers to 0

    // print using iterators: should print 10 0's
    for (vector<int>::iterator iter = ivec.begin();
                               iter != ivec.end(); ++iter)
            cout << *iter << " "; // print the element to which iter refers 
    cout << endl;

    vector<int>::iterator iter = ivec.begin();
    while (iter != ivec.end()) {
            *iter = 0;
            ++iter;
    }
    return 0;
}