C++,最快的 STL 容器,用于递归执行 {delete[begin]、insert[end] 以及对整个数组内容求和}

发布于 2024-11-29 11:02:47 字数 1055 浏览 1 评论 0 原文

我有一些代码和一个数组,其中每次迭代我都会删除第一个元素,在末尾添加一个元素,然后对数组的内容求和。当然,数组的大小保持不变。我尝试过使用向量和列表,但两者看起来都很慢。

int length = 400;

vector <int> v_int(length, z);
list <int>   l_int(length, z);

for(int q=0; q < x; q++)
{
    int sum =0;

    if(y)                       //if using vector
    {      
        v_int.erase(v_int.begin());    //takes `length` amount of time to shift memory
        v_int.push_back(z);   
        for(int w=0; w < v_int.size(); w++)
            sum += v_int[w];
    }      
    else                        //if using list
    {
        l_int.pop_front();             //constant time
        l_int.push_back(z);
        list<int>::iterator it;
        for ( it=l_int.begin() ; it != l_int.end(); it++ ) //seems to take much  
            sum += *it;                                    //longer than vector does
    }
}

问题在于,擦除向量的第一个元素需要将每个其他元素向下移动,并乘以向量的大小和每次迭代所花费的时间。使用链表可以避免这种情况(恒定时间删除元素),并且不应该牺牲任何对数组求和的时间(数组的线性时间遍历),除了在我的程序中,对内容求和似乎比向量确实如此(至少长1个数量级)。

这里有更好的容器可以使用吗?或解决问题的不同方法?

I have some code and an array where each iteration I delete the first element, add an element at the end, and then sum the contents of the array. Naturally, the array stays the same size. I have tried using both vector and list, but both seem pretty slow.

int length = 400;

vector <int> v_int(length, z);
list <int>   l_int(length, z);

for(int q=0; q < x; q++)
{
    int sum =0;

    if(y)                       //if using vector
    {      
        v_int.erase(v_int.begin());    //takes `length` amount of time to shift memory
        v_int.push_back(z);   
        for(int w=0; w < v_int.size(); w++)
            sum += v_int[w];
    }      
    else                        //if using list
    {
        l_int.pop_front();             //constant time
        l_int.push_back(z);
        list<int>::iterator it;
        for ( it=l_int.begin() ; it != l_int.end(); it++ ) //seems to take much  
            sum += *it;                                    //longer than vector does
    }
}

The problem is that erasing the first element of the vector requires that each other element be shifted down, multiplying, by the size of the vector, the amount of time taken each iteration. Using a linked list avoids this (constant time removal of elements), and should not sacrifice any time summing the array (linear time traversal of the array), except that in my program it seems to be taking way longer to sum the contents than the vector does (at least 1 order of magnitude longer).

Is there a better container to use here? or a different way to approach the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朮生 2024-12-06 11:02:47

为什么不使用 sum -= l_int.front(); 来保存运行总和?总和+=z?

此外,您正在寻找具有删除/插入性能的数据结构是队列

Why not keep a running sum with sum -= l_int.front(); sum += z?

Also the data structure you're looking for with that delete/insert performance is a queue

那些过往 2024-12-06 11:02:47

deque 的目的是在容器中高效地添加和删除结束元素。

如果您只是在一端插入并在另一端删除,那么您可以使用 队列

Efficient additions and deletions of end elements in a container is what the deque was made for.

If you are just inserting at one end and deleting at the other then you can use a queue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文