{ Thinking in C++第二卷读书笔记 }

  • 1-12-2 创建自己的异常类

    1.12-2 创建自己的异常类

    #include <iostream>
    <!--more-->
    #include <exception>
    using namespace std;
    class MyClass
    {
    class Myexception : public logic_error
    {
    public:
    Myexception(const char *s) : logic_error(s) {}
    };

    public:
    void ThrowExcept() throw(Myexception)
    {
    throw Myexception("my exception");
    }
    };

    int main()
    {
    try
    {
    MyClass mc;
    mc.ThrowExcept();
    }
    catch (const std::exception &e)
    {
    std::cerr << e.what() << '\n';
    }
    }
  • 1-12-5 异常恢复

    1.12-5 异常恢复

    #include <iostream>
    <!--more-->
    #include <exception>
    using namespace std;

    class newBad
    {
    static int i;
    static void *p;

    public:
    void *operator new(size_t sz)
    {
    p = ::operator new(sz);
    cout << "The newBad object number: " << i + 1 << " has been created" << endl;
    if (++i >= 11)
    {
    throw out_of_range("the 11st object created failed.");
    }
    return p;
    }

    static void releaseMemory()
    {
    if (p)
    {
    ::operator delete(p);
    cout << "the " << i << "st object was freed" << endl;
    i--;
    }
    }
    };

    int newBad::i = 0;
    void *newBad::p = nullptr;

    int main(int argc, char const *argv[])
    {
    while (true)
    {
    try
    {
    newBad *mp = new newBad;
    }
    catch (const std::exception &e)
    {
    std::cerr << e.what() << '\n';
    newBad::releaseMemory();
    }
    }
    return 0;
    }

  • 1-12-6 栈反解时再次异常

    1.12-6 栈反解时再次异常

    #include <iostream>
    <!--more-->
    #include <exception>
    using namespace std;

    class BadIdea
    {
    public:
    ~BadIdea()
    {
    throw logic_error("This is a bad exception");
    }
    };

    void my_terminate()
    {
    cout << "This is a bad Idea" << endl;
    exit(0);
    }
    void *psource = (void *)set_terminate(my_terminate);

    int main(int argc, char const *argv[])
    {
    try
    {
    BadIdea ba;
    throw logic_error("test");
    }
    catch (const std::exception &e)
    {
    std::cerr << e.what() << '\n';
    }

    return 0;
    }

  • 1-12-7 异常正常析构的证明

    1.12-7 异常正常析构的证明

    #include <iostream>
    <!--more-->
    #include <exception>

    using namespace std;
    class MyException : public logic_error
    {
    public:
    MyException(const char *s) : logic_error(s) {}
    ~MyException() { cout << "the Exception has been destructed." << endl; }
    };
    class test
    {
    public:
    void throwException() { throw MyException("hello, this is MyException."); }
    };

    int main(int argc, char const *argv[])
    {
    try
    {
    test t;
    t.throwException();
    }
    catch (const std::exception &e)
    {
    std::cerr << e.what() << '\n';
    }

    return 0;
    }

  • 3-7-1 逆转字符串

    3.7-1 逆转字符串

    #include <string>
    <!--more-->
    #include <iostream>
    using namespace std;

    void reverse(string &s)
    {
    int size = s.size();
    for (size_t i = 0; i < size / 2; i++)
    {
    char temp = s[i];
    s[i] = s[size - 1 - i];
    s[size - 1 - i] = temp;
    }
    }
    int main()
    {
    string s("123456");
    cout << s << endl;
    reverse(s);
    cout << s << endl;
    }
  • 3-7-2 测试回文

    3.7-2 测试回文

    #include <string>
    <!--more-->
    #include <iostream>
    using namespace std;

    void reverse(string &s)
    {
    int size = s.size();
    for (size_t i = 0; i < size / 2; i++)
    {
    char temp = s[i];
    s[i] = s[size - 1 - i];
    s[size - 1 - i] = temp;
    }
    }

    int main(int argc, char const *argv[])
    {
    if (argc < 2)
    {
    cout << "argc less than 2!" << endl;
    return -1;
    }

    string s(argv[1]);
    string s2(argv[1]);
    reverse(s);
    cout << (s == s2) << endl;
    return 0;
    }

  • 3-7-3 无视大小写的回文

    3.7-3 无视大小写的回文

    #include <string>
    <!--more-->
    #include <cctype>
    #include <iostream>
    using namespace std;

    void reverse(string &s)
    {
    int size = s.size();
    for (size_t i = 0; i < size / 2; i++)
    {
    char temp = tolower(s[i]);
    s[i] = tolower(s[size - 1 - i]);
    s[size - 1 - i] = temp;
    }
    }

    int main(int argc, char const *argv[])
    {
    if (argc < 2)
    {
    cout << "argc less than 2!" << endl;
    return -1;
    }

    string s(argv[1]);
    string s2(argv[1]);
    for (auto &i : s2)
    i = tolower(i);

    reverse(s);
    cout << (s == s2) << endl;
    return 0;
    }
  • 3-7-7 无视大小写替换字符串

    3.7-7 无视大小写替换字符串

    #include <fstream>
    <!--more-->
    #include <iostream>
    #include <string>
    #include "ichar_traits.h"
    using namespace std;

    istring getData(string filename) throw(std::ifstream::failure)
    {
    std::ifstream streamReader(filename, std::ios::binary);
    if (!streamReader.is_open())
    throw std::ifstream::failure("file couldn't open");
    streamReader.seekg(0, std::ios::end); // 游标移到文件结尾
    unsigned filesize = streamReader.tellg(); // 获取游标当前位置 - 文件开始位置,此处为文件大小
    char *_data = new char[filesize]; // 分配内存
    streamReader.seekg(0, std::ios::beg); // 跳转回开始
    streamReader.read(_data, filesize); // 读取文件
    streamReader.close();
    istring data(_data);
    delete[] _data;
    return std::move(data);
    }

    istring &replaceAll(istring &context,
    const istring &from,
    const istring &to)
    {
    size_t lookHere = 0;
    size_t foundHere;
    while ((foundHere = context.find(from, lookHere)) != istring::npos) // find from lookHere
    { // to avoid the string
    context.replace(foundHere, from.size(), to); //"to" is the substr
    lookHere = foundHere + to.size(); // of "from"
    #ifdef SHOWREPLACE
    cout << "stringNumber:" << foundHere << endl;
    #endif
    }
    return context;
    }

    int main(int argc, char const *argv[])
    {
    istring fileTxt = getData("Rparse.h");
    cout<<fileTxt<<endl;
    replaceAll(fileTxt,"Size","Hello");
    cout<<fileTxt<<endl;
    return 0;
    }