Junior — Middle
What containers from the C++ standard library do you use for organizing data in your projects?
sobes.tech AI
Answer from AI
In the standard C++ library, the following containers are often used for data organization:
- std::vector — a dynamic array, convenient for storing elements with quick access by index.
- std::list — a doubly linked list, useful for frequent insertions and deletions in the middle.
- std::deque — a double-ended queue, supports fast insertions and deletions at both ends.
- std::map — an associative container implementing a balanced tree, storing key-value pairs with unique keys.
- std::unordered_map — a hash table for fast key-based access without ordering.
- std::set and std::unordered_set — containers for storing unique elements with different organization methods.
The choice of container depends on requirements for access speed, insertion, deletion, and the need for data ordering.
Example of using std::vector:
#include <vector>
#include <iostream>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
numbers.push_back(6);
for (int num : numbers) {
std::cout << num << " ";
}
return 0;
}