温馨提示×

从C++ Cookbook掌握STL容器用法

c++
小樊
83
2024-08-29 18:58:09
栏目: 编程语言

C++ Cookbook 是一本关于 C++ 编程的实用教程,其中包含了许多关于 STL(Standard Template Library)容器的用法示例

  1. 向量(Vector):
#include<iostream>
#include<vector>

int main() {
    std::vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);

    for (int i = 0; i < vec.size(); ++i) {
        std::cout<< vec[i] << " ";
    }

    return 0;
}
  1. 列表(List):
#include<iostream>
#include <list>

int main() {
    std::list<int> lst;
    lst.push_back(1);
    lst.push_back(2);
    lst.push_back(3);

    for (std::list<int>::iterator it = lst.begin(); it != lst.end(); ++it) {
        std::cout << *it << " ";
    }

    return 0;
}
  1. 双端队列(Deque):
#include<iostream>
#include <deque>

int main() {
    std::deque<int> deq;
    deq.push_back(1);
    deq.push_front(0);
    deq.push_back(2);

    for (int i = 0; i < deq.size(); ++i) {
        std::cout << deq[i] << " ";
    }

    return 0;
}
  1. 集合(Set):
#include<iostream>
#include <set>

int main() {
    std::set<int> s;
    s.insert(3);
    s.insert(1);
    s.insert(2);

    for (std::set<int>::iterator it = s.begin(); it != s.end(); ++it) {
        std::cout << *it << " ";
    }

    return 0;
}
  1. 多重集合(Multiset):
#include<iostream>
#include <set>

int main() {
    std::multiset<int> ms;
    ms.insert(3);
    ms.insert(1);
    ms.insert(2);
    ms.insert(3);

    for (std::multiset<int>::iterator it = ms.begin(); it != ms.end(); ++it) {
        std::cout << *it << " ";
    }

    return 0;
}
  1. 映射(Map):
#include<iostream>
#include <map>

int main() {
    std::map<char, int> m;
    m['a'] = 1;
    m['b'] = 2;
    m['c'] = 3;

    for (std::map<char, int>::iterator it = m.begin(); it != m.end(); ++it) {
        std::cout << it->first << ": " << it->second << " ";
    }

    return 0;
}
  1. 多重映射(Multimap):
#include<iostream>
#include <map>

int main() {
    std::multimap<char, int> mm;
    mm.insert(std::make_pair('a', 1));
    mm.insert(std::make_pair('b', 2));
    mm.insert(std::make_pair('c', 3));
    mm.insert(std::make_pair('a', 4));

    for (std::multimap<char, int>::iterator it = mm.begin(); it != mm.end(); ++it) {
        std::cout << it->first << ": " << it->second << " ";
    }

    return 0;
}
  1. 无序集合(Unordered Set):
#include<iostream>
#include <unordered_set>

int main() {
    std::unordered_set<int> us;
    us.insert(3);
    us.insert(1);
    us.insert(2);

    for (std::unordered_set<int>::iterator it = us.begin(); it != us.end(); ++it) {
        std::cout << *it << " ";
    }

    return 0;
}
  1. 无序多重集合(Unordered Multiset):
#include<iostream>
#include <unordered_set>

int main() {
    std::unordered_multiset<int> ums;
    ums.insert(3);
    ums.insert(1);
    ums.insert(2);
    ums.insert(3);

    for (std::unordered_multiset<int>::iterator it = ums.begin(); it != ums.end(); ++it) {
        std::cout << *it << " ";
    }

    return 0;
}
  1. 无序映射(Unordered Map):
#include<iostream>
#include <unordered_map>

int main() {
    std::unordered_map<char, int> um;
    um['a'] = 1;
    um['b'] = 2;
    um['c'] = 3;

    for (std::unordered_map<char, int>::iterator it = um.begin(); it != um.end(); ++it) {
        std::cout << it->first << ": " << it->second << " ";
    }

    return 0;
}
  1. 无序多重映射(Unordered Multimap):
#include<iostream>
#include <unordered_map>

int main() {
    std::unordered_multimap<char, int> umm;
    umm.insert(std::make_pair('a', 1));
    umm.insert(std::make_pair('b', 2));
    umm.insert(std::make_pair('c', 3));
    umm.insert(std::make_pair('a', 4));

    for (std::unordered_multimap<char, int>::iterator it = umm.begin(); it != umm.end(); ++it) {
        std::cout << it->first << ": " << it->second << " ";
    }

    return 0;
}

这些示例展示了如何使用 C++ STL 容器。你可以根据需要选择合适的容器类型,并学习如何使用它们的成员函数来操作数据。在实际项目中,你可能需要根据特定需求和性能要求选择合适的容器。

0