std::vector::insert() 是 C++ STL 中的內置函數,用於在指定位置的元素之前插入新元素,從而按插入元素的數量有效增加容器大小。
時間複雜度 -線性,O(N)
插入函數被重載以處理多種情況,如下所示:
- 在給定索引處插入一個元素。
- 多次插入一個元素。
- 在給定索引處插入一係列元素。
1. 在給定索引處插入元素
Vector 中 insert() 的語法
vector_name.insert (position, val);
參數
該函數接受下麵指定的兩個參數:
- 位置-它指定指向要完成插入的位置的迭代器。
- 值-它指定要插入的值。
向量中 insert() 的示例
C++
// C++ program to illustrate the function of
// vector_name.insert(position,val)
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initialising the vector
vector<int> vector_name{ 1, 2, 3, 4, 5 };
// Printing out the original vector
cout << "Original vector :\n";
for (auto x : vector_name)
cout << x << " ";
cout << "\n";
// Inserting the value 100 at position 3(0-based
// indexing) in the vector
vector_name.insert(vector_name.begin() + 3, 100);
// Printing the modified vector
cout << "Vector after inserting 100 at position 3 :\n";
for (auto x : vector_name)
cout << x << " ";
cout << "\n";
// Inserting the value 500 at position 1(0-based
// indexing) in the vector
vector_name.insert(vector_name.begin() + 1, 500);
// Printing the modified vector
cout << "Vector after inserting 500 at position 1 :\n";
for (auto x : vector_name)
cout << x << " ";
return 0;
}
// This code is contributed by Abhijeet Kumar(abhijeet19403)
輸出
Original vector : 1 2 3 4 5 Vector after inserting 100 at position 3 : 1 2 3 100 4 5 Vector after inserting 500 at position 1 : 1 500 2 3 100 4 5
2. 在給定索引處插入多個元素
Vector 中 insert() 的語法
vector_name.insert(position, size, val)
參數
該函數接受如下指定的三個參數:
- 位置-它指定指向要完成插入的位置的迭代器。
- 尺寸-它指定在指定位置插入 val 的次數。
- 值-它指定要插入的值。
向量中 insert() 的示例
C++
// C++ program to illustrate the function of
// vector_name.insert(position,size,val)
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initialising the vector
vector<int> vector_name{ 1, 2, 3, 4, 5 };
// Printing out the original vector
cout << "Original vector :\n";
for (auto x : vector_name)
cout << x << " ";
cout << endl;
// Inserting the value 100, 4 times starting at position
// 3
vector_name.insert(vector_name.begin() + 3, 4, 100);
// Printing the modified vector
cout << "Vector after inserting 100, 4 times, starting "
"at position 3 :\n";
for (auto x : vector_name)
cout << x << " ";
return 0;
}
// This code contributed by Harsh Singh (hsnooob)
輸出
Original vector : 1 2 3 4 5 Vector after inserting 100, 4 times, starting at position 3 : 1 2 3 100 100 100 100 4 5
3. 在給定索引處插入元素範圍
矢量insert()的語法
vector_name.insert(position, iterator1, iterator2)
參數
該函數接受下麵指定的三個參數:
- 位置-它指定要在向量中完成插入的位置。
- 迭代器1-它指定要插入元素的起始位置
- 迭代器2-它指定要插入元素的結束位置
矢量示例insert()
C++
// C++ program to illustrate the function of
// vector_name.insert(position,itr1,itr2)
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initialising the vector
vector<int> original{ 1, 2, 3, 4, 5 };
vector<int> temp{ 2, 5, 9, 0, 3, 10 };
// Printing out the original vector
cout << "Original vector :\n";
for (auto x : original)
cout << x << " ";
cout << endl;
// Inserting the portion of temp vector in original
// vector temp.begin()+3 is starting iterator of vector
// to be copied temp.begin()+5 is ending iterator of
// vector to be copied
original.insert(original.begin() + 3, temp.begin() + 2,
temp.begin() + 5);
// Printing the modified vector
cout << "Vector after Inserting the portion of temp "
"vector in original vector :\n";
for (auto x : original)
cout << x << " ";
return 0;
}
// This code contributed by Harsh Singh (hsnooob)
輸出
Original vector : 1 2 3 4 5 Vector after Inserting the portion of temp vector in original vector : 1 2 3 9 0 3 4 5
相關用法
- C++ vector insert()用法及代碼示例
- C++ vector capacity()用法及代碼示例
- C++ vector data()用法及代碼示例
- C++ vector emplace()用法及代碼示例
- C++ vector shrink_to_fit()用法及代碼示例
- C++ vector max_size()用法及代碼示例
- C++ vector rbegin()、rend()用法及代碼示例
- C++ vector erase()、clear()用法及代碼示例
- C++ vector::at()用法及代碼示例
- C++ vector::back()用法及代碼示例
- C++ vector::begin()用法及代碼示例
- C++ vector::capacity()用法及代碼示例
- C++ vector::cbegin()用法及代碼示例
- C++ vector::cend()用法及代碼示例
- C++ vector::clear()用法及代碼示例
- C++ vector::crbegin()用法及代碼示例
- C++ vector::crend()用法及代碼示例
- C++ vector::data()用法及代碼示例
- C++ vector::empty()用法及代碼示例
- C++ vector::end()用法及代碼示例
- C++ vector::erase()用法及代碼示例
- C++ vector::front()用法及代碼示例
- C++ vector::insert()用法及代碼示例
- C++ vector::max_size()用法及代碼示例
- C++ vector::operator[]用法及代碼示例
注:本文由純淨天空篩選整理自Twinkl Bajaj大神的英文原創作品 vector insert() Function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。