Namespaces
Variants

std::hive<T,Allocator>::assign

来自 zh.cppreference.net

 
 
 
 
void assign( size_type count, const T& value );
(1) (自 C++26)
template< class InputIt >
void assign( InputIt first, InputIt last );
(2) (自 C++26)
void assign( std::initializer_list<T> ilist );
(3) (自 C++11)

替换容器的内容。

1) 将内容替换为 count 个 value 的副本 value。
2) 将内容替换为范围 [first, last) 中元素的副本。
如果任一参数是指向 *this 的迭代器,则行为未定义。 此重载仅在 InputIt 满足 LegacyInputIterator 时参与重载决议。
3) 将内容替换为来自 ilist 的元素。


参数

count - 容器的新大小
value - 用于初始化容器元素的值
first, last - 定义要复制的元素来源 范围 的迭代器对
ilist - std::initializer_list 从中复制值

复杂度

1) 与 count 成线性。
2) 与 first 和 last 之间的距离成线性。
3) 与 ilist.size() 成线性。

示例

以下代码使用 assign 向一个 std::hive<char> 添加几个字符:

#include <hive>
#include <iostream>
#include <string>

int main()
{
    std::hive<char> characters;
    
    auto print_hive = [&]()
    {
        for (char c : characters)
            std::cout << c << ' ';
        std::cout << '\n';
    };
    
    characters.assign(5, 'a');
    print_hive();
    
    const std::string extra(6, 'b');
    characters.assign(extra.begin(), extra.end());
    print_hive();
    
    characters.assign({'C', '+', '+', '2', '6'});
    print_hive();
}

输出:

a a a a a
b b b b b b
C + + 2 6

参见

向容器赋值一个值范围
(公开成员函数)
向容器赋值
(公开成员函数)