这个 列表::emplace() 是C++ STL中的一个内置函数,它通过在给定位置插入新元素来扩展列表。
null
语法:
list_name.emplace(position, element)
参数: 该函数接受两个强制参数,如下所述:
- 位置 –它指定迭代器,该迭代器指向列表中要插入新元素的位置。
- 阿格斯 –它指定要插入列表容器中的元素。
返回值: 它返回一个指向新插入元素的随机访问迭代器。
以下程序说明了上述功能:
项目1:
// C++ program to illustrate the // list::emplace() function #include <bits/stdc++.h> using namespace std; int main() { // declaration of list list< int > lis = { 5, 6, 7, 8, 9, 10 }; auto it = lis.emplace(lis.begin(), 2); // inserts at the beginning of the list lis.emplace(it, 1); cout << "List: " ; for ( auto it = lis.begin(); it != lis.end(); ++it) cout << *it << " " ; return 0; } |
输出:
List: 1 2 5 6 7 8 9 10
项目2:
// C++ program to illustrate the // list::emplace() function #include <bits/stdc++.h> using namespace std; int main() { // declaration of list list<pair< int , char > > lis; // inserts at the beginning of the list auto it = lis.emplace(lis.begin(), 4, 'a' ); // inserts at the beginning of the list lis.emplace(it, 3, 'b' ); cout << "List: " ; for ( auto it : lis) cout << "(" << it.first << ", " << it.second << ") " ; return 0; } |
输出:
List: (3, b) (4, a)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END