I was wondering what the STL forward_list implementation for assign would be. I tried searching for the source code online, but the forward_list doesn't seem to be anywhere.
This is what I envisioned the definition to look like:
// Replaces the contents with count copies of value value
template<typename T, typename A>
void forward_list<T, A>::assign(size_type count, const T& value)
{
if(count_ <= count)
{
for(auto&& element : *this)
{
element = value;
}
auto rem = count - count_;
for(size_t i = 0; i < rem; ++i)
{
push_back(value);
}
}
else
{
auto itr = begin();
for(size_t i = 0; i < count; ++i)
{
(*itr)++ = value;
}
auto rem = count_ - count;
for(size_t i = 0; i < rem; ++i)
{
pop_back();
}
}
}
Aucun commentaire:
Enregistrer un commentaire