Cpp_Document
Cpp_Document
1
Example 1: Convert 210 to 10y
10 × 0.301 = 3.01
210 ≈ 103
(Actual: 1024 ≈ 103 )
Example 2: Convert 232 to 10y
32 × 0.301 = 9.63
2
7 How std::string Works Internally
7.1 Basic Structure
Internally, std::string maintains:
Example:
# include < iostream >
# include < string >
int main () {
string s = " Hello " ;
cout << " Address of s : " << ( void *) s . data () << endl ;
}
Output Example:
Address of s: 0x5642d9f2a6f0
This address points to a contiguous array of characters.
3
8 Small String Optimization (SSO)
For short strings ( 15 characters or less), std::string stores characters
inside itself (without heap allocation).
Example:
# include < iostream >
# include < string >
int main () {
string small = " Hello " ; // Stored in stack ( SSO )
string large = " This is a very long string " ; // Stored in heap
cout << " Address of small : " << ( void *) small . data () << endl ;
cout << " Address of large : " << ( void *) large . data () << endl ;
}
Output Example:
Address of small: 0x7ffe5f2c34a8 (Stack memory - SSO)
Address of large: 0x5642d9f2a6f0 (Heap memory)
Why SSO? SSO avoids heap allocation for short strings, making oper-
ations faster.
4
| ’T’ | ’h’ | ’i’ | ’s’ | ’ ’ | ... | ’g’ | ’\0’ |
--------------------------------
11 Summary
• ✓ std::string uses a dynamic array, not a linked list.
5
Example:
char cstr [] = " Hello " ; // Stored in stack
cout << cstr ; // Output : Hello
int main () {
std :: string s = " Hello " ; // Uses dynamic memory ( or SSO )
s += " World " ; // A u t o m a t i c a l l y resizes
std :: cout << s ; // Output : Hello World
}
6
• Dynamic resizing happens automatically.
• No null-termination is required because std::string keeps track of
size.
Example:
char ch = ’A ’; // 1 byte
std :: string s = " Hello " ; // Each c h a r a c t e r = 1 byte
std :: wstring ws = L " Hello " ; // Each c h a r a c t e r = 2+ bytes ( UTF -16)
7
– ✓ It’s safer (avoids buffer overflow).
– ✓ It’s easier (automatic resizing, no need to manage memory).
– ✓ It’s optimized (SSO makes small strings very fast).
16 Final Summary
• ✓ Both char[] and std::string store characters in an array, NOT
linked lists.
• ✓ Each character usually takes 1 byte (char) unless using Unicode.
void testCString () {
auto start = h i g h _ r e s o l u t i o n _ c l o c k :: now () ;
8
str [ N ] = ’ \0 ’; // Null t e r m i n a t i o n
void testCppString () {
auto start = h i g h _ r e s o l u t i o n _ c l o c k :: now () ;
int main () {
testCString () ;
testCppString () ;
return 0;
}
19 Expected Results
Operation char[] (C-Style) std::string
String Creation ✓ Faster (Direct memory allocation) ✗ Slightly slower (Dynamic management)
Concatenation ✗ Slower (strcpy & reallocation) ✓ Faster (+= optimization)
Iteration ✓ Fast (Direct memory access) ✓ Almost the same speed
Memory Efficiency ✓ Less overhead (no extra capacity) ✗ Extra capacity allocated (for resizing)
Ease of Use ✗ Harder (manual memory management) ✓ Easier (automatic memory management)
9
• std::string avoids strcpy overhead in concatenation.
• std::string uses Small String Optimization (SSO), making small
strings faster than char[].
22 Conclusion
• For fixed-size, performance-critical applications → Use char[].
• For general-purpose, safer, and flexible string handling → Use
std::string.
23 1. Comparison Table
10
24.2 2. Heap Allocation (Avoids Stack Overflow)
If constraints are large (N ≤ 106 or 107 ), arrays risk stack overflow, while vectors
use heap memory.
// Large array ( Risky !)
int arr [1000000];
26 4. Code Comparison
26.1 Array Version (More Manual Work)
# include < iostream >
# include < algorithm >
using namespace std ;
int main () {
int arr [] = {3 , 1 , 4 , 1 , 5};
int N = sizeof ( arr ) / sizeof ( arr [0]) ;
11
sort ( arr , arr + N ) ;
int main () {
vector < int > v = {3 , 1 , 4 , 1 , 5};
sort ( v . begin () , v . end () ) ;
27 5. Conclusion
• Arrays (int arr[N]) → Faster but fixed-size, stack-allocated, error-
prone.
• Vectors (vector<int> v) → Dynamic, safer, STL-supported, preferred
in CP.
28 Initialization Functions
28.1 Creating a Vector
vector < int > v1 ; // Empty vector
vector < int > v2 (5) ; // Vector of size 5 , i n i t i a l i z e d with
default values (0)
vector < int > v3 (5 , 100) ; // Vector of size 5 , i n i t i a l i z e d with 100
vector < int > v4 = {1 , 2 , 3}; // Vector i n i t i a l i z e d with given
values
12
29 Size and Capacity Functions
29.1 Checking Size (size)
vector < int > v = {1 , 2 , 3 , 4 , 5};
cout << v . size () ; // Output : 5
13
31 Modifying Functions
31.1 Adding Elements
vector < int > v ;
v . push_back (10) ; // {10}
v . push_back (20) ; // {10 , 20}
14
32.3 Finding an Element (find)
vector < int > v = {10 , 20 , 30};
if ( find ( v . begin () , v . end () , 20) != v . end () )
cout << " Found ! " ;
33 Advanced Functions
33.1 Swapping Two Vectors (swap)
vector < int > a = {1 , 2 , 3};
vector < int > b = {10 , 20 , 30};
a . swap ( b ) ; // Now a = {10 , 20 , 30} , b = {1 , 2 , 3}
34 Iterators in Vectors
34.1 Iterating Over a Vector
vector < int > v = {1 , 2 , 3};
for ( auto it = v . begin () ; it != v . end () ; it ++)
cout << * it << " " ;
15
36 Passing an Array to a Function
In C++, arrays cannot be passed by value because they decay into pointers.
Instead, they are passed as pointers (*).
int main () {
int arr [] = {1 , 2 , 3 , 4 , 5};
int size = sizeof ( arr ) / sizeof ( arr [0]) ;
16
37.1 Pass by Value (Makes a Copy)
# include < iostream >
# include < vector >
using namespace std ;
int main () {
vector < int > v = {1 , 2 , 3 , 4 , 5};
modifyVector ( v ) ;
cout << v [0]; // Output : 1 ( u n c h a n g e d )
}
Bad for large vectors → Creates a copy (slow for large data).
int main () {
vector < int > v = {1 , 2 , 3 , 4 , 5};
modifyVector ( v ) ;
cout << v [0]; // Output : 100 ( m o d i f i e d )
}
int main () {
vector < int > v = {1 , 2 , 3 , 4 , 5};
modifyVector (& v ) ; // Pass address
cout << v [0]; // Output : 100
}
17
38 Summary Table
Passing Type Syntax (Array) Syntax (Vector) Modifies Original? Memory Efficient?
By Value ✗ Not possible void func(vector<int> v) ✗ No ✗ No (Copy created)
By Pointer (*) void func(int *arr, int size) void func(vector<int> *v) ✓ Yes ✓ Yes
By Reference (&) void func(int (&arr)[N]) void func(vector<int> &v) ✓ Yes ✓ Yes
18