Skip to content

Commit 5733b0b

Browse files
[Term Entry] C++ Strings: .capacity()
* docs: Add term entry for C++ Strings: capacity() * updated content * Update capacity.md ---------
1 parent ba47e4b commit 5733b0b

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
Title: '.capacity()'
3+
Description: 'Returns the number of characters a string can contain before allocating new memory.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Characters'
9+
- 'Memory'
10+
- 'Methods'
11+
- 'Strings'
12+
CatalogContent:
13+
- 'learn-c-plus-plus'
14+
- 'paths/computer-science'
15+
---
16+
17+
In C++, the **`.capacity()`** method returns the amount of storage currently allocated for the string. This capacity is always equal to or greater than the value returned by the [`.size()`](https://www.codecademy.com/resources/docs/cpp/strings/size) method. The `.capacity()` method is useful for optimizing performance in scenarios where a string undergoes frequent modifications. It helps determine how much memory is already allocated, which can reduce unnecessary reallocations during operations such as appending in loops or constructing large strings. When used alongside `.reserve()`, it enables preallocation of sufficient space, enhancing efficiency in performance-critical applications.
18+
19+
## Syntax
20+
21+
```pseudo
22+
string.capacity();
23+
```
24+
25+
**Parameters:**
26+
27+
- The `.capacity()` method does not take any parameters.
28+
29+
**Return value:**
30+
31+
Returns a `size_t` value representing the number of characters the string can hold without requiring a reallocation of memory.
32+
33+
## Example
34+
35+
This example demonstrates the basic usage of the `.capacity()` method and how it can differ from the `.size()` of the string:
36+
37+
```cpp
38+
#include <iostream>
39+
#include <string>
40+
41+
using namespace std;
42+
43+
int main() {
44+
string message = "Hello";
45+
46+
cout << "String: " << message << '\n';
47+
cout << "Size: " << message.size() << '\n';
48+
cout << "Capacity: " << message.capacity() << '\n';
49+
50+
return 0;
51+
}
52+
```
53+
54+
The output of the code above will be:
55+
56+
```shell
57+
String: Hello
58+
Size: 5
59+
Capacity: 15
60+
```
61+
62+
## Codebyte Example
63+
64+
This codebyte example shows how the `.capacity()` of a string can grow when needed, and how it may stay the same when there's already enough reserved space:
65+
66+
```codebyte/cpp
67+
#include <iostream>
68+
#include <string>
69+
70+
using namespace std;
71+
72+
int main() {
73+
string text = "Hi";
74+
75+
cout << "Initial string:\n";
76+
cout << "Text: " << text << '\n';
77+
cout << "Capacity: " << text.capacity() << "\n\n";
78+
79+
// Add characters to increase capacity
80+
text += " there, how are you?";
81+
cout << "After adding more characters:\n";
82+
cout << "Text: " << text << '\n';
83+
cout << "Capacity: " << text.capacity() << "\n\n";
84+
85+
// Add one more character
86+
text += "!";
87+
cout << "After adding one more character:\n";
88+
cout << "Text: " << text << '\n';
89+
cout << "Capacity: " << text.capacity() << '\n';
90+
91+
return 0;
92+
}
93+
```

0 commit comments

Comments
 (0)