A container adapter is a wrapper around an existing container (like vector
, deque
, etc.) that provides a restricted interfaceโspecialized for a particular usage pattern.
They adapt underlying containers to provide specific functionality like a stack, queue, or priority queue.
For example, a stack adapter uses a vector or deque to provide LIFO (Last In First Out) access, while a queue adapter uses a deque to provide FIFO (First In First Out) access. This allows you to use the underlying container's features while enforcing a specific access pattern.
Adapter | Behavior | Default Underlying Container |
---|---|---|
stack |
LIFO (Last-In-First-Out) | deque |
queue |
FIFO (First-In-First-Out) | deque |
priority_queue |
Highest priority first | vector (with make_heap ) |
The binary_search
function in C++ (from <algorithm>
) checks whether a given element exists in a sorted range.
bool binary_search(start_iterator, end_iterator, value);
// end_iterator: Iterator pointing past the end of the range.
rotate(arr, arr + 3, arr + 5);
This rotates the range [arr, arr+5)
around the point arr + 3
.
vec.resize(new_size); // Resize to new_size elements
vec.resize(new_size, default_value); // Fill new elements with default_value
default_value
is the value to fill the new elements with.end_iterator
(e.g.,v.end()
) points just past the last element.
string str = "Hello";
str.append(" World"); // Appends " World" to str
str += " World"; // Another way to append " World" to str
Use insert
to insert a substring at a specific position in the string.
double add(int = 0, double d = 0.0);
int add(int = 0, double d = 0.0);
These two overloads are NOT allowed because they only differ in return type.
- Allocates raw memory (like
malloc
in C). - Does not call any constructor.
- Returns a
void*
pointer. - Defined in
<new>
.
Static functions can be accessed via objects too, although it's not recommended.
When an object of class C
is instantiated:
- Constructor of
B
is called, - Which calls constructor of
A
, printing A then B. - Data members of
C
are created, again printing A then B. - Finally, constructor of
C
is executed.
Destruction happens in reverse order after main()
ends.
Number n1();
- Looks like it's creating an object, but it's not!
- It declares a function named
n1
returningNumber
.
- C++ prefers function declaration if it can be interpreted that way.
Number n1; // Direct
Number n1{}; // Uniform initialization
MyClass(const char* _s1, const char* _s2, const char* _s3)_*_*_*