pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

你如何在一個排序的向量中插入值?

how do you insert the value in a sorted vector?(你如何在一個排序的向量中插入值?)
本文介紹了你如何在一個排序的向量中插入值?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

所有,

這個問題是這個問題的延續.我認為 STL 錯過了這個功能,但它只是我的恕我直言.

This question is a continuation of this one. I think that STL misses this functionality, but it just my IMHO.

現在,問題來了.

考慮以下代碼:

class Foo
{
public:
    Foo();
    int paramA, paramB;
    std::string name;
};

struct Sorter
{
    bool operator()(const Foo &foo1, const Foo &foo2) const
    {
         switch( paramSorter )
         {
             case 1:
                 return foo1.paramA < foo2.paramA;
             case 2:
                 return foo1.paramB < foo2.paramB;
             default:
                 return foo1.name < foo2.name;
         }
    }

    int paramSorter;
};

int main()
{
    std::vector<Foo> foo;
    Sorter sorter;
    sorter.paramSorter = 0;
        // fill the vector
    std::sort( foo.begin(), foo.end(), sorter );
}

在任何給定的時刻,向量都可以重新排序.該類還有用于排序器結構的 getter 方法.

At any given moment of time the vector can be re-sorted. The class also have the getter methods which are used in the sorter structure.

在向量中插入新元素的最有效方法是什么?

What would be the most efficient way to insert a new element in the vector?

我的情況是:

我有一個網格(電子表格),它使用類的排序向量.在任何給定的時間,向量都可以重新排序,網格將相應地顯示排序后的數據.

I have a grid (spreadsheet), that uses the sorted vector of a class. At any given time the vector can be re-sorted and the grid will display the sorted data accordingly.

現在我需要在向量/網格中插入一個新元素.我可以插入,然后重新排序,然后重新顯示整個網格,但這效率非常低,尤其是對于大網格.

Now I will need to insert a new element in the vector/grid. I can insert, then re-sort and then re-display the whole grid, but this is very inefficient especially for the big grid.

任何幫助將不勝感激.

推薦答案

問題的簡單回答:

template< typename T >
typename std::vector<T>::iterator 
   insert_sorted( std::vector<T> & vec, T const& item )
{
    return vec.insert
        ( 
            std::upper_bound( vec.begin(), vec.end(), item ),
            item 
        );
}

帶有謂詞的版本.

template< typename T, typename Pred >
typename std::vector<T>::iterator
    insert_sorted( std::vector<T> & vec, T const& item, Pred pred )
{
    return vec.insert
        ( 
           std::upper_bound( vec.begin(), vec.end(), item, pred ),
           item 
        );
}

其中 Pred 是類型 T 上的嚴格排序謂詞.

Where Pred is a strictly-ordered predicate on type T.

為了使其工作,輸入向量必須已經在這個謂詞上排序.

For this to work the input vector must already be sorted on this predicate.

這樣做的復雜性是 O(log N) 對于 upper_bound 搜索(找到插入的位置)但高達 O(N) 用于插入本身.

The complexity of doing this is O(log N) for the upper_bound search (finding where to insert) but up to O(N) for the insert itself.

為了更好的復雜性,您可以使用 std::set 如果不會有任何重復或 std::multiset 如果有可能是重復的.這些將自動為您保留排序順序,您也可以對這些指定您自己的謂詞.

For a better complexity you could use std::set<T> if there are not going to be any duplicates or std::multiset<T> if there may be duplicates. These will retain a sorted order for you automatically and you can specify your own predicate on these too.

您還可以做其他各種更復雜的事情,例如管理一個 vector 和一個 set/multiset/sorted vector 新添加的項目,然后將它們合并足夠了.任何類型的遍歷您的集合都需要遍歷兩個集合.

There are various other things you could do which are more complex, e.g. manage a vector and a set / multiset / sorted vector of newly added items then merge these in when there are enough of them. Any kind of iterating through your collection will need to run through both collections.

使用第二個向量的優點是可以保持數據緊湊.這里你的新添加"項目 vector 將相對較小,因此插入時間將是 O(M) 其中 M 是這個的大小vector 并且可能比每次插入大向量的 O(N) 更可行.合并將是 O(N+M) 這比 O(NM) 更好,它會一次插入一個,所以總的來說它是 O(N+M) + O(M2) 插入 M 個元素然后合并.

Using a second vector has the advantage of keeping your data compact. Here your "newly added" items vector will be relatively small so the insertion time will be O(M) where M is the size of this vector and might be more feasible than the O(N) of inserting in the big vector every time. The merge would be O(N+M) which is better than O(NM) it would be inserting one at a time, so in total it would be O(N+M) + O(M2) to insert M elements then merge.

