最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - How to handle elements addingremoving in structure of arrays - Stack Overflow

programmeradmin1浏览0评论

I want to make safe and easy adding and removing elements from structure of arrays. Is there a way in modern C++ to do so without writing functions and make these operations explicitly on every array? Separate functions do their work, but with increasing number of arrays code duplication (long list of arrays) becomes more visible and error-prone.

Something like (which, of course, doesn't compile demo; I don't provide error log, since this is just code to show the idea, not code which should work as is)

#include <iostream>
#include <ranges>
#include <vector>

int main()
{
    std::vector<float> value;
    std::vector<float> threshold;

    auto SoA = std::views::zip(value, threshold);

    std::fill_n(std::inserter(SoA, SoA.begin()), 1, { 0.0f, 0.0f } );

    std::erase(SoA.end());
}

would work for me.

Here I mean that I want to zip somehow vectors in one place and in many other places just use one safe call to change them. Real importance of this will be noticeable when there are many vectors which build the structure and many places where you need add/remove elements.

Useful sources

I found two related implementations:

  • Structure of Arrays
  • vapid soa

They are comprehensive and well-developed, but the question still on the table if this is possible to implement easier (taking into account my simpler requirements).

发布评论

评论列表(0)

  1. 暂无评论