C++ 字串庫 - reserve



描述

它請求更改容量。

宣告

以下是 std::string::reserve 的宣告。

void reserve (size_t n = 0);

C++11

void reserve (size_t n = 0);

引數

n − 字串的計劃長度。

返回值

異常

如果丟擲異常,字串不會發生任何更改。

示例

以下為 std::string::reserve 的示例。

#include <iostream>
#include <fstream>
#include <string>

int main () {
   std::string str;

   std::ifstream file ("test.txt",std::ios::in|std::ios::ate);
   if (file) {
      std::ifstream::streampos filesize = file.tellg();
      str.reserve(filesize);

      file.seekg(0);
      while (!file.eof()) {
         str += file.get();
      }
   std::cout << str;
   }
   return 0;
}
string.htm
廣告
© . All rights reserved.