您可能也將插入向量保持在其容量,以便隨著您的增長而不會進行任何重新分配,而只是移動元素.

You would probably keep the insertion vector at its capacity too, so as you grow that you will not be doing any reallocations, just moving of elements.

這篇關于你如何在一個排序的向量中插入值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 北京康百特科技有限公司-分子蒸馏-短程分子蒸馏设备-实验室分子蒸馏设备 | 定制异形重型钢格栅板/钢格板_定做踏步板/排水沟盖板_钢格栅板批发厂家-河北圣墨金属制品有限公司 | 诸城网站建设-网络推广-网站优化-阿里巴巴托管-诸城恒泰互联 | LINK FASHION 童装·青少年装展| 博博会2021_中国博物馆及相关产品与技术博览会【博博会】 | 臭氧老化试验箱,高低温试验箱,恒温恒湿试验箱,防水试验设备-苏州亚诺天下仪器有限公司 | 酒吧霸屏软件_酒吧霸屏系统,酒吧微上墙,夜场霸屏软件,酒吧点歌软件,酒吧互动游戏,酒吧大屏幕软件系统下载 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | 电动高压冲洗车_价格-江苏速利达机车有限公司 | 综合管廊模具_生态,阶梯护坡模具_检查井模具制造-致宏模具厂家 | 搜活动房网—活动房_集装箱活动房_集成房屋_活动房屋 | SDG吸附剂,SDG酸气吸附剂,干式酸性气体吸收剂生产厂家,超过20年生产使用经验。 - 富莱尔环保设备公司(原名天津市武清县环保设备厂) | 反渗透水处理设备|工业零排放|水厂设备|软化水设备|海南净水设备--海南水处理设备厂家 | 皮带机_移动皮带机_大倾角皮带机_皮带机厂家 - 新乡市国盛机械设备有限公司 | 玻璃钢罐_玻璃钢储罐_盐酸罐厂家-河北华盛节能设备有限公司 | 电伴热系统施工_仪表电伴热保温箱厂家_沃安电伴热管缆工业技术(济南)有限公司 | 厦门ISO认证|厦门ISO9001认证|厦门ISO14001认证|厦门ISO45001认证-艾索咨询专注ISO认证行业 | 瓶盖扭矩仪(扭力值检测)-百科| 金蝶帐无忧|云代账软件|智能财税软件|会计代账公司专用软件 | 电机修理_二手电机专家-河北豫通机电设备有限公司(原石家庄冀华高压电机维修中心) | 广东西屋电气有限公司-广东西屋电气有限公司 | 山东led显示屏,山东led全彩显示屏,山东LED小间距屏,临沂全彩电子屏-山东亚泰视讯传媒有限公司 | 天津力值检测-天津管道检测-天津天诚工程检测技术有限公司 | 注浆压力变送器-高温熔体传感器-矿用压力传感器|ZHYQ朝辉 | 【北京写字楼出租_写字楼租赁_办公室出租网/出售】-远行地产官网 | 闪电优家-卫生间防水补漏_酒店漏水渗水维修_防水堵漏公司 | 恒温恒湿试验箱_高低温试验箱_恒温恒湿箱-东莞市高天试验设备有限公司 | 【官网】博莱特空压机,永磁变频空压机,螺杆空压机-欧能优 | 大白菜官网,大白菜winpe,大白菜U盘装系统, u盘启动盘制作工具 | 智能门锁电机_智能门锁离合器_智能门锁电机厂家-温州劲力智能科技有限公司 | 光照全温振荡器(智能型)-恒隆仪器 | 北京发电机出租_发电机租赁_北京发电机维修 - 河北腾伦发电机出租 | 山东信蓝建设有限公司官网 | 矿用履带式平板车|探水钻机|气动架柱式钻机|架柱式液压回转钻机|履带式钻机-启睿探水钻机厂家 | 全屋整木定制-橱柜,家具定制-四川峨眉山龙马木业有限公司 | 辊道窑炉,辊道窑炉厂家-山东艾希尔| 石家庄律师_石家庄刑事辩护律师_石家庄取保候审-河北万垚律师事务所 | 旋转/数显粘度计-运动粘度测定仪-上海平轩科学仪器 | GAST/BRIWATEC/CINCINNATI/KARL-KLEIN/ZIEHL-ABEGG风机|亚喜科技 | 震动筛选机|震动分筛机|筛粉机|振筛机|振荡筛-振动筛分设备专业生产厂家高服机械 |