c++標準庫-c++11新特性

Range-Based for循環

  int x2[]{1, 2, 3, 4, 6};
  for (auto i:x2)
  {
    cout << i << endl;
  }
  {
    //等同上面for循環
    for (auto _pos = begin(x2), _end = end(x2); _pos!=_end; ++_pos)
    {
//      auto decl = *_pos;
      cout << *_pos << endl;
    }

    //class template std::initializer_lis<>提供了begin()和end()
    //所以可以如下for循環
    for (auto tmp: {1,2,3,4,5,7})
    {
      cout << tmp << endl;
    }
  }
//避免下面的元素調用了構造和析構函數,下面的for循環採用const reference
template<typename T>
void printElements(const T &coll)
{
  for (const auto &elem : coll)//const reference
  {
    std::cout << elem << std::endl;

  }
}

Move語義和Rvalue Reference

待補充,對實現理解的一知半解

新式字符串字面常量(String Literal)

Raw String Literal

語法:R"delim(....)delim"
其中delim是一個字符序列,最多16個基本字符,不可以包含反斜線、空格、小括號。
其允許我們定義字符序列(character sequence),從而避免書寫很多的轉移字符。

  std::string x3(R"(\\n)");
  std::string x4("\\\\n");//同上
  std::string rawStr = R"(\\n)";
  std::string rawStr = R"nc(a\
                            b\nc()"
                            )nc";
  cout << rawStr << endl;
  //完整語法書輸出
  cout << "===========" << endl;
  /*
  a\
                            b\nc()"
                            
===========
*/

編碼的String Literal

  • u8 以UTF-8編定的某個字符起頭,字符類型爲const char
  • u 定義一個string literal,帶着類型爲char16_t
  • U定義一個string literal,帶着類型爲char32_t
  • L定義一個wide string literal,帶着類型爲wchar_t的字符
  • Raw string開頭的那個R的前面還可以放置一個編碼前綴

關鍵字noexcept

用來指明某個函數無法——或不打算——拋出異常。例如:

void foo() noexcept;//如果foo()有異常未被處理或者拋出異常,則程序會被終止
//然後std::terminate()被調用並默認調用std::abort()

關鍵字constexpr

不是很瞭解用法,需要後續補充

//constexpr  用於讓表達式在編譯期通過檢查
constexpr int square(int x)
{
  return x*x;
}
float a[square(9)];//constexpr用來在這裏使得數組通過編譯期運算

# main()定義式
c++標準中定義“正確且具有移植性”的main()如下所示,c++中隱式的定義了一個return 0;

int main()
{
}int main(int argc, char * argv[])
{
}

異常Execption

一下示範如何使用一個泛型函數來處理(這裏只是打印)不同的異常

#include <exception>
#include <system_error>
#include <future>
#include <iostream>

template <typename T>
void processCodeException (const T& e)
{
    using namespace std;
    auto c = e.code();
    cerr << "- category:     " << c.category().name() << endl;
    cerr << "- value:        " << c.value() << endl;
    cerr << "- msg:          " << c.message() << endl;
    cerr << "- def category: "
         << c.default_error_condition().category().name() << endl;
    cerr << "- def value:    "
         << c.default_error_condition().value() << endl;
    cerr << "- def msg:      "
         << c.default_error_condition().message() << endl;
}

void processException()
{
    using namespace std;
    try {
        throw;  // rethrow exception to deal with it here
    }
    catch (const ios_base::failure& e) {
        cerr << "I/O EXCEPTION: " << e.what() << endl;
        processCodeException(e);
    }
    catch (const system_error& e) {
        cerr << "SYSTEM EXCEPTION: " << e.what() << endl;
        processCodeException(e);
    }
    catch (const future_error& e) {
        cerr << "FUTURE EXCEPTION: " << e.what() << endl;
        processCodeException(e);
    }
    catch (const bad_alloc& e) {
        cerr << "BAD ALLOC EXCEPTION: " << e.what() << endl;
    }
    catch (const exception& e) {
        cerr << "EXCEPTION: " << e.what() << endl;
    }
    catch (...) {
        cerr << "EXCEPTION (unknown)" << endl;
    }
}


//那麼我們可以如下使用方式來處理異常
try{

}
catch(...)
{
    processException();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章