培训通网站建设地域名网址查询
目录
vector
1. vector的成员函数
1.1 构造、析构和赋值运算符重载
1.1.1 构造函数
1.1.2 析构函数
1.1.3 赋值运算符重载
1.2 迭代器
1.3 容量
1.4 元素访问
1.4.1 遍历方法
1.5 修改器
1.6 配置器
2. vector的非成员函数
vector
1. vector的成员函数
1.1 构造、析构和赋值运算符重载
1.1.1 构造函数
default | explicit vector(const allocator_type& alloc = allocator_type()); | 默认构造 |
fill | explicit vector(size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type()); | 用n个val来构造 |
range | template <class InputIterator> vector(InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()); | 用迭代器区间构造 |
copy | vector(const vector& x); | 拷贝构造 |
#include <iostream>
#include <vector>
#include <string>using namespace std;int main()
{vector<int> v1;//defaultfor (size_t i = 0; i < v1.size(); ++i){cout << v1[i] << " ";}cout << endl;//空vector<int> v2(10, 1);//fillfor (size_t i = 0; i < v2.size(); ++i){cout << v2[i] << " ";}cout << endl;//1 1 1 1 1 1 1 1 1 1string s("hello world");vector<char> v3(s.begin() + 3, --s.end());//rangefor (size_t i = 0; i < v3.size(); ++i){cout << v3[i] << " ";}cout << endl;//l o w o r lvector<char> v4(v3);//copyfor (size_t i = 0; i < v4.size(); ++i){cout << v4[i] << " ";}cout << endl;//l o w o r lreturn 0;
}
1.1.2 析构函数
~vector(); | 销毁vector类对象 |
1.1.3 赋值运算符重载
copy | vector& operator= (const vector& x); |
1.2 迭代器
begin&end rbegin&rend cbegin&cend crbegin&crend
与string的迭代器类似,详见【C++】string的成员函数、成员常量和非成员函数_秋秋晗晗的博客-CSDN博客中1.2 迭代器
1.3 容量
size max_size resize capacity empty reserve shrink_to_fit
与string的容器类似,详见【C++】string的成员函数、成员常量和非成员函数_秋秋晗晗的博客-CSDN博客中1.3 容器
1.4 元素访问
operator[] at front back data
与string的元素访问类似,详见【C++】string的成员函数、成员常量和非成员函数_秋秋晗晗的博客-CSDN博客中1.4 元素访问
1.4.1 遍历方法
与string的遍历方法类似,详见【C++】string的成员函数、成员常量和非成员函数_秋秋晗晗的博客-CSDN博客中1.4.1 遍历方法
1.5 修改器
assign push_back pop_back insert erase swap clear emplace emplace_back
与string的修改器类似,详见【C++】string的成员函数、成员常量和非成员函数_秋秋晗晗的博客-CSDN博客中1.5 修改器
1.6 配置器
get_allocator | allocator_type get_allocator() const; | 返回配置器 |
2. vector的非成员函数
relational operators | template <class T, class Alloc> bool operator== (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs); template <class T, class Alloc> bool operator!= (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs); template <class T, class Alloc> bool operator< (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs); template <class T, class Alloc> bool operator<= (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs); template <class T, class Alloc> bool operator> (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs); template <class T, class Alloc> bool operator>= (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs); | 关系运算符重载 |
swap | template <class T, class Alloc> void swap(vector<T, Alloc>& x, vector<T, Alloc>& y); | 交换vector的内容 |