C++ Logo

std-proposals

Advanced search

[std-proposals] New function for std::vector

From: Фаррах Фаттахов <farrakhfattahov_at_[hidden]>
Date: Sat, 28 Dec 2024 00:57:05 +0300
After working with SFML, I so hated std::vector (I was a bad programmer
back then and didn't know about the reserve() function, which led to not
understanding why std::vector is so slow, while a simple array performs
much better). As a result, in my new project i created my own version of
std::vector - FE2D::dynamic_array. And there, from the beginning i created
one useful function - reset()
```C++

// Reset the Size of Dynamic Array to Zero, but it's don't Touches the
Real Occupied Memory of Dynamic Array
void reset() { mSize = 0; }
```

Instead of clear() it's just setting mSize to zero. It's very useful in the
situations like

```C++
void Render() { // It's called every frame
// Add a sprites to the FE2D::dynamic_array to draw it later
m_Renderer.AddSprite(m_Sprite_0);
m_Renderer.AddSprite(m_Sprite_1);
m_Renderer.AddSprite(m_Sprite_2);
...
// Draw the Sprites and reset the dynamic arrays to prepare it for the
next frame
m_Renderer.RenderSprites();
}
```
Here reset() works like clear(), but much faster

In the std::vector or std::deque is no functions like reset() and i want to
add it there, especially sometimes i get errors because my
FE2D::dynamic_array is not so good like std::vector and i have to use it
instead of mine, but there is no function i need. I think reset() will be
nice thing for std::vector.

Received on 2024-12-27 21:57:18