- For vector, a dynamic array that can store any type of objects and access them by index, we can use methods like push_back(), pop_back(), insert(), erase(), clear(), size(), empty() and swap() to manipulate the elements. For example:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> nums; // create an empty vector
for(int i = 0; i < 5; i++)
{
nums.push_back(i); // add elements at the end
}
cout << "Size: " << nums.size() << endl; // get the number of elements
cout << "Capacity: " << nums.capacity() << endl; // get the allocated space
cout << "Front: " << nums.front() << endl; // get the first element
cout << "Back: " << nums.back() << endl; // get the last element
cout << "Element at index 2: " << nums[2] << endl; // get the element by index
nums.insert(nums.begin() + 1, 10); // insert an element at a specific position
cout << "After insertion: ";
for(int num : nums) // use range-based for loop to iterate over elements
{
cout << num << " ";
}
cout << endl;
nums.erase(nums.begin() + 3); // erase an element at a specific position
cout << "After erasure: ";
for(int num : nums)
{
cout << num << " ";
}
cout<<endl;
return 0;
}
- For map, an associative container that stores key-value pairs in a sorted order based on their keys, we can use methods like insert(), erase(), find(), count(), clear() and swap() to manipulate the elements. We can also use operator[] or at() to access or modify the value associated with a key. For example:
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string, int> scores; // create an empty map
scores["Alice"] = 90; // insert a key-value pair using operator[]
scores["Bob"] = 80;
scores["Charlie"] = 85;
scores.insert(make_pair("David", 95)); // insert a key-value pair using insert()
cout<<"Size: "<<scores.size()<<endl; // get the number of elements
if(scores.find("Bob") != scores.end()) // check if a key exists using find()
{
cout<<"Bob's score: "<<scores["Bob"]<<endl;
scores.erase("Bob"); // erase an element by key using erase()
cout<<"After erasing Bob"<<endl;
}
if(scores.count("Eve") == 0) // check if a key exists using count()
{
scores.at("Eve") = 100; // modify or create a value associated with a key using at()
cout<<"After adding Eve"<<endl;
}
for(auto it = scores.begin(); it != scores.end(); it++)
/* use iterator to iterate over elements
(note that they are sorted by keys in ascending order) */
/* alternatively, we can also use range-based for loop as follows:
for(auto p : scores)
*/
/* p is a pair object that contains first (key) and second (value) fields */
/* alternatively, we can also use structured binding as follows:
for(auto [name,score] : scores)
*/
{
string name = it->first;
int score = it->second;
/* alternatively, we can also use tie as follows:
tie(name,score) = *it;
*/
/* print out each pair */
/* alternatively, we can also use structured binding as follows:
auto [name,score] = *it;
*/
cout<<name<<": "<<score<<endl;
}
return 0;
}
- Set is an associative container that stores unique keys in a sorted order based on their values. Set does not allow two elements to have the same value. Set does not support modifying the value of an element directly because it may break the order of the elements. Set uses a red-black tree as its underlying data structure. Set provides methods such as insert(), erase(), find(), count(), clear() and swap() to manipulate the elements. Set also provides iterators to access and traverse the elements. Set can be constructed with different comparison functions to change its sorting rule.
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> nums; // create an empty set
nums.insert(10); // insert an element using insert()
nums.insert(20);
nums.insert(30);
cout<<"Size: "<<nums.size()<<endl; // get the number of elements
if(nums.find(20) != nums.end()) // check if an element exists using find()
{
cout<<"Found 20"<<endl;
nums.erase(20); // erase an element by value using erase()
cout<<"After erasing 20"<<endl;
}
if(nums.count(40) == 0) // check if an element exists using count()
{
nums.insert(40); // insert another element
cout<<"After inserting 40"<<endl;
}
for(auto it = nums.begin(); it != nums.end(); it++)
/* use iterator to iterate over elements
(note that they are sorted by values in ascending order) */
/* alternatively, we can also use range-based for loop as follows:
for(int num : nums)
*/
{
int num = *it;
/* print out each element */
cout<<num<<endl;
}
return 0;
}
- For unordered_map, an associative container that stores key-value pairs in an unsorted order based on their keys, we can use methods like insert(), erase(), find(), count(), clear() and swap() to manipulate the elements. We can also use operator[] or at() to access or modify the value associated with a key. For example:
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string, int> scores; // create an empty unordered_map
scores["Alice"] = 90; // insert a key-value pair using operator[]
scores["Bob"] = 80;
scores["Charlie"] = 85;
scores.insert(make_pair("David", 95)); // insert a key-value pair using insert()
cout<<"Size: "<<scores.size()<<endl; // get the number of elements
if(scores.find("Bob") != scores.end()) // check if a key exists using find()
{
cout<<"Bob's score: "<<scores["Bob"]<<endl;
scores.erase("Bob"); // erase an element by key using erase()
cout<<"After erasing Bob"<<endl;
}
if(scores.count("Eve") == 0) // check if a key exists using count()
{
scores.at("Eve") = 100; // modify or create a value associated with a key using at()
cout<<"After adding Eve"<<endl;
}
for(auto it = scores.begin(); it != scores.end(); it++)
/* use iterator to iterate over elements
(note that they are not sorted by keys) */
/* alternatively, we can also use range-based for loop as follows:
for(auto p : scores)
*/
/* p is a pair object that contains first (key) and second (value) fields */
/* alternatively, we can also use structured binding as follows:
for(auto [name,score] : scores)
*/
{
string name = it->first;
int score = it->second;
/* alternatively, we can also use tie as follows:
tie(name,score) = *it;
*/
/* print out each pair */
/* alternatively, we can also use structured binding as follows:
auto [name,score] = *it;
*/
cout<<name<<": "<<score<<endl;
}
return 0;
}
- For unordered_set, an associative container that stores unique keys in an unsorted order based on their values, we can use methods like insert(), erase(), find(), count(), clear() and swap() to manipulate the elements. We cannot access or modify individual elements directly because they are immutable. For example:
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<int> nums; // create an empty unordered_set
nums.insert(10); // insert an element using insert()
nums.insert(20);
nums.insert(30);
cout<<"Size: "<<nums.size()<<endl; // get the number of elements
if(nums.find(20) != nums.end()) // check if an element exists using find()
{
cout<<"Found 20"<<endl;
nums.erase(20); // erase an element by value using erase()
cout<<"After erasing 20"<<endl;
}
if(nums.count(40) == 0) // check if an element exists using count()
{
nums.insert(40); // insert another element
cout<<"After inserting 40"<<endl;
}
for(auto it = nums.begin(); it != nums.end(); it++)
/* use iterator to iterate over elements
(note that they are not sorted by values) */
/* alternatively, we can also use range-based for loop as follows:
for(int num : nums)
*/
{
int num = *it;
/* print out each element */
cout<<num<<endl;
}
return 0;
}