The STL: Containers and Iterators

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/33

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 4:44 AM on 7/22/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

34 Terms

1
New cards

What are the two most important C++ libraries?

  • Standard Library (STD)

  • Standard Template Library (STL)

2
New cards

What basic constructs does core C++ provide?

  • variables

  • control flow

  • functions

  • classes

  • pointers

3
New cards

What does the Streams Library provide?

  • iostream

  • fstream

  • sstream

Console and file input/output

4
New cards

What does std::string provide?

Safer and dynamic text manipulation

5
New cards

What is the Standard Template Library (STL)?

A C++ library built around generic programming using templates that provides:

  • containers

  • algorithms

  • iterators

  • function objects

6
New cards

What does the C++ Standard Library (STD) provide?

  • Streams library

  • Strings library

  • Memory management

7
New cards

What are the four main parts of STL?

  • Containers

  • Algorithms

  • Iterators

  • Function Objects

8
New cards

What problem do STL containers solve compared to normal arrays?

They automatically manage:

  • resizing

  • memory allocation

  • copying

9
New cards

Why are resizing arrays using new[] error-prone?

It requires manually copying elements and can cause:

  • memory leaks

  • dangling pointers

10
New cards

What is the main difference between an array and a vector?

Array: fixed size
Vector: dynamic size and automatically manages resizing

11
New cards

What is a vector?

A dynamic array stored in one contiguous block of memory

12
New cards

When should you use a vector?

When you:

  • read or iterate a lot

  • need fast indexing

  • insert/remove at the end

13
New cards

What is the syntax to create a vector?

std::vector<int> v{10, 3, 7};
  • std::vector → container type

  • int → element type

  • v → vector name

  • {} → initial values

14
New cards

What does push_back() do?

Adds an element to the end of a vector or deque

v.push_back(42);

15
New cards

What do insert() and erase() do in vectors?

insert() adds an element at a position

v.insert(v.begin()+2,111);

erase() removes an element

v.erase(v.begin());

16
New cards

What is a deque?

A double ended queue made of multiple memory blocks

17
New cards

When should you use a deque instead of a vector?

When you need efficient insert/remove at both ends while still wanting indexing

18
New cards

What are the main deque operations?

d.push_front(-5); // adds to beginning
d.push_back(42); // adds to end

19
New cards

What are container adapters?

Classes that constrain how containers work. They are wrappers around other containers

20
New cards

What is the difference between stack and queue?

Stack:

  • LIFO (last in, first out)

Queue:

  • FIFO (first in, first out)

21
New cards

What are the main stack functions?

s.push(10); // add element
s.top();    // access top element
s.pop();    // remove top element
s.empty();

22
New cards

What are the main queue functions?

q.push(10); 
q.front();  // first element
q.back();   // last element
q.pop();    // removes first element

23
New cards

Why do linked lists exist?

They allow efficient insertion/removal by storing elements in separate nodes connected by pointers

24
New cards

What is the main limitation of vectors and deques?

Inserting in the middle requires shifting many elements

25
New cards

What is a list in STL?

A doubly-linked list where nodes are linked with next/prev pointers

26
New cards

When should you use a list?

When you frequently insert/erase in the middle

Avoid when:

  • you need fast indexing

  • you do lots of traversal/reads

27
New cards

What is an iterator?

Provides a uniform way to traverse containers

28
New cards

What do these iterator operations do?

it++;
it--;
*it;

  • it++ → move to next node

  • it-- → move to previous node

  • *it → access value

29
New cards

How do you create a list iterator?

std::list<int>::iterator it;
  • std::list<int> → container type

  • iterator → iterator type

  • it → iterator name

30
New cards

How do you loop through a list using an iterator?

for(it = l.begin(); it != l.end(); it++)
{
    std::cout << *it;
}
  • begin() → first element

  • end() → after last element

31
New cards

What is a forward_list?

A singly linked list where each node points only to the next node

32
New cards

What are the main forward_list functions?

fl.before_begin(); // position before first element
fl.insert_after(it,111); // insert after position
fl.erase_after(it); // remove after position

33
New cards

Which container should you choose?

Need fast indexing + lots of reading:
→ vector

Need insert/remove at both ends:
→ deque

Need LIFO:
→ stack

Need FIFO:
→ queue

Need frequent middle insertion/removal:
→ list

Need lowest memory overhead linked list:
→ forward_list

34
New cards

Are container adapters (stack/queue) containers themselves?

No. They are wrappers around other containers that restrict how they can be used