列表:EnguleFrimeToMe()和列表::C++ STL中的EMPTIONEL()

清单 C++中使用的容器以非连续的方式存储数据,通常,数组和向量本质上是连续的,因此插入和删除操作与列表中的插入和删除选项相比更昂贵。

null
列表::安置前方()

此函数用于将新元素插入列表容器,新元素将添加到列表的开头。 语法:

listname.emplace_front(value)
Parameters :
The element to be inserted into the list
is passed as the parameter.
Result :
The parameter is added to the
list at the beginning.

例如:

Input  : mylist{1, 2, 3, 4, 5};
         mylist.emplace_front(6);
Output : mylist = 6, 1, 2, 3, 4, 5

Input  : mylist{};
         mylist.emplace_front(4);
Output : mylist = 4

错误和异常 1.它有很强的异常保证,因此,如果抛出异常,则不会进行任何更改。 2.参数的类型应与容器的类型相同,否则会引发错误。

// CPP program to illustrate
// Implementation of emplace_front() function
#include <iostream>
#include <list>
using namespace std;
int main()
{
list< int > mylist;
mylist.emplace_front(1);
mylist.emplace_front(2);
mylist.emplace_front(3);
mylist.emplace_front(4);
mylist.emplace_front(5);
mylist.emplace_front(6);
// list becomes 6, 5, 4, 3, 2, 1
// printing the list
for ( auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
return 0;
}


输出:

6 5 4 3 2 1

时间复杂性: O(1)

列表::安置_back()

此函数用于将新元素插入列表容器,新元素将添加到列表的末尾。 语法:

listname.emplace_back(value)
Parameters :
The element to be inserted into the list
is passed as the parameter.
Result :
The parameter is added to the
list at the end.

例如:

Input  : mylist{1, 2, 3, 4, 5};
         mylist.emplace_back(6);
Output : mylist = 1, 2, 3, 4, 5, 6

Input  : mylist{};
         mylist.emplace_back(4);
Output : mylist = 4

错误和异常 1.它有很强的异常保证,因此,如果抛出异常,则不会进行任何更改。 2.参数的类型应与容器的类型相同,否则会引发错误。

// CPP program to illustrate
// Implementation of emplace_back() function
#include <iostream>
#include <list>
using namespace std;
int main()
{
list< int > mylist;
mylist.emplace_back(1);
mylist.emplace_back(2);
mylist.emplace_back(3);
mylist.emplace_back(4);
mylist.emplace_back(5);
mylist.emplace_back(6);
// list becomes 1, 2, 3, 4, 5, 6
// printing the list
for ( auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
return 0;
}


输出:

1 2 3 4 5 6

时间复杂性: O(1)

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享