std::vector(续)
继续上篇博文.........
- Capacity(容量/元素个数)
-
size
- 返回容器中元素个数 (public member function )
-
max_size
- 返回该容器最多能存储的元素个数 (public member function )
-
resize
- 该表容器大小 (public member function )
-
capacity
- 返回已经为容器分配的内存的容量 (public member function )
-
empty
- 判别容器是否为空 (public member function )
-
reserve
- 重新设定容器内存的容量 (public member function )
-
shrink_to_fit
- Shrink to fit (public member function )
- resize
void resize (size_type n, value_type val = value_type());
参数 n: 代表新设置的容器可包含的元素数量
参数 val : 容器在变大的过程中,默认填充的数据
注意:
在修改容器大小过程中根据传入的n的不同分为如下三种处理情况:
(1)当要设置的n比当前的容器的数据元素个数小时,容器将会保存前n个元素,其他的元素将会被移除
(2)当要设置的n比当前的容器的数据元素个数大时,容器会用val指定的值去填充新增加的未赋值的元素,如 果没有指定val值,容器会使用默认值进行填充。
(3) 当要设置的n比当前的容器的已分配的内存大时,容器就会重新分配内存。
// resizing vector
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector;
// set some initial content:
for (int i=1;i<10;i++) myvector.push_back(i);
myvector.resize(5);
myvector.resize(8,100);
myvector.resize(12);
std::cout << "myvector contains:";
for (int i=0;i<myvector.size();i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
return 0;
}
Output: myvector contains: 1 2 3 4 5 100 100 100 0 0 0 0 |
- reserve
void reserve (size_type n);
注意:
当n比当前的容器容量大时,容器从新分配内存,当n比当前容器容量小时,不进行操作
// vector::reserve
#include <iostream>
#include <vector>
int main ()
{
std::vector<int>::size_type sz;
std::vector<int> foo;
sz = foo.capacity();
std::cout << "making foo grow:\n";
for (int i=0; i<100; ++i) {
foo.push_back(i);
if (sz!=foo.capacity()) {
sz = foo.capacity();
std::cout << "capacity changed: " << sz << '\n';
}
}
std::vector<int> bar;
sz = bar.capacity();
bar.reserve(100); // this is the only difference with foo above
std::cout << "making bar grow:\n";
for (int i=0; i<100; ++i) {
bar.push_back(i);
if (sz!=bar.capacity()) {
sz = bar.capacity();
std::cout << "capacity changed: " << sz << '\n';
}
}
return 0;
}
Possible output:
making foo grow:
capacity changed: 1
capacity changed: 2
capacity changed: 4
capacity changed: 8
capacity changed: 16
capacity changed: 32
capacity changed: 64
capacity changed: 128
making bar grow:
capacity changed: 100 |
- Element access(元素访问)
-
operator[]
- 下标访问元素 (public member function )
-
at
- 访问元素(public member function )
-
front
- 访问第一个元素 (public member function )
-
back
- 访问第二个元素(public member function )
-
data
- Access data (public member function )
// vector::at
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector (10); // 10 zero-initialized ints
// assign some values:
for (unsigned i=0; i<myvector.size(); i++)
myvector.at(i)=i;
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); i++)
std::cout << ' ' << myvector.at(i);
std::cout << '\n';
return 0;
}
Output:
myvector contains: 0 1 2 3 4 5 6 7 8 9 |
- Modifiers(修改元素)
-
assign
- 重新分配容器中内容(public member function )
-
push_back
- 在vector尾部加入元素 (public member function )
-
pop_back
- 在vector尾部删除元素 (public member function )
-
insert
- 在指定位置插入元素(public member function )
-
erase
- 删除指定位置元素 (public member function )
-
swap
- 交换两个容器中的元素 (public member function )
-
clear
- 清空容器中的元素 (public member function )
-
emplace
- Construct and insert element (public member function )
-
emplace_back
- Construct and insert element at the end (public member function )
示例如下:
- assign
// vector assign
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> first;
std::vector<int> second;
std::vector<int> third;
first.assign (7,100); // 7 ints with a value of 100
std::vector<int>::iterator it;
it=first.begin()+1;
second.assign (it,first.end()-1); // the 5 central values of first
int myints[] = {1776,7,4};
third.assign (myints,myints+3); // assigning from array.
std::cout << "Size of first: " << int (first.size()) << '\n';
std::cout << "Size of second: " << int (second.size()) << '\n';
std::cout << "Size of third: " << int (third.size()) << '\n';
return 0;
}
Output:
Size of first: 7 Size of second: 5 Size of third: 3 |
- insert(插入元素)
(1)单个元素
iterator insert (iterator position, const value_type& val);
void insert (iterator position, size_type n, const value_type& val);
(3)多个元素
template <class InputIterator>
void insert (iterator position, InputIterator first, InputIterator last);
// inserting into a vector
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector (3,100);
std::vector<int>::iterator it;
it = myvector.begin();
it = myvector.insert ( it , 200 );
myvector.insert (it,2,300);
// "it" no longer valid, get a new one:
it = myvector.begin();
std::vector<int> anothervector (2,400);
myvector.insert (it+2,anothervector.begin(),anothervector.end());
int myarray [] = { 501,502,503 };
myvector.insert (myvector.begin(), myarray, myarray+3);
std::cout << "myvector contains:";
for (it=myvector.begin(); it<myvector.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
Output:
myvector contains: 501 502 503 300 300 400 400 200 100 100 100 |
- erase(删除元素)
(1)删除一个元素
iterator erase (iterator position);
(2)删除一个区间元素
iterator erase (iterator first, iterator last);
// erasing from vector
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector;
// set some values (from 1 to 10)
for (int i=1; i<=10; i++) myvector.push_back(i);
// erase the 6th element
myvector.erase (myvector.begin()+5);
// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); ++i)
std::cout << ' ' << myvector[i];
std::cout << '\n';
return 0;
}
Output:
myvector contains: 4 5 7 8 9 10 |
- swap(交换两个vector元素)
// swap vectors
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> foo (3,100); // three ints with a value of 100
std::vector<int> bar (5,200); // five ints with a value of 200
foo.swap(bar);
std::cout << "foo contains:";
for (unsigned i=0; i<foo.size(); i++)
std::cout << ' ' << foo[i];
std::cout << '\n';
std::cout << "bar contains:";
for (unsigned i=0; i<bar.size(); i++)
std::cout << ' ' << bar[i];
std::cout << '\n';
return 0;
}
Output:
foo contains: 200 200 200 200 200 bar contains: 100 100 100 |