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

QVector 與 QList

QVector vs QList(QVector 與 QList)
本文介紹了QVector 與 QList的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個需要迭代的整數列表,但數組不夠用.vectorslists 之間有什么區別,在選擇類型之前有什么我需要知道的嗎?

I have a list of integers that I need to iterate over but an array is inadequate. What are the differences between vectors and lists and is there anything I need to know before I pick a type?

為了清楚起見,我已經閱讀了 QT 文檔,但這是我所知道的范圍:

Just to be clear, I've read the QT docs but this is the extent of what I know:

QListQLinkedListQVector 提供類似的功能.概覽如下:

QList<T>, QLinkedList<T>, and QVector<T> provide similar functionality. Here's an overview:

  • 在大多數情況下,QList 是適合使用的類.它的基于索引的 API 比基于 QLinkedList 的 迭代器的 API 更方便,并且通常比 QVector 更快,因為它將其項存儲在內存中的方式.它還可以擴展到您的可執行文件中更少的代碼.
  • 如果您需要一個真正的鏈表,并保證在列表中間插入恒定時間并使用迭代器而不是索引,請使用 QLinkedList.
  • 如果您希望項目占據相鄰的內存位置,請使用 QVector.
  • For most purposes, QList is the right class to use. Its index-based API is more convenient than QLinkedList's iterator-based API, and it is usually faster than QVector because of the way it stores its items in memory. It also expands to less code in your executable.
  • If you need a real linked list, with guarantees of constant time insertions in the middle of the list and iterators to items rather than indexes, use QLinkedList.
  • If you want the items to occupy adjacent memory positions, use QVector.

推薦答案

QVector 大部分類似于 std::vector,您可能會從名稱中猜到.QList 更接近于 boost::ptr_deque,盡管與 std::list 有明顯的關聯.它不直接存儲對象,而是存儲指向它們的指針.您可以獲得兩端快速插入的所有好處,并且重新分配涉及改組指針而不是復制構造函數,但失去了實際 std::dequestd::vector<的空間局部性/code>,并獲得大量的堆分配.它確實有一些決策來避免小對象的堆分配,重新獲得空間局部性,但據我所知,它只適用于小于 int 的東西.

QVector is mostly analogous to std::vector, as you might guess from the name. QList is closer to boost::ptr_deque, despite the apparent association with std::list. It does not store objects directly, but instead stores pointers to them. You gain all the benefits of quick insertions at both ends, and reallocations involve shuffling pointers instead of copy constructors, but lose the spacial locality of an actual std::deque or std::vector, and gain a lot of heap allocations. It does have some decision making to avoid the heap allocations for small objects, regaining the spacial locality, but from what I understand it only applies to things smaller than an int.

QLinkedList 類似于 std::list,并且具有它的所有缺點.一般來說,這應該是你最后選擇的容器.

QLinkedList is analogous to std::list, and has all the downsides of it. Generally speaking, this should be your last choice of a container.

QT 庫非常喜歡使用 QList 對象,因此在您自己的代碼中使用它們有時可以避免一些不必要的乏味.在某些情況下,額外的堆使用和實際數據的隨機定位在理論上可能會造成傷害,但通常不會引起注意.所以我建議使用 QList 直到分析建議更改為 QVector.如果您希望連續分配很重要 [閱讀:您正在與需要 T[] 而不是 QList] 的代碼交互,這也可能是一個原因立即開始使用 QVector.

The QT library heavily favors the use of QList objects, so favoring them in your own code can sometimes avoid some unneccessary tedium. The extra heap use and the random positioning of the actual data can theoretically hurt in some circumstances, but oftentimes is unnoticable. So I would suggest using QList until profiling suggests changing to a QVector. If you expect contiguous allocation to be important [read: you are interfacing with code that expects a T[] instead of a QList<T>] that can also be a reason to start off with QVector right off the bat.

如果你問的是一般的容器,只是參考了 QT 文檔,那么上面的信息用處不大.

If you are asking about containers in general, and just used the QT documents as a reference, then the above information is less useful.

