代码改变世界

c++ primer学习笔记(3)-字符串操作

2009-04-04 18:11  Clingingboy  阅读(1287)  评论(0编辑  收藏  举报

 

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;
}


输出:
image

比较规则:

(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;
}

bitset部分略

以上为第三章部分