std::vector::assign
STD::向量::分配
void assign( size_type count, const T& value | (1) | |
---|---|---|
template< class InputIt > void assign( InputIt first, InputIt last | (2) | |
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直线距离first
和last
3%29线性ilist.size()
例
下面的代码使用assign若要将多个字符添加到std::vector<char>*
二次
#include <vector>
#include <iostream>
int main()
{
std::vector<char> characters;
characters.assign(5, 'a'
for (char c : characters) {
std::cout << c << '\n';
}
return 0;
}
二次
产出:
二次
a
a
a
a
a
二次
另见
(constructor) | constructs the vector (public member function) |
---|
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。