std::vector 是一個可以調整大小的數組.所有元素都彼此相鄰存儲,您可以快速訪問單個元素.缺點是插入僅在一端有效.如果在中間或開頭放了一些東西,則必須復制其他對象以騰出空間.在 big-oh 符號中,最后插入是 O(1),其他地方插入是 O(N),隨機訪問是 O(1).

An std::vector is an array that you can resize. All the elements are stored next to each other, and you can access individual elements quickly. The downside is that insertions are only efficient at one end. If you put something in the middle, or at the beginning, you have to copy the other objects to make room. In big-oh notation, insertion at the end is O(1), insertion anywhere else is O(N), and random access is O(1).

An std::deque 類似,但不保證對象彼此相鄰存儲,并且允許兩端插入為 O(1).它還需要一次分配較小的內存塊,這有時很重要.隨機訪問是 O(1),中間插入是 O(N),與 vector 相同.空間局部性比 std::vector 差,但對象往往是聚類的,因此您可以獲得一些好處.

An std::deque is similar, but does not guarentee objects are stored next to each other, and allows insertion at both ends to be O(1). It also requires smaller chunks of memory to be allocated at a time, which can sometimes be important. Random access is O(1) and insertion in the middle is O(N), same as for a vector. Spacial locality is worse than std::vector, but objects tend to be clustered so you gain some benefits.

std::list 是一個鏈表.在三個標準順序容器中,它需要的內存開銷最大,但可以在任何地方快速插入……前提是您提前知道需要插入的位置.它不提供對單個元素的隨機訪問,因此您必須在 O(N) 中進行迭代.但是一旦到達那里,實際的插入是 O(1).std::list 的最大好處是您可以快速將它們拼接在一起……如果您將整個范圍的值移動到不同的 std::list,整個操作是 O(1).使列表中的引用無效也更加困難,這有時很重要.

An std::list is a linked list. It requires the most memory overhead of the three standard sequential containers, but offers fast insertion anywhere... provided you know in advance where you need to insert. It does not offer random access to individual elements, so you have to iterate in O(N). But once there, the actual insertion is O(1). The biggest benefit to std::list is that you can splice them together quickly... if you move an entire range of values to a different std::list, the entire operation is O(1). It is also much harder to invalidate references into the list, which can sometimes be important.

作為一般規則,我更喜歡 std::dequestd::vector,除非我需要能夠將數據傳遞給需要原始數組.std::vector 保證是連續的,因此 &v[0] 用于此目的.我不記得上次使用 std::list 是什么時候,但幾乎可以肯定是因為我需要更強的保證引用保持有效.

As a general rule, I prefer std::deque to std::vector, unless I need to be able to pass the data to a library that expects a raw array. std::vector is guaranteed contiguous, so &v[0] works for this purpose. I don't remember the last time I used a std::list, but it was almost certainly because I needed the stronger guaretee about references remaining valid.

這篇關于QVector 與 QList的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 北京开源多邦科技发展有限公司官网 | 多物理场仿真软件_电磁仿真软件_EDA多物理场仿真软件 - 裕兴木兰 | 油冷式_微型_TDY电动滚筒_外装_外置式电动滚筒厂家-淄博秉泓机械有限公司 | AGV叉车|无人叉车|AGV智能叉车|AGV搬运车-江西丹巴赫机器人股份有限公司 | 幂简集成 - 品种超全的API接口平台, 一站搜索、试用、集成国内外API接口 | 结晶点测定仪-润滑脂滴点测定仪-大连煜烁 | 济南保安公司加盟挂靠-亮剑国际安保服务集团总部-山东保安公司|济南保安培训学校 | 钢衬四氟管道_钢衬四氟直管_聚四氟乙烯衬里管件_聚四氟乙烯衬里管道-沧州汇霖管道科技有限公司 | 东莞喷砂机-喷砂机-喷砂机配件-喷砂器材-喷砂加工-东莞市协帆喷砂机械设备有限公司 | 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | 顺辉瓷砖-大国品牌-中国顺辉 | PCB接线端子_栅板式端子_线路板连接器_端子排生产厂家-置恒电气 喷码机,激光喷码打码机,鸡蛋打码机,手持打码机,自动喷码机,一物一码防伪溯源-恒欣瑞达有限公司 假肢-假肢价格-假肢厂家-河南假肢-郑州市力康假肢矫形器有限公司 | 振动筛,震动筛,圆形振动筛,振动筛价格,振动筛厂家-新乡巨宝机电 蒸汽热收缩机_蒸汽发生器_塑封机_包膜机_封切收缩机_热收缩包装机_真空机_全自动打包机_捆扎机_封箱机-东莞市中堡智能科技有限公司 | 无硅导热垫片-碳纤维导热垫片-导热相变材料厂家-东莞市盛元新材料科技有限公司 | 防伪溯源|防窜货|微信二维码营销|兆信_行业内领先的防伪防窜货数字化营销解决方案供应商 | 网架支座@球铰支座@钢结构支座@成品支座厂家@万向滑动支座_桥兴工程橡胶有限公司 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 大倾角皮带机-皮带输送机-螺旋输送机-矿用皮带输送机价格厂家-河南坤威机械 | 模具硅橡胶,人体硅胶,移印硅胶浆厂家-宏图硅胶科技 | 培训无忧网-教育培训咨询招生第三方平台 | 无锡门窗-系统门窗-阳光房-封阳台-断桥铝门窗厂[窗致美] | 厦门ISO认证|厦门ISO9001认证|厦门ISO14001认证|厦门ISO45001认证-艾索咨询专注ISO认证行业 | 深圳美安可自动化设备有限公司,喷码机,定制喷码机,二维码喷码机,深圳喷码机,纸箱喷码机,东莞喷码机 UV喷码机,日期喷码机,鸡蛋喷码机,管芯喷码机,管内壁喷码机,喷码机厂家 | 电缆接头_防水接头_电缆防水接头_防水电缆接头_上海闵彬 | 灰板纸、灰底白、硬纸板等纸品生产商-金泊纸业 | 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 | 板框压滤机-隔膜压滤机-厢式压滤机生产厂家-禹州市君工机械设备有限公司 | 真空泵厂家_真空泵机组_水环泵_旋片泵_罗茨泵_耐腐蚀防爆_中德制泵 | 航空障碍灯_高中低光强航空障碍灯_民航许可认证航空警示灯厂家-东莞市天翔航天科技有限公司 | 云南标线|昆明划线|道路标线|交通标线-就选云南云路施工公司-云南云路科技有限公司 | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 临海涌泉蜜桔官网|涌泉蜜桔微商批发代理|涌泉蜜桔供应链|涌泉蜜桔一件代发 | 舞台木地板厂家_体育运动木地板_室内篮球馆木地板_实木运动地板厂家_欧氏篮球地板推荐 | 伊卡洛斯软装首页-电动窗帘,别墅窗帘,定制窗帘,江浙沪1000+别墅窗帘案例 | 步入式高低温测试箱|海向仪器 | 环讯传媒,永康网络公司,永康网站建设,永康小程序开发制作,永康网站制作,武义网页设计,金华地区网站SEO优化推广 - 永康市环讯电子商务有限公司 | 水厂污泥地磅|污泥处理地磅厂家|地磅无人值守称重系统升级改造|地磅自动称重系统维修-河南成辉电子科技有限公司 | 精密模具制造,注塑加工,吹塑和吹瓶加工,EPS泡沫包装生产 - 济南兴田塑胶有限公司 | 滚筒烘干机_转筒烘干机_滚筒干燥机_转筒干燥机_回转烘干机_回转干燥机-设备生产厂家 | 福兰德PVC地板|PVC塑胶地板|PVC运动地板|PVC商用地板-中国弹性地板系统专业解决方案领先供应商! 福建成考网-福建成人高考网 | 广州冷却塔维修厂家_冷却塔修理_凉水塔风机电机填料抢修-广东康明节能空调有限公司 |