STL中的轉發列表實現單鏈接列表。從C++ 11引入的前向列表比其他容器有用,可以進行插入,刪除和移動操作(如排序),並允許時間常數地插入和刪除元素。它與列表的不同之處在於前向列表跟蹤list僅保留下一個元素的位置,同時跟蹤下一個和上一個元素。
forward_list::emplace_front()
此函數用於將新元素插入到轉發列表容器中,並將新元素添加到轉發列表的開頭。
用法:
forwardlistname.emplace_front(value) 參數: The element to be inserted into the forward list is passed as the parameter. Result: The parameter is added to the forward list at the beginning.
例子:
Input :myflist{1, 2, 3, 4, 5}; myflist.emplace_front(6); Output:myflist = 6, 1, 2, 3, 4, 5 Input :myflist{}; myflist.emplace_front(4); Output:myflist = 4
錯誤和異常
1.它具有強大的異常保證,因此,如果引發異常,則不會進行任何更改。
2.參數的類型應與容器的類型相同,否則將引發錯誤。
// INTEGER FORWARD LIST EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <forward_list>
#include <iostream>
using namespace std;
int main() {
forward_list<int> myflist;
myflist.emplace_front(1);
myflist.emplace_front(2);
myflist.emplace_front(3);
myflist.emplace_front(4);
myflist.emplace_front(5);
myflist.emplace_front(6);
// forward list becomes 6, 5, 4, 3, 2, 1
// printing the forward list
for (auto it = myflist.begin(); it != myflist.end(); ++it)
cout << ' ' << *it;
return 0;
}
輸出:
6 5 4 3 2 1
// STRING FORWARD LIST EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <forward_list>
#include <iostream>
#include <string>
using namespace std;
int main() {
forward_list<string> myflist;
myflist.emplace_front("Geeksforgeeks");
myflist.emplace_front("is");
myflist.emplace_front("This");
// forward list becomes This, is, Geeksforgeeks
// printing the forward list
for (auto it = myflist.begin(); it != myflist.end(); ++it)
cout << ' ' << *it;
return 0;
}
輸出:
This is Geeksforgeeks
// CHARACTER FORWARD LIST EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <forward_list>
#include <iostream>
using namespace std;
int main() {
forward_list<char> myflist;
myflist.emplace_front('z');
myflist.emplace_front('y');
myflist.emplace_front('x');
myflist.emplace_front('b');
myflist.emplace_front('a');
// forward list becomes a, b, x, y, z
// printing the forward list
for (auto it = myflist.begin(); it != myflist.end(); ++it)
cout << ' ' << *it;
return 0;
}
輸出:
a b x y z
時間複雜度:O(1)
應用:使用emplace_front()函數輸入具有以下編號和順序的空的轉發列表,並對給定的轉發列表進行排序。
Input: 7, 89, 45, 6, 24, 58, 43 Output:6, 7, 24, 43, 45, 58, 89
// CPP program to illustrate
// application Of emplace_front() function
#include <forward_list>
#include <iostream>
using namespace std;
int main() {
forward_list<int> myforwardlist{};
myforwardlist.emplace_front(43);
myforwardlist.emplace_front(58);
myforwardlist.emplace_front(24);
myforwardlist.emplace_front(6);
myforwardlist.emplace_front(45);
myforwardlist.emplace_front(89);
myforwardlist.emplace_front(7);
// Forward list becomes
// 7, 89, 45, 6, 24, 58, 43
// Sorting function
myforwardlist.sort();
for (auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it)
cout << ' ' << *it;
}
輸出量
6 7 24 43 45 58 89
相關用法
注:本文由純淨天空篩選整理自AyushSaxena大神的英文原創作品 forward_list::emplace_front() in C++STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。