|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
vector容器提供了 insert() 和 emplace() 这 2 个成员函数
6 O ]! j f6 L& D8 [! {
; Z7 U7 f$ R1 x+ [: y2 B X1 ^# W- ~' W. c
insert()6 j' _, ~+ x" C6 I
; B, Q: C! d+ t/ W; M
; M, M3 p4 n* minsert() 函数的功能是在 vector 容器的指定位置插入一个或多个元素。该函数的语法格式有多种。
& ]. u0 y5 j# S. f% ~% I6 h) M3 c* E S( a
8 c. {/ N% c5 ^: Y0 ]: m7 witerator insert(pos,elem) - _4 d4 }2 @* C: t
在迭代器 pos 指定的位置之前插入一个新元素elem,并返回表示新插入元素位置的迭代器。
6 r. P0 `, j8 ^: a& s7 biterator insert(pos,n,elem) 5 c w. c6 ^' C0 s: f
在迭代器 pos 指定的位置之前插入 n 个元素 elem,并返回表示第一个新插入元素位置的迭代器。, W& M, Q2 }1 P) @
iterator insert(pos,first,last) 5 P4 R3 K4 O- D$ \' c" Y3 A
在迭代器 pos 指定的位置之前,插入其他容器(不仅限于vector)中位于 [first,last) 区域的所有元素,并返回表示第一个新插入元素位置的迭代器。
$ A3 l+ Y4 C# Aiterator insert(pos,initlist) & j+ _4 {* m9 w% n1 }7 U3 ~
在迭代器 pos 指定的位置之前,插入初始化列表(用大括号{}括起来的多个元素,中间有逗号隔开)中所有的元素,并返回表示第一个新插入元素位置的迭代器。
9 n' Y" o+ m, `. P$ {7 }1 S
" a$ b- R/ Z s/ L6 e, q9 ]8 k
; D0 z5 h: [+ ?3 D+ J+ X下面的例子,演示了如何使用 insert() 函数向 vector 容器中插入元素。8 D+ S% w9 Y- w0 t$ T0 M% b) Y
( o! U: P. ?# |, \7 k7 d! Y
4 U' S) J/ z: Y8 I8 @$ A#include <iostream>
# a' ?/ e3 e8 w& w; T6 r! }% M#include <vector>
* @+ a/ _" d l2 r) Z#include <array>
3 P) w1 N3 B' L9 }8 H9 C& |using namespace std;
; ^- L& w! x5 F+ z, P, aint main()7 M: I( A4 A$ l4 h/ _( ~* @7 g
{; u; V5 H5 K+ \$ F- R/ H, y3 \" `
std::vector<int> demo{1,2};3 B5 K7 z" Y# D8 O+ w
//第一种格式用法! l* s0 L/ X1 o7 t
demo.insert(demo.begin() + 1, 3);//{1,3,2}1 T$ N$ D" n; x l* G( {) m
//第二种格式用法
2 r4 p+ S5 ~' U m demo.insert(demo.end(), 2, 5);//{1,3,2,5,5}) B) v. @1 c5 ^0 S: s7 c8 X1 f( D
//第三种格式用法# o$ {# }8 z1 _ X
std::array<int,3>test{ 7,8,9 };
0 L! y; t- ]9 k& L& j- S demo.insert(demo.end(), test.begin(), test.end());//{1,3,2,5,5,7,8,9}
( H* d# C5 S& E$ v* L) _. ^4 R- j //第四种格式用法+ Y; J2 b- T- k
demo.insert(demo.end(), { 10,11 });//{1,3,2,5,5,7,8,9,10,11}
0 T7 ^8 {* C: ]0 [ 8 Y3 ~* m" i Z8 A p
return 0; g1 c" e) o0 H4 \% U; W# S. l
}
8 X6 w! ?) m5 n- V) R5 @9 P: e
+ P: p. ]8 O/ e0 T8 r F4 l! o5 q9 U4 U' {0 N
emplace()) Z% D s8 {2 r# M& s
emplace() 是 C++ 11 标准新增加的成员函数,用于在 vector 容器指定位置之前插入一个新的元素。/ h1 c$ j+ a3 @( ]7 R3 r
$ d( B1 g9 x5 {( R( q7 U' p1 {# V. u
3 c o j. L* F$ z该函数的语法格式如下:
7 ?3 c& ~' ~( siterator emplace (const_iterator pos, args...);6 Z3 d; n( Z. o, w' b# Z( c
2 V# y. H" ~) t" A8 q$ `5 I1 e# }# K
6 M* h5 R) j) ]3 g& y4 b! p5 M
其中,pos 为指定插入位置的迭代器;args... 表示与新插入元素的构造函数相对应的多个参数;该函数会返回表示新插入元素位置的迭代器。
: Z! E0 F4 y5 g8 s9 Q1 g/ R简单的理解 args...,即被插入元素的构造函数需要多少个参数,那么在 emplace() 的第一个参数的后面,就需要传入相应数量的参数。
0 D$ H# q1 b5 R: b1 N4 ]! r#include <vector>
$ `* J* ?6 |" \+ \2 [; Y* M# @#include <iostream>( ~+ c. @1 o) t' `/ m
using namespace std;! |5 N* U# d( k' x. H- s
int main()
3 ~% \" l* b8 I! W{8 V/ x& z) u" L
std::vector<int> demo1{1,2};2 ?% F- r$ N& I t- q- ]
//emplace() 每次只能插入一个 int 类型元素( M5 N* T! h( Y4 j; q
demo1.emplace(demo1.begin(), 3);
0 Z T! J3 r& T8 { return 0;7 Y7 d5 S* N: ^8 f
}* M0 x7 K! t3 x" }- w. t9 v
8 o; o. ^* \1 H! I- @
) L% p% y% n4 ^
结果 3 1 2
' \3 K: g" g$ l
, v* J# D& \- T/ ~. Z
3 y, E( D- K+ ~8 } |
|