vector容器insert()函數的三種使用方式

                                         insert() 

iterator insert ( iterator position, const T& x );
    void insert ( iterator position, size_type n, const T& x );
template <class InputIterator>
    void insert ( iterator position, InputIterator first, InputIterator last );

 

Insert elements

The vector is extended by inserting new elements before the element at position.

This effectively increases the vector size, which causes an automatic reallocation of the allocated storage space if, and only if, the new vector size surpases the current vector capacity. Reallocations in vector containers invalidate all previously obtained iterators, references and pointers.

Because vectors keep an array format, insertions on positions other than the vector end are performed by moving all the elements between position and the end of the vector to their new positions, and then inserting the new element(s), which may not be a method as efficient as the insertion in other kinds of sequence containers (deque, list).

The parameters determine how many elements are inserted and to which values they are initialized:

 

Parameters

position

Position in the vector where the new elements are inserted.
iterator is a member type, defined as a random access iterator type.

x

Value to be used to initialize the inserted elements.
T is the first template parameter (the type of the elements stored in the vector).

n

Number of elements to insert. Each element is initialized to the value specified in x.
Member type size_type is an unsigned integral type.

first, last

Iterators specifying a range of elements. Copies of the elements in the range [first,last) are inserted at position position.
Notice that the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
The template type can be any type of input iterator.

 

Return value

Only the first version returns a value, which is an iterator that points to the newly inserted element.

If reallocations happen, they are performed using Allocator::allocate(), which may throw exceptions (for the default allocator, bad_alloc is thrown if the allocation request does not succeed).

 元素:

通過在元素之前插入新的元素來擴展向量。

這有效地增加了向量大小,當且僅當新向量大小超過當前向量容量時,就會自動重新分配分配的存儲空間。向量容器中的重新分配會使之前獲得的所有迭代器、引用和指針無效。

因爲向量數組格式、插入以外的位置向量執行結束位置之間移動的所有元素和向量的結束他們的新位置,然後插入新元素(s),這可能不是一個方法一樣有效的其他類型的序列中插入容器(雙端隊列、列表)。

這些參數決定了插入了多少元素,並將它們初始化爲哪些值:

參數:

位置

在插入新元素的向量中的位置。

iterator是成員類型,定義爲隨機訪問迭代器類型。

x

值,用於初始化插入的元素。

T是第一個模板參數(存儲在向量中的元素的類型)。

n

要插入的元素數量。每個元素都初始化爲x中指定的值。

成員類型size_type是無符號整數類型。

開始,最後

指定元素範圍的迭代器。範圍[first,last]中的元素的副本插入到position位置。

注意,範圍包括first和last之間的所有元素,包括first指向的元素,但不包括last指向的元素。

模板類型可以是任何類型的輸入迭代器。

返回值:

只有第一個版本返回一個值,它是一個迭代器,指向新插入的元素。

舉例:

#include <iostream>
#include <vector>
using namespace std;
int main()
{
	vector<int> myvector(3, 100);/* 3個100 */
	vector<int>::iterator it;
	for (it = myvector.begin(); it != myvector.end(); it++)
	{
		/* 迭代器是一個指針,但它的++或者--跳過的字節數不同,根據用戶聲明的數據類型
		大小來自增*/
		cout << *it << ' ';
	}
	cout << endl;
	it = myvector.begin();
	it = myvector.insert(it, 200);/* 指向新插入的元素的位置 */

	myvector.insert(it, 2, 300);
	//
	it = myvector.begin();/* 容器最開始的位置 */
	//myvector.insert(it, 250);
	
	vector<int> anothervector(2, 400);
	myvector.insert(it + 2, anothervector.begin(), anothervector.end());
	/* 向後移動2個位置 */
    int myarray[] = { 501,502,503 };
	myvector.insert(myvector.begin(), myarray, myarray + 3);

	cout << "myvector contains:";
	for (it = myvector.begin(); it < myvector.end(); it++)
		cout << " " << *it;
	cout << endl;
	system("pause"); 
	return 0;
}
 


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章