介绍 (Introduction)
In this tutorial, we are going to learn about vector insert() in C++. As well as look at how it works and can be used to accomplish the insertion operation in different ways with examples.
在本教程中,我们将学习C ++中的vector insert() 。 以及示例,以不同的方式看一下它是如何工作的以及可用于完成插入操作。
C ++中的vector :: insert()函数 (The vector::insert() function in C++)
Basically, the vector::insert()
function from the STL in C++ is used to insert elements or values into a vector container. In general, the function returns an iterator pointing to the first of the inserted elements.
基本上,C ++中STL的vector::insert()
函数用于将元素或值插入向量容器中。 通常,该函数返回一个迭代器,该迭代器指向插入的元素中的第一个。
在向量上使用insert()函数 (Using the insert() Function on Vectors)
The insert()
method can be used to insert single or multiple elements into a given vector in different ways, for different cases. We can insert a single value at our desired position, we can even insert multiple values into the vector at once, and even we can insert a bunch of values from another vector to it.
对于不同的情况,可以使用insert()
方法以不同的方式将单个或多个元素插入给定向量。 我们可以在期望的位置插入一个值,甚至可以一次将多个值插入向量,甚至可以从另一个向量中插入一堆值。
So, let us see how we can do that with ease.
因此,让我们看看如何轻松地做到这一点。
1.将一个值插入向量 (1. Insert a single value into a Vector)
We can directly pass an iterator pointing to our desired position and the value to be inserted there to the insert()
function to modify a vector.
我们可以直接将指向我们所需位置的迭代器以及要在此处插入的值传递给insert()
函数,以修改向量。
Look carefully at the example below, here we try to insert a value 10 at the beginning of the vector.
仔细查看下面的示例,在这里我们尝试在向量的开头插入值10。
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> vec {1,2,3,4,5};
cout<<"Intially vector: ";
for(auto i=vec.begin(); i<vec.end(); i++)
{
cout<<" "<<*i;
}
vec.insert(vec.begin(),10);//Inserting 10 to the vector
cout<<"\n\nThe modified vector is: ";
for(auto i=vec.begin(); i<vec.end(); i++)
{
cout<<" "<<*i;
}
return 0;
}
Output;
输出;
Intially vector: 1 2 3 4 5
The modified vector is: 10 1 2 3 4 5
Here,
这里,
- Firstly we initialize a vector,
vec
. And print the same, 首先,我们初始化向量vec
。 并打印相同 - Then we call the insert() function on the vector
vec
with parametersvec.begin()
and 10(new value). Note, herevec.begin()
returns an iterator pointing to the start of the vector, 然后,我们在向量vec
使用参数vec.begin()
和10 (新值)调用insert()函数。 请注意,此处vec.begin()
返回指向向量开头的迭代器, - After the insertion has been done we print the new vector using a simple for loop to see the resultant vector. 插入完成后,我们使用简单的for循环打印新矢量,以查看生成的矢量。
2.多次插入相同的值 (2. Insert the same value Multiple times)
We can also insert multiple values to a vector at once using the insert()
function. This can be done by passing an iterator pointing to our starting position where we want to insert, the number of times the value is going to repeat, and at last the value.
我们还可以使用insert()
函数一次将多个值插入向量。 这可以通过传递一个迭代器来实现,该迭代器指向我们要插入的起始位置,该值将要重复的次数以及最后一个值。
The example below illustrates the use properly.
下面的示例正确说明了用法。
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> vec {10,20,30,40};
cout<<"Intially vector: ";
for(auto i=vec.begin(); i<vec.end(); i++)
{
cout<<" "<<*i;
}
vec.insert(vec.end(),3,100);//Inserting 100, 3 times to the vector
cout<<"\n\nThe modified vector is: ";
for(auto i=vec.begin(); i<vec.end(); i++)
{
cout<<" "<<*i;
}
return 0;
}
Output;
输出;
Intially vector: 10 20 30 40
The modified vector is: 10 20 30 40 100 100 100
Here,
这里,
- We initialize our vector
vec
and print the same, 我们初始化向量vec
并打印出来, - Then we pass an iterator pointing to the end of the vector, as returned by
vec.end()
, 3(the number of times we want the value to repeat), and the value 100 to the insert() function. 然后,我们将一个迭代器指向vec.end()
返回的向量的结尾,该迭代器返回3 (我们希望该值重复的次数),并将值100传递给insert()函数。 - In this way, as we can observe from the output, 100 is inserted thrice at the end of the vector,
vec
. 这样,正如我们从输出中看到的,在向量vec
的末尾插入了100三次。
3.插入另一个向量 (3. Insert Another Vector)
Further, we can also insert elements of another vector to our old vector. Just we need to pass an iterator pointing to the position in our vector where we need to insert another vector. Along with that, the iterators pointing to the starting and end of the second vector.
此外,我们还可以将另一个向量的元素插入旧向量。 只是我们需要传递一个指向向量中需要插入另一个向量的位置的迭代器。 除此之外,迭代器指向第二个向量的开始和结束。
Let us take a small example to understand the working.
让我们举一个小例子来了解其工作原理。
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> vec {2,4,6,8};
vector<int> vec2 {1,3,5,7};
cout<<"Intially first vector: ";
for(auto i=vec.begin(); i<vec.end(); i++)
{
cout<<" "<<*i;
}
cout<<"\nIntially second vector: ";
for(auto i=vec2.begin(); i<vec2.end(); i++)
{
cout<<" "<<*i;
}
//Inserting vec2 at the beginning of the vec vector
vec.insert(vec.begin(),vec2.begin(),vec2.end());
cout<<"\n\nThe modified vector is: ";
for(auto i=vec.begin(); i<vec.end(); i++)
{
cout<<" "<<*i;
}
return 0;
}
Output;
输出;
Intially first vector: 2 4 6 8
Intially second vector: 1 3 5 7
The modified vector is: 1 3 5 7 2 4 6 8
Here, vec
and vec2
are two vectors. Out of which vec2 is the one whose elements we need to insert into the vector, vec. We call the insert()
function with appropriate parameters as mentioned earlier. This modifies our vector vec
, resulting in the insertion of the second vector elements at the beginning.
在这里, vec
和vec2
是两个向量。 我们需要从哪个vec2中插入元素vec 。 如前所述,我们使用适当的参数调用insert()
函数。 这会修改向量vec
,从而在开始处插入第二个向量元素。
结论 (Conclusion)
So, in this tutorial, we explained the working as well as the use of the vector insert() function from the STL in C++. For better understanding, we recommend trying the above code snippets yourselves. And for any questions, feel free to comment below.
因此,在本教程中,我们说明了C ++中STL的vector insert()函数的工作方式和用法。 为了更好地理解,我们建议您尝试以上代码片段。 对于任何问题,请随时在下面评论。
参考资料 (References)
- Replace an element into a specific position of a vector – Stack Overflow Question, 将元素替换为向量的特定位置 –堆栈溢出问题,
- How to insert an element into the beginning of a vector? – Stack Overflow Question, 如何将元素插入向量的开头? –堆栈溢出问题,
- Vectors in C++ – JournalDev Tutorial. C ++中的向量 – JournalDev教程。
翻译自: https://www.journaldev.com/37622/vector-insert-in-c-plus-plus