Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Data Structures

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:

Example

int

arr

[

5

]

=

{

1

,

2

,

3

,

4

,

5

}

;

cout

<<

arr

[

0

]

;

// Output the first element

Pros and Cons:

2. Struct

A struct allows combining data of different types into a custom data type.

Feature:

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:

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:

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:

5. Stack

A stack is a Last-In-First-Out (LIFO) data structure, commonly used in recursion, depth-first search, and similar scenarios.

Feature:

Example

stack

<
int
s

;

s.

push

(

1

)

;

s.

push

(

2

)

;

cout

<<

s.

top

(

)

;

// Output 2

s.

pop

(

)

;

Pros and Cons:

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:

Example

queue

<
int
q

;

q.

push

(

1

)

;

q.

push

(

2

)

;

cout

<<

q.

front

(

)

;

// Output 1

q.

pop

(

)

;

Pros and Cons:

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:

Example

deque

<
int
dq

;

dq.

push_back

(

1

)

;

dq.

push_front

(

2

)

;

cout

<<

dq.

front

(

)

;

// Output 2

dq.

pop_front

(

)

;

Pros and Cons:

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:

Example

unordered_map

<
string,
int
hashTable

;

hashTable

[

"apple"

]

=

10

;

cout

<<

hashTable

[

"apple"

]

;

// Output 10

Pros and Cons:

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:

Example

map

<
string,
int
myMap

;

myMap

[

"apple"

]

=

10

;

cout

<<

myMap

[

"apple"

]

;

// Output 10

Pros and Cons:

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:

Example

set

<
int
s

;

s.

insert

(

1

)

;

s.

insert

(

2

)

;

cout

<<

s.

begin

(

)

;

// Output 1

Pros and Cons:

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:

Example

vector

<
int
v

;

v.

push_back

(

1

)

;

v.

push_back

(

2

)

;

cout

<<

v

[

0

]

;

// Output 1

Pros and Cons: