1、集合类模板 set/multiset

#include <iostream>
#include <set>

using namespace std;

int main(int argc, char *argv[]) 
{
    // 创建一个空的 set
    set<int> mySet;

    // size()
    cout << "Initial size: " << mySet.size() << endl;

    // empty()
    cout << "Is set empty? " << (mySet.empty() ? "Yes" : "No") << endl;

    // insert()
    mySet.insert(10);
    mySet.insert(20);
    mySet.insert(30);
    cout << "After insert: ";
    for (int i : mySet) {cout << i << " ";}
    cout << endl;

    // find()
    auto it = mySet.find(20);
    if (it != mySet.end()) 
    {
        cout << "Found element: " << *it << endl;
    }
     else 
     {
        cout << "Element not found." << endl;
    }

    // count()
    cout << "Count of 20: " << mySet.count(20) << endl;
    cout << "Count of 40: " << mySet.count(40) << endl;

    // erase()
    mySet.erase(20);
    cout << "After erase 20: ";
    for (int i : mySet) {cout << i << " ";}
    cout << endl;

    // clear()
    mySet.clear();
    cout << "After clear: ";
    cout << "Size: " << mySet.size() << endl;

    // insert
    mySet.insert({1, 2, 3, 4, 5});
    cout << "After insert multiple elements: ";
    for (int i : mySet) {cout << i << " ";}
    cout << endl;

    // upper_bound() 和 lower_bound()
    it = mySet.lower_bound(3);
    cout << "Lower bound of 3: " << *it << endl;

    it = mySet.upper_bound(3);
    cout << "Upper bound of 3: " << *it << endl;

    // equal_range()
    pair<set<int>::iterator, set<int>::iterator> range = mySet.equal_range(3);
    cout << "Equal range of 3: ";
    for (auto it = range.first; it != range.second; ++it) 
    {
        cout << *it << " ";
    }
    cout << endl;

    // swap()
    set<int> anotherSet = {10, 20, 30};
    mySet.swap(anotherSet);
    cout << "After swap with anotherSet: ";
    for (int i : mySet) {cout << i << " ";}
    cout << endl;

    // 迭代器it
    cout << "Iterating through set: ";
    for (auto it = mySet.begin(); it != mySet.end(); ++it) 
    {
        cout << *it << " ";
    }
    cout << endl;

    return 0;
}
xuanmiao@linux:~/Test/C++$ ./test
Initial size: 0
Is set empty? Yes
After insert: 10 20 30 
Found element: 20
Count of 20: 1
Count of 40: 0
After erase 20: 10 30 
After clear: Size: 0
After insert multiple elements: 1 2 3 4 5 
Lower bound of 3: 3
Upper bound of 3: 4
Equal range of 3: 3 
After swap with anotherSet: 10 20 30 
Iterating through set: 10 20 30 

 

2、键值对模板 map/multimap

#include <iostream>
#include <map>

using namespace std;

int main(int argc, char * argv[]) 
{
    // 创建一个空的 map
    map<int, string> myMap;

    // size()
    cout << "Initial size: " << myMap.size() << endl;

    // empty()
    cout << "Is map empty? " << (myMap.empty() ? "Yes" : "No") << endl;

    // insert()
    myMap.insert({1, "Apple"});
    myMap.insert({2, "Banana"});
    myMap.insert({3, "Cherry"});
    cout << "After insert: ";
    for (const auto& pair : myMap)
    {
        cout << pair.first << ": " << pair.second << " ";
    }
    cout << endl;

    // at()
    try 
    {
        cout << "Value at key 2: " << myMap.at(2) << endl;
        cout << "Value at key 10: " << myMap.at(10) << endl; // 超出范围,会抛出异常
    } 
    catch(const out_of_range& e) 
     {
        cout << "Error: " << e.what() << endl;
    }

    // operator[]
    myMap[4] = "Date";
    cout << "Value at key 4: " << myMap[4] << endl;

    // find()
    auto it = myMap.find(3);
    if (it != myMap.end()) 
    {
        cout << "Found key 3 with value: " << it->second << endl;
    } 
    else
    {
        cout << "Key 3 not found." << endl;
    }

    // count()
    cout << "Count of key 2: " << myMap.count(2) << endl;
    cout << "Count of key 5: " << myMap.count(5) << endl;

    // erase()
    myMap.erase(2);
    cout << "After erase key 2: ";
    for (const auto& pair : myMap) 
    {
        cout << pair.first << ": " << pair.second << " ";
    }
    cout << endl;

    // clear()
    myMap.clear();
    cout << "After clear: ";
    cout << "Size: " << myMap.size() << endl;

    // insert
    myMap.insert({{1, "Apple"}, {2, "Banana"}, {3, "Cherry"}});
    cout << "After insert multiple pairs: ";
    for (const auto& pair : myMap) 
    {
        cout << pair.first << ": " << pair.second << " ";
    }
    cout << endl;

    // upper_bound() 和 lower_bound()
    it = myMap.lower_bound(2);
    cout << "Lower bound of key 2: " << it->first << ": " << it->second << endl;

    it = myMap.upper_bound(2);
    cout << "Upper bound of key 2: " << it->first << ": " << it->second << endl;

    // equal_range()
    pair<map<int, string>::iterator, map<int, string>::iterator> range = myMap.equal_range(2);
    cout << "Equal range of key 2: ";
    for (auto it = range.first; it != range.second; ++it) 
    {
        cout << it->first << ": " << it->second << " ";
    }
    cout << endl;

    // swap()
    map<int, string> anotherMap = {{4, "Date"}, {5, "Elderberry"}};
    myMap.swap(anotherMap);
    cout << "After swap with anotherMap: ";
    for (const auto& pair : myMap) 
    {
        cout << pair.first << ": " << pair.second << " ";
    }
    cout << endl;

    cout << "Iterating through map: ";
    for (auto it = myMap.begin(); it != myMap.end(); ++it) 
    {
        cout << it->first << ": " << it->second << " ";
    }
    cout << endl;

    return 0;
}
xuanmiao@linux:~/Test/C++$ ./test
Initial size: 0
Is map empty? Yes
After insert: 1: Apple 2: Banana 3: Cherry 
Value at key 2: Banana
Value at key 10: Error: map::at
Value at key 4: Date
Found key 3 with value: Cherry
Count of key 2: 1
Count of key 5: 0
After erase key 2: 1: Apple 3: Cherry 4: Date 
After clear: Size: 0
After insert multiple pairs: 1: Apple 2: Banana 3: Cherry 
Lower bound of key 2: 2: Banana
Upper bound of key 2: 3: Cherry
Equal range of key 2: 2: Banana 
After swap with anotherMap: 4: Date 5: Elderberry 
Iterating through map: 4: Date 5: Elderberry 
posted on 2025-05-07 07:48  轩~邈  阅读(11)  评论(0)    收藏  举报