C++ 温习笔记(3) - auto_ptr,string

时间:2022-10-11 00:41:41

auto_ptr

  1. 利用特点“栈上对象在离开作用范围时会自动析构”。
  2. 对于动态分配的内存,其作用范围是程序员手动控制的,这给程序员带来了方便但也不可避免疏忽造成的内存泄漏,毕竟只有编译器是最可靠的。
  3. auto_ptr通过在栈上构建一个对象a,对象a中wrap了动态分配内存的指针p,所有对指针p的操作都转为对对象a的操作。而在a的析构函数中会自动释放p的空间,而该析构函数是编译器自动调用的,无需程序员操心。

auto_ptr用法:
1. 需要包含头文件memory。
2. Constructor:explicit auto_ptr(X* p = 0) throw(); 将指针p交给auto_ptr对象托管。
3. Copy constructor:auto_ptr(const auto_ptr&) throw(); template auto_ptr(const auto_ptr& a) throw(); 指针的托管权会发生转移。
4. Destructor: ~auto_ptr(); 释放指针p指向的空间。
5. 提供了两个成员函数 X* get() const throw(); //返回保存的指针
6. 对象中仍保留指针 X* release() const throw(); //返回保存的指针,对象中不保留指针

auto_ptr <Calculator> calAutoPtr(new Calculator());
Calculator *calculator1 = calAutoPtr.get();
calculator1->Setparameter1(2.0);
calculator1->Setparameter2(3.0);
cout << calculator1->add();

迭代器配置器分别实现了算法,内存和数据结构的分离

(basic_string)string

typedef basic_string<char> string;
typedef basic_string<wchar> wstring;

定义string类对象时,string类自身可以管理内存。

void StringTest::Test1()
{
    string string1("zhxdick");
    string string2(string1, 3);
    cout << string2 << endl;
    string string3(string1, 1, 4);
    cout << string3 << endl;
    char chs[] = "zhxhash";
    string string4(chs, 4);
    cout << string4 << endl;
    string string5(10, 'X');
    cout << string5 << endl;
    string string6(string1, 0, 5);
    cout << string6 << endl;
}

void StringTest::Test2()
{
    string string1("test hash");
    string1.reserve(3);
    //capacity,the size the string can hold before re-allocation of memory
    cout << string1.capacity() << endl;
    cout << string1.size() << endl;
    cout << string1.length() << endl;
    cout << string1.max_size() << endl;

    string1.resize(10);
    cout << string1.capacity() << endl;
    cout << string1.size() << endl;
    cout << string1.length() << endl;
    cout << string1.max_size() << endl;

    string1 = string1 + string1;
    cout << string1 << endl;
    cout << string1.capacity() << endl;
    cout << string1.size() << endl;
    cout << string1.length() << endl;
    cout << string1.max_size() << endl;
}

void StringTest::Test3()
{
    string string1("test");
    const string conststring("const");
    char ch1 = string1[string1.length()];
    char ch2 = conststring[conststring.length()];

    /*char ch3 = string1.at(string1.length()); char ch4 = conststring.at(conststring.length());*/

    cout << ch1 << endl;
    cout << ch2 << endl;
    /*cout << ch3 << endl; cout << ch4 << endl;*/

}

void StringTest::Test4(){
    string string1("1234567890");
    char *p1 = &string1[2];
    char &p2 = string1[3];
    *p1 = 'a';
    p2 = 'b';
    cout << string1 << endl;
    //the following operations are dangerous!
    string1 = "qw";
    *p1 = 'a';
    p2 = 'b';
    cout << string1 << endl;
    string1 = "qwert";
    *p1 = 'a';
    p2 = 'b';
    cout << string1 << endl;
}

void StringTest::Test5(){
    string string1("abcdefg");
    string string2("ABCDEFG");
    string string3("1234567");

    cout << (string1 < string2) << endl;
    cout << (string1 > string2) << endl;

    cout << string1.compare(string2) << endl;
    cout << string2.compare(1, 1, string2) << endl;//b
    cout << string2.compare(1, 1, string2, 4, 2) << endl;//b,EF

}

void StringTest::Test6(){
    string string1("zhxhash");
    string1.assign(6, 'X');
    cout << string1 << endl;
    string1.assign("0123456789");
    string1.erase(7);//erase after(include) index 7
    cout << string1 << endl;
    string1.erase(0, 3);//erase between 0(include) and 3(exclude)
    cout << string1 << endl;
    string string2("string2");
    string1.swap(string2);
    cout << string1 << endl;
    cout << string2 << endl;
    string1.insert(1, string2);//s3456tring2
    cout << string1 << endl;
    string1.insert(0, 5, 'c');//cccccs3456tring2
    cout << string1 << endl;
    string1.append(string1);
    cout << string1 << endl;
}

void StringTest::Test7(){
    string string1("zhxhash");
    string1.replace(3, 4, "dick");
    cout << string1 << endl;

    string::iterator ita, itb, itc, itd;
    ita = string1.begin();
    itb = string1.end();
    itc = string1.begin() + 1;
    itd = string1.end() - 1;

    string1.replace(ita, itb, itc, itd);
    cout << string1 << endl;
}

void StringTest::Test8(){
    string string1("zhxhashzhxdickhxz");
    string::size_type i1 = string1.find("zhx");//0
    string::size_type i2 = string1.rfind("zhx");//7
    cout << (int)i1 << endl;
    cout << (int)i2 << endl;
    string::size_type i3 = string1.find_first_of("has"); //1,first occurence of 'h'
    string::size_type i4 = string1.find_last_of("has"); //14, last occurence of 'h'
    cout << (int)i3 << endl;
    cout << (int)i4 << endl;
    string1.assign("hashashhhihashias");
    string::size_type i5 = string1.find_first_not_of("has");//first character that not in ['h','a','s']
    string::size_type i6 = string1.find_last_not_of("has");//last character that not in ['h','a','s']
    cout << (int)i5 << endl;
    cout << (int)i6 << endl;
}
void StringTest::iteratorTest(){
    string string1("Hello! I am ZhangHaoxin!");
    string string3;
    string temp;
    string::reverse_iterator it2;
    for (it2 = string1.rbegin(); it2 < string1.rend(); it2++){
        temp = *it2;
        string3.append(temp);
    }
    cout << string3 << endl;
}