C++ Data Structures
C++ provides a variety of data structures, from basic ones like arrays, structs, and classes, to advanced STL containers such as vector, map and unordered_map , etc.
The following details the commonly used data structures in C++ and their characteristics and usage.
1. Array
An array is the most basic data structure, used to store a group of data of the same type.
Feature:
- Fixed size: once declared, its size cannot be changed.
- Direct element access with O(1) time complexity.
- Suitable for collections whose size is known and whose elements are of the same type.
Example
int
arr
[
5
]
=
{
1
,
2
,
3
,
4
,
5
}
;
cout
<<
arr
[
0
]
;
// Output the first element
Pros and Cons:
- Pros: fast access, compact memory.
- Cons: fixed size, cannot expand dynamically, not suitable for datasets of uncertain size.
2. Struct
A struct allows combining data of different types into a custom data type.
Feature:
- It can contain member variables of different types.
- It provides basic encapsulation of data, but with limited functionality.
Example:
Example
struct
Person
{
string name
;
int
age
;
}
;
Person p
=
{
"Alice"
,
25
}
;
cout
<<
p.
name
<<
endl
;
// Output Alice
3. Class
A class is the core structure in C++ for object-oriented programming, allowing the definition of member variables and member functions. Like struct a struct, but more powerful, supporting inheritance, encapsulation, polymorphism, and other features.
Feature:
- It can contain member variables, member functions, constructors, and destructors.
- It supports object-oriented features such as encapsulation, inheritance, and polymorphism.
Example
class
Person
{
private
:
string name
;
int
age
;
public
:
Person
(
string n,
int
a
)
:
name
(
n
)
, age
(
a
)
{
}
void
printInfo
(
)
{
cout
<<
"Name: "
<<
name
<<
", Age: "
<<
age
<<
endl
;
}
}
;
Person p
(
"Bob"
,
30
)
;
p.
printInfo
(
)
;
// Output: Name: Bob, Age: 30
4. Linked List
A linked list is a dynamic data structure consisting of a series of nodes, each containing data and a pointer to the next node.
Feature:
- Dynamically adjusts its size; no need to define capacity in advance.
- Insertion and deletion are efficient, with O(1) time complexity (when operating at the head or tail of the list).
- Linear search with O(n) time complexity.
Example (singly linked list)
struct
Node
{
int
data
;
Node
next
;
}
;
Node
head
=
nullptr
;
Node
newNode
=
new
Node
{
10
, nullptr
}
;
head
=
newNode
;
// Insert a new node
Pros and Cons:
- Pros: dynamic size, suitable for scenarios with frequent insertion and deletion.
- Cons: inefficient random access; not as fast as direct array access.
5. Stack
A stack is a Last-In-First-Out (LIFO) data structure, commonly used in recursion, depth-first search, and similar scenarios.
Feature:
- Insertion and deletion are allowed only at the top of the stack.
- Time complexity is O(1).
Example
stack
<
int
s;
s.
push
(
1
)
;
s.
push
(
2
)
;
cout
<<
s.
top
(
)
;
// Output 2
s.
pop
(
)
;
Pros and Cons:
- Pros: simple operations, high efficiency.
- Cons: can only operate at the top; accessing other elements requires popping the top element.
6. Queue
A queue is a First-In-First-Out (FIFO) data structure, commonly used in breadth-first search, task scheduling, and similar scenarios.
Feature:
- Insertion is performed at the rear and deletion at the front.
- Time complexity is O(1).
Example
queue
<
int
q;
q.
push
(
1
)
;
q.
push
(
2
)
;
cout
<<
q.
front
(
)
;
// Output 1
q.
pop
(
)
;
Pros and Cons:
- Pros: suitable for scenarios that process data in order, such as task scheduling.
- Cons: cannot access elements randomly.
7. Deque (Double-Ended Queue)
A deque allows insertion and deletion at both ends; it is a combination of a stack and a queue.
Feature:
- Insertion and deletion are allowed at both ends.
- Time complexity is O(1).
Example
deque
<
int
dq;
dq.
push_back
(
1
)
;
dq.
push_front
(
2
)
;
cout
<<
dq.
front
(
)
;
// Output 2
dq.
pop_front
(
)
;
Pros and Cons:
- Pros: flexible bidirectional operations.
- Cons: larger space overhead; suitable for scenarios requiring frequent operations at both ends.
8. Hash Table
A hash table is a data structure that stores data via key-value pairs, supporting fast lookup, insertion, and deletion. In C++, unordered_map is an implementation of a hash table.
Feature:
- Uses a hash function to locate elements quickly, with O(1) time complexity.
- Does not guarantee the order of elements.
Example
unordered_map
<
string,
int
hashTable;
hashTable
[
"apple"
]
=
10
;
cout
<<
hashTable
[
"apple"
]
;
// Output 10
Pros and Cons:
- Pros: efficient lookup, insertion, and deletion.
- Cons: cannot guarantee element order; performance degrades on hash collisions.
9. Map
map is an ordered key-value container implemented with a red-black tree. Unlike unordered_map , it guarantees key order, with O(log n) time complexity for lookup, insertion, and deletion.
Feature:
- Guarantees elements are ordered by key.
- Implemented using a binary search tree.
Example
map
<
string,
int
myMap;
myMap
[
"apple"
]
=
10
;
cout
<<
myMap
[
"apple"
]
;
// Output 10
Pros and Cons:
- Pros: ordered elements, suitable for scenarios requiring ordered data processing.
- Cons: operation efficiency is slightly lower than
unordered_map.
10. Set
set is an ordered collection for storing unique elements, also implemented with a red-black tree underneath. It guarantees elements are unique and ordered.
Feature:
- Guarantees element uniqueness.
- Elements are automatically arranged in ascending order.
- Time complexity is O(log n).
Example
set
<
int
s;
s.
insert
(
1
)
;
s.
insert
(
2
)
;
cout
<<
s.
begin
(
)
;
// Output 1
Pros and Cons:
- Pros: automatic sorting and uniqueness guarantee.
- Cons: insertion and deletion are less efficient than with unordered containers.
11. Vector (Dynamic Array)
vector is a dynamic array implementation provided by the C++ standard library; it can expand capacity dynamically and supports random access.
Feature:
- Dynamically adjusts its size.
- Supports random access with O(1) time complexity.
- When capacity is insufficient, it expands dynamically with amortized O(1) time complexity.
Example
vector
<
int
v;
v.
push_back
(
1
)
;
v.
push_back
(
2
)
;
cout
<<
v
[
0
]
;
// Output 1
Pros and Cons:
- Pros: supports random access and dynamic expansion.
- Cons: less efficient for inserting and deleting elements in the middle.