欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vector容器insert()函数的三种使用方式

程序员文章站 2022-03-22 21:22:35
...

                                         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;
}