Namespaces
Variants

std::list<T,Allocator>:: push_back

From cppreference.net

void push_back ( const T & value ) ;
(1) (自 C++26 起为 constexpr)
void push_back ( T && value ) ;
(2) (自 C++11 起)
(自 C++26 起为 constexpr)

value 的副本追加到容器末尾。

不会使任何迭代器或引用失效。

目录

参数

value - 要追加的元素值

类型要求
-
若满足以下条件,则行为未定义:
1) T 不可 复制插入 list
2) T 不可 移动插入 list
(自 C++11 起)

复杂度

常量。

异常

如果抛出异常(可能由于 Allocator::allocate() 或元素复制/移动构造函数/赋值操作导致),此函数不产生任何效果( 强异常安全保证 )。

示例

#include <iomanip>
#include <iostream>
#include <string>
#include <list>
int main()
{
    std::list<std::string> letters;
    letters.push_back("abc");
    std::string s{"def"};
    letters.push_back(std::move(s));
    std::cout << "std::list letters holds: ";
    for (auto&& e : letters)
        std::cout << std::quoted(e) << ' ';
    std::cout << "\nMoved-from string s holds: " << std::quoted(s) << '\n';
}

可能的输出:

std::list letters holds: "abc" "def"
Moved-from string s holds: ""

参见

在容器末尾原位构造元素
(公开成员函数)
在容器开头插入元素
(公开成员函数)
移除末尾元素
(公开成员函数)
创建根据实参类型推导的 std::back_insert_iterator
(函数模板)