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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。