- ST L容器在C++中经常被使用到,实际开发中作为存储方式也各有其优势和用法,选择一个较优的容器在日常开发中使用,可以有效的解决问题,并且保证程序运行的效率,下面对于经常使用到的容器以及常用方法进行介绍。
vector
//TODO: 源码如下
#include <vector>
template <class T, class Allocator = allocator<T> >
class vector
{
public:
//构造函数
vector()
explicit vector(size_type n, const allocator_type&); // C++14
vector(size_type n, const value_type& value, const allocator_type& = allocator_type());
template <class InputIterator>
vector(const vector& x);
vector(vector&& x)
noexcept(is_nothrow_move_constructible<allocator_type>::value);
vector(initializer_list<value_type> il);
vector(initializer_list<value_type> il, const allocator_type& a);
~vector();
vector& operator=(const vector& x);
vector& operator=(vector&& x)
//迭代器 用于遍历
iterator begin() noexcept; //开始的迭代器
const_iterator begin() const noexcept;
iterator end() noexcept; // 最后的
const_iterator end() const noexcept;
size_type size() const noexcept; ///大小
size_type max_size() const noexcept; //最大值
size_type capacity() const noexcept; //预留空间
bool empty() const noexcept; //判断容器是否为空
void reserve(size_type n); //申请 n*size_type大小的空间
void shrink_to_fit() noexcept; //将vector收缩到合适的空间
reference operator[](size_type n); //支持下表索引的方式查找元素
reference at(size_type n); //查找n位置的元素,越界抛出异常
reference front(); //返回第一个元素,前置
reference back(); //返回最后一个元素
void push_back(const value_type& x); //插入元素
void pop_back(); //删除元素
template <class... Args> iterator emplace(const_iterator position, Args&&... args);
iterator insert(const_iterator position, const value_type& x); //在迭代器的位置插入元素
iterator insert(const_iterator position, value_type&& x);
iterator insert(const_iterator position, size_type n, const value_type& x);
template <class InputIterator>
iterator insert(const_iterator position, InputIterator first, InputIterator last);
iterator insert(const_iterator position, initializer_list<value_type> il);
iterator erase(const_iterator position); //删除迭代器位置的元素
iterator erase(const_iterator first, const_iterator last); //删除迭代器 从 first - last的元素
void clear() noexcept; //清空
void resize(size_type sz); //重制大小
void resize(size_type sz, const value_type& c);
void swap(vector&)。 //交换,把vector容器进行交换
};
//================================
//@param: isFromBegin 是否是按顺序从头到位输出,默认为正序
void travelVector(const std::vector<int>& vec, bool isFormBegin = true)
{
if(vec.empty())
return;
//通过迭代器方式遍历
if(isFormBegin)
{
for(auto itr = vec.begin(); itr!=vec.end(); itr++)
{
cout << *itr;
}
}
else
{
for(auto itr = vec.end(); itr!=vec.begin(); itr++)
{
cout << *itr;
}
}
cout << endl;
}
void printSizeMethod(int vecSize, const char* method)
{
cout << method << "方法的大小是:" << vecSize <<endl;
cout << endl;
}
//@brief 获取第一个元素
int getFirst(const std::vector<int>& vec)
{
//assert(vec.empty());
return vec.front();
}
//@brief 获取最后一个元素
int getLast(const std::vector<int>& vec)
{
//assert(vec.empty()); 一般要判空,不然容易崩溃
return vec.back();
}
//打印单个元素
void printSignleValue(int val , const char * method = nullptr)
{
cout << method << "的值是:" << val <<endl;
cout << endl;
}
int main()
{
std::vector<int> vec;
for(int i = 0; i< 10; i++)
vec.push_back(i);
travelVector(vec);
std::vector<int> vecForSwap;
//前置,末尾元素
int firstValue = getFirst(vec);
int lastValue = getLast(vec);
printSignleValue(firstValue,"vec.front()");
printSignleValue(lastValue,"vec.back()");
//使得vecForSwap当作vec来用,并且把vec清空
vec.swap(vecForSwap);
travelVector(vecForSwap);
//size的区分 vec被交换了,所以里面啥也没有了
printSizeMethod(vec.size(),"vec.size()"); // 0
printSizeMethod(vec.capacity(), "vec.capacity()"); //0
printSizeMethod(vec.max_size(), "vec.max_size()"); //-1
//
printSizeMethod(vecForSwap.size(),"vecForSwap.size()"); //10
printSizeMethod(vecForSwap.capacity(), "vecForSwap.capacity()"); //16
printSizeMethod(vecForSwap.max_size(), "vecForSwap.max_size()"); //-1
}
set
// types:
typedef Key key_type;
typedef key_type value_type;
typedef Compare key_compare;
typedef key_compare value_compare;
typedef Allocator allocator_type;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::size_type size_type;
typedef typename allocator_type::difference_type difference_type;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef implementation-defined iterator; //迭代器
typedef std::reverse_iterator<iterator> reverse_iterator; //反向迭代器
// construct/copy/destroy:
set()
explicit set(const value_compare& comp);
set(const value_compare& comp, const allocator_type& a);
template <class InputIterator>
set(InputIterator first, InputIterator last,
const value_compare& comp = value_compare());
template <class InputIterator>
set(InputIterator first, InputIterator last, const value_compare& comp,
const allocator_type& a);
set(const set& s);
set(set&& s)
explicit set(const allocator_type& a);
set(const set& s, const allocator_type& a);
set(set&& s, const allocator_type& a);
set(initializer_list<value_type> il, const value_compare& comp = value_compare());
set(initializer_list<value_type> il, const value_compare& comp,
const allocator_type& a);
template <class InputIterator>
set(InputIterator first, InputIterator last, const allocator_type& a)
: set(first, last, Compare(), a) {} // C++14
set(initializer_list<value_type> il, const allocator_type& a)
: set(il, Compare(), a) {} // C++14
~set();
set& operator=(const set& s);
set& operator=(set&& s)
set& operator=(initializer_list<value_type> il);
// iterators:
iterator begin() noexcept;
const_iterator begin() const noexcept;
iterator end() noexcept;
const_iterator end() const noexcept;
reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;
reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
const_reverse_iterator crbegin() const noexcept;
const_reverse_iterator crend() const noexcept;
// capacity:
bool empty() const noexcept;
size_type size() const noexcept;
size_type max_size() const noexcept;
// modifiers:
template <class... Args>
pair<iterator, bool> emplace(Args&&... args);
template <class... Args>
iterator emplace_hint(const_iterator position, Args&&... args);
pair<iterator,bool> insert(const value_type& v);
pair<iterator,bool> insert(value_type&& v);
iterator insert(const_iterator position, const value_type& v);
iterator insert(const_iterator position, value_type&& v);
template <class InputIterator>
void insert(InputIterator first, InputIterator last);
void insert(initializer_list<value_type> il);
node_type extract(const_iterator position); // C++17
node_type extract(const key_type& x); // C++17
insert_return_type insert(node_type&& nh); // C++17
iterator insert(const_iterator hint, node_type&& nh); // C++17
iterator erase(const_iterator position);
iterator erase(iterator position); // C++14
size_type erase(const key_type& k);
iterator erase(const_iterator first, const_iterator last);
void clear() noexcept;
template<class C2>
void merge(set<Key, C2, Allocator>& source); // C++17
template<class C2>
void merge(set<Key, C2, Allocator>&& source); // C++17
template<class C2>
void merge(multiset<Key, C2, Allocator>& source); // C++17
template<class C2>
void merge(multiset<Key, C2, Allocator>&& source); // C++17
void swap(set& s)
noexcept(
__is_nothrow_swappable<key_compare>::value &&
(!allocator_type::propagate_on_container_swap::value ||
__is_nothrow_swappable<allocator_type>::value));
// observers:
allocator_type get_allocator() const noexcept;
key_compare key_comp() const;
value_compare value_comp() const;
// set operations:
iterator find(const key_type& k);
const_iterator find(const key_type& k) const;
template<typename K>
iterator find(const K& x);
template<typename K>
const_iterator find(const K& x) const; // C++14
template<typename K>
size_type count(const K& x) const; // C++14
size_type count(const key_type& k) const;
bool contains(const key_type& x) const; // C++20
template<class K> bool contains(const K& x) const; // C++20
iterator lower_bound(const key_type& k);
const_iterator lower_bound(const key_type& k) const;
template<typename K>
iterator lower_bound(const K& x); // C++14
template<typename K>
const_iterator lower_bound(const K& x) const; // C++14
iterator upper_bound(const key_type& k);
const_iterator upper_bound(const key_type& k) const;
template<typename K>
iterator upper_bound(const K& x); // C++14
template<typename K>
const_iterator upper_bound(const K& x) const; // C++14
pair<iterator,iterator> equal_range(const key_type& k);
pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
template<typename K>
pair<iterator,iterator> equal_range(const K& x); // C++14
template<typename K>
pair<const_iterator,const_iterator> equal_range(const K& x) const; // C++14
};
template <class Key, class Compare, class Allocator>
bool
operator==(const set<Key, Compare, Allocator>& x,
const set<Key, Compare, Allocator>& y);
template <class Key, class Compare, class Allocator>
bool
operator< (const set<Key, Compare, Allocator>& x,
const set<Key, Compare, Allocator>& y);
template <class Key, class Compare, class Allocator>
bool
operator!=(const set<Key, Compare, Allocator>& x,
const set<Key, Compare, Allocator>& y);
template <class Key, class Compare, class Allocator>
bool
operator> (const set<Key, Compare, Allocator>& x,
const set<Key, Compare, Allocator>& y);
template <class Key, class Compare, class Allocator>
bool
operator>=(const set<Key, Compare, Allocator>& x,
const set<Key, Compare, Allocator>& y);
template <class Key, class Compare, class Allocator>
bool
operator<=(const set<Key, Compare, Allocator>& x,
const set<Key, Compare, Allocator>& y);
// specialized algorithms:
template <class Key, class Compare, class Allocator>
void
swap(set<Key, Compare, Allocator>& x, set<Key, Compare, Allocator>& y)
noexcept(noexcept(x.swap(y)));
template <class Key, class Compare, class Allocator, class Predicate>
typename set<Key, Compare, Allocator>::size_type
erase_if(set<Key, Compare, Allocator>& c, Predicate pred); // C++20
template <class Key, class Compare = less<Key>,
class Allocator = allocator<Key>>