-
Notifications
You must be signed in to change notification settings - Fork 52
cpp_std_vector
jiaxw32 edited this page Jul 15, 2020
·
2 revisions
C++标准库 vector 类是序列容器的类模板。 矢量以线性方式存储给定类型的元素,并允许快速随机访问任何元素。 当随机访问性能处于高级版时,矢量是序列的首选容器。
template <class Type, class Allocator = allocator<Type>>
class vector
- 向量允许在序列末尾插入和删除常量事件。 若要在矢量中间插入或删除元素,则需要线性时间。
- 当成员函数必须将矢量对象中所含序列增加到超过其当前存储容量时,将进行矢量重新分配。 其他的插入和删除均可能改变序列中的各个存储地址。
#include <vector>
#include <iostream>
int main()
{
using namespace std;
vector <int>::iterator v1_Iter, v2_Iter, v3_Iter, v4_Iter, v5_Iter, v6_Iter;
// Create an empty vector v0
vector <int> v0;
// Create a vector v1 with 3 elements of default value 0
vector <int> v1(3);
// Create a vector v2 with 5 elements of value 2
vector <int> v2(5, 2);
// Create a copy, vector v4, of vector v2
vector <int> v4(v2);
// Create a new temporary vector for demonstrating copying ranges
vector <int> v5(5);
for (auto i : v5) {
v5[i] = i;
}
}
返回对矢量中指定位置的元素的引用。
reference at(size_type position);
const_reference at(size_type position) const;
示例:
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
vector <int> v1;
v1.push_back( 10 );
v1.push_back( 20 );
const int &i = v1.at( 0 );
int &j = v1.at( 1 );
cout << "The first element is " << i << endl;
cout << "The second element is " << j << endl;
}
返回对向量中第一个元素的引用。 如果向量为空,则返回值不确定。
reference front();
const_reference front() const;
示例:
// vector_front.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
vector <int> v1;
v1.push_back( 10 );
v1.push_back( 11 );
int& i = v1.front( );
const int& ii = v1.front( );
cout << "The first integer of v1 is "<< i << endl;
// by incrementing i, we move the front reference to the second element
i++;
cout << "Now, the first integer of v1 is "<< i << endl;
}
返回对向量中最后一个元素的引用。如果向量为空,则返回值不确定。
reference back();
const_reference back() const;
示例:
#include <vector>
#include <iostream>
int main() {
using namespace std;
vector <int> v1;
v1.push_back( 10 );
v1.push_back( 11 );
int& i = v1.back( );
const int& ii = v1.front( );
cout << "The last integer of v1 is " << i << endl;
i--;
cout << "The next-to-last integer of v1 is "<< ii << endl;
}
对该向量中第一个元素返回随机访问迭代器。
const_iterator begin() const;
iterator begin();
示例:
#include <vector>
#include <iostream>
int main()
{
using namespace std;
vector<int> c1;
vector<int>::iterator c1_Iter;
c1.push_back(1);
c1.push_back(2);
cout << "The vector c1 contains elements:";
c1_Iter = c1.begin();
for (; c1_Iter != c1.end(); c1_Iter++)
{
cout << " " << *c1_Iter;
}
cout << endl;
}
返回超过末尾迭代器。向量的超过末尾迭代器。 如果向量为空,则 vector::end() == vector::begin()
示例:
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
vector <int> v1;
vector <int>::iterator v1_Iter;
v1.push_back( 1 );
v1.push_back( 2 );
for ( v1_Iter = v1.begin( ) ; v1_Iter != v1.end( ) ; v1_Iter++ )
cout << *v1_Iter << endl;
}
清除向量的元素。
void clear();
示例:
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
vector <int> v1;
v1.push_back( 10 );
v1.push_back( 20 );
v1.push_back( 30 );
cout << "The size of v1 is " << v1.size( ) << endl; //output 3
v1.clear( );
cout << "The size of v1 after clearing is " << v1.size( ) << endl; //output 0
}
测试矢量是否为空。
bool empty() const;
示例
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
vector <int> v1;
v1.push_back( 10 );
if ( v1.empty( ) )
cout << "The vector is empty." << endl;
else
cout << "The vector is not empty." << endl;
}
将一个元素、多个元素或一系列元素插入到指定位置的向量中。
iterator insert(
const_iterator position,
const Type& value);
void insert(
const_iterator position,
size_type count,
const Type& value);
template <class InputIterator>
void insert(
const_iterator position,
InputIterator first,
InputIterator last);
- position: 向量中插入第一个元素的位置
- value: 插入到向量中的元素的值
- count: 插入向量中的元素数目
- first: 要复制的范围元素中的第一个元素的位置
- last: 要复制的元素范围以外的第一个元素的位置
示例:
// vector_insert.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
vector <int> v1;
vector <int>::iterator Iter;
v1.push_back( 10 );
v1.push_back( 20 );
v1.push_back( 30 );
cout << "v1 =" ;
for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
cout << " " << *Iter;
cout << endl;
//output: v1 = 10 20 30
v1.insert( v1.begin( ) + 1, 40 );
cout << "v1 =";
for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
cout << " " << *Iter;
cout << endl;
//output: v1 = 10 40 20 30
v1.insert( v1.begin( ) + 2, 4, 50 );
cout << "v1 =";
for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
cout << " " << *Iter;
cout << endl;
//output: v1 = 10 40 50 50 50 50 20 30
const auto v2 = v1;
v1.insert( v1.begin( )+1, v2.begin( )+2, v2.begin( )+4 );
cout << "v1 =";
for (Iter = v1.begin( ); Iter != v1.end( ); Iter++ )
cout << " " << *Iter;
cout << endl;
//output: v1 = 10 50 50 40 50 50 50 50 20 30
}
返回向量的最大长度。
size_type max_size() const;
示例:
// vector_max_size.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
vector <int> v1;
vector <int>::size_type i;
i = v1.max_size( );
cout << "The maximum possible length of the vector is " << i << "." << endl;
}
返回对指定位置的矢量元素的引用
reference operator[](size_type position);
const_reference operator[](size_type position) const;
示例:
// vector_op_ref.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
vector <int> v1;
v1.push_back( 10 );
v1.push_back( 20 );
int& i = v1[1];
cout << "The second integer of v1 is " << i << endl;
}
用另一个向量的副本替换该向量中的元素。擦除 vector中的任何现有元素后,operator= 将右侧的内容复制或移动到 vector中。
vector& operator=(const vector& right);
vector& operator=(vector&& right);
删除矢量末尾处的元素。
void pop_back();
在矢量末尾处添加一个元素。
void push_back(const T& value);
void push_back(T&& value);
返回向量中的元素数量。
size_type size() const;
示例:
// vector_size.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
vector <int> v1;
vector <int>::size_type i;
v1.push_back( 1 );
i = v1.size( );
cout << "Vector length is " << i << "." << endl; //output: 1
v1.push_back( 2 );
i = v1.size( );
cout << "Vector length is now " << i << "." << endl; //output: 2
}