首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::forward_list::assign

void assign( size_type count, const T& value );

(1)

(since C++11)

template< class InputIt > void assign( InputIt first, InputIt last );

(2)

(since C++11)

void assign( std::initializer_list<T> ilist );

(3)

(since C++11)

替换容器的内容。

1%29将内容替换为count价值副本value

2%29将内容替换为范围内内容的副本。[first, last)...

This overload has the same effect as overload (1) if InputIt is an integral type.

(until C++11)

This overload only participates in overload resolution if InputIt satisfies InputIterator.

(since C++11)

3%29用初始化程序列表中的元素替换内容。ilist...

所有迭代器、指针和对容器元素的引用都无效。

参数

count

-

the new size of the container

value

-

the value to initialize elements of the container with

first, last

-

the range to copy the elements from

ilist

-

initializer list to copy the values from

复杂性

1%29线性count

2%29直线距离firstlast

3%29线性ilist.size()

下面的代码使用assign若要将多个字符添加到std::forward_list<char>*

二次

代码语言:javascript
复制
#include <forward_list>
#include <iostream>
 
int main()
{
    std::forward_list<char> characters;
 
    characters.assign(5, 'a');
 
    for (char c : characters) {
        std::cout << c << '\n';
    } 
 
    return 0;
}

二次

产出:

二次

代码语言:javascript
复制
a
a
a
a
a

二次

另见

(constructor)

constructs the forward_list (public member function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券