public member function
<string>
std::basic_string::reservevoid reserve (size_type n = 0);
Request a change in capacity
Requests that the string capacity be adapted to a planned change in size to a length of up to n characters.If n is greater than the current string capacity, the function causes the container to increase its capacity to n characters (or greater).
In all other cases, it is taken as a non-binding request to shrink the string capacity: the container implementation is free to optimize otherwise and leave the basic_string with a capacity greater than n.
This function has no effect on the string length and cannot alter its content.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 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;
}
If n is greater than the max_size, a length_error exception is thrown.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4