vector::emplace
将就地构造的元素插入到指定位置的向量中。
iterator emplace( const_iterator _Where, Type&& _Val );
参数
参数 |
描述 |
_Where |
vector 类 中插入第一个元素的位置。 |
_Val |
插入到 vector 中的元素的值。 |
返回值
该函数将返回一个指向 vector 中新元素的插入位置的迭代器。
备注
任何插入操作都可能产生巨额费用,请参阅 vector 类,了解有关 vector 性能的讨论。
示例
// vector_emplace.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
vector <int> v1;
vector <int>::iterator Iter;
v1.push_back( 10 );
v1.push_back( 20 );
v1.push_back( 30 );
cout << "v1 =" ;
for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
cout << " " << *Iter;
cout << endl;
// initialize a vector of vectors by moving v1
vector < vector <int> > vv1;
vv1.emplace( vv1.begin(), move( v1 ) );
if ( vv1.size( ) != 0 && vv1[0].size( ) != 0 )
{
cout << "vv1[0] =";
for (Iter = vv1[0].begin( ); Iter != vv1[0].end( ); Iter++ )
cout << " " << *Iter;
cout << endl;
}
}
要求
标头:<vector>
命名空间: std