Namespaces
Variants

std::forward_list<T,Allocator>:: emplace_after

From cppreference.net
template < class ... Args >
iterator emplace_after ( const_iterator pos, Args && ... args ) ;
(C++11부터)
(constexpr C++26부터)

지정된 위치 다음에 새로운 요소를 컨테이너에 삽입합니다. 요소는 제자리에서 생성되며, 복사나 이동 연산이 수행되지 않습니다. 요소의 생성자는 함수에 제공된 인자와 정확히 동일한 인자로 호출됩니다.

다음 조건 중 하나라도 충족되면, 동작은 정의되지 않습니다:

반복자나 참조가 무효화되지 않습니다.

목차

매개변수

pos - 새 요소가 생성될 위치의 뒤를 가리키는 반복자
args - 요소의 생성자로 전달될 인자들

반환값

새로운 요소에 대한 반복자.

복잡도

상수.

예외

어떤 이유로든 예외가 발생하면, 이 함수는 아무런 효과를 가지지 않습니다( strong exception safety guarantee ).

예제

이 예제는 단일 연결 리스트를 역순이 아닌 자연스러운 순서로 채우는 표준적인 방법을 보여줍니다.

#include <forward_list>
#include <iostream>
#include <string>
struct Sum
{
    std::string remark;
    int sum;
    Sum(std::string remark, int sum)
        : remark{std::move(remark)}, sum{sum} {}
    void print() const
    {
        std::cout << remark << " = " << sum << '\n';
    }
};
int main()
{
    std::forward_list<Sum> list;
    auto iter = list.before_begin();
    std::string str{"1"};
    for (int i{1}, sum{1}; i != 10; sum += i)
    {
        iter = list.emplace_after(iter, str, sum);
        ++i;
        str += " + " + std::to_string(i);
    }
    for (const Sum& s : list)
        s.print();
}

출력:

1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15
1 + 2 + 3 + 4 + 5 + 6 = 21
1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

참고 항목

요소 뒤에 요소들을 삽입합니다
(public member function)