久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

迭代 std::map 的順序是否已知(并由標準保證)?

Is the order of iterating through std::map known (and guaranteed by the standard)?(迭代 std::map 的順序是否已知(并由標準保證)?)
本文介紹了迭代 std::map 的順序是否已知(并由標準保證)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我的意思是 - 我們知道 std::map 的元素是根據鍵排序的.所以,假設鍵是整數.如果我使用 forstd::map::begin() 迭代到 std::map::end(),標準保證我將因此迭代帶有鍵的元素,按升序排序?

What I mean is - we know that the std::map's elements are sorted according to the keys. So, let's say the keys are integers. If I iterate from std::map::begin() to std::map::end() using a for, does the standard guarantee that I'll iterate consequently through the elements with keys, sorted in ascending order?

示例:

std::map<int, int> map_;
map_[1] = 2;
map_[2] = 3;
map_[3] = 4;
for( std::map<int, int>::iterator iter = map_.begin();
     iter != map_.end();
     ++iter )
{
    std::cout << iter->second;
}

這是否保證打印 234 或者它是實現定義的?

Is this guaranteed to print 234 or is it implementation defined?

現實生活中的原因:我有一個帶有 int 鍵的 std::map.在極少數情況下,我想使用大于具體 int 值的鍵遍歷所有元素.是的,聽起來 std::vector 是更好的選擇,但請注意我的非常罕見的情況".

Real life reason: I have a std::map with int keys. In very rare situations, I'd like to iterate through all elements, with key, greater than a concrete int value. Yep, it sounds like std::vector would be the better choice, but notice my "very rare situations".

EDIT:我知道,std::map 的元素已排序......無需指出(對于這里的大多數答案).我什至在我的問題中寫了它.
我在遍歷容器時詢問了迭代器和順序.感謝@Kerrek SB 的回答.

EDIT: I know, that the elements of std::map are sorted.. no need to point it out (for most of the answers here). I even wrote it in my question.
I was asking about the iterators and the order when I'm iterating through a container. Thanks @Kerrek SB for the answer.

推薦答案

是的,這是有保證的.此外,*begin() 為您提供由比較運算符確定的最小元素和 *rbegin() 最大元素,以及兩個鍵值 ab 其中表達式 !compare(a,b) &&!compare(b,a) 為真被認為是相等的.默認的比較函數是std::less.

Yes, that's guaranteed. Moreover, *begin() gives you the smallest and *rbegin() the largest element, as determined by the comparison operator, and two key values a and b for which the expression !compare(a,b) && !compare(b,a) is true are considered equal. The default comparison function is std::less<K>.

排序不是幸運的獎勵功能,而是數據結構的一個基本方面,因為排序用于確定兩個鍵何時相同(通過上述規則)并執行有效的查找(本質上是一種二分查找,其元素數量具有對數復雜性).

The ordering is not a lucky bonus feature, but rather, it is a fundamental aspect of the data structure, as the ordering is used to determine when two keys are the same (by the above rule) and to perform efficient lookup (essentially a binary search, which has logarithmic complexity in the number of elements).

這篇關于迭代 std::map 的順序是否已知(并由標準保證)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 轉換單一顏色)
主站蜘蛛池模板: 日韩av在线免费播放 | 午夜视频在线免费观看 | 天天做夜夜爽 | 国产精品一区二区在线 | 午夜影院在线 | 日本www色 | 俺去俺来也在线www色官网 | 午夜视频免费观看 | 婷婷视频网 | 免费91网站 | 91福利视频导航 | 国产成人一区二区三区 | 欧美精品久久久久 | 亚洲精品字幕 | 久久人人爽人人爽人人片 | 亚洲91av | 风间由美一区二区三区 | 天天色天天干天天 | 日本a网站 | 欧美在线亚洲 | 亚洲视频一区二区三区四区 | 午夜你懂的 | 青草国产 | 男女激情视频网站 | 伊人色播 | 日韩影音| 中文字幕婷婷 | 黄色成人在线 | 中文在线字幕 | 全部免费毛片在线播放高潮 | 亚洲精品久久 | 国产精品欧美一区二区 | 日日夜夜噜 | 色婷婷丁香| 一区二区三区中文字幕 | 国产精品成人国产乱一区 | 中文字幕亚洲综合 | 免费在线小视频 | 三上悠亚激情av一区二区三区 | 欧美一级免费 | 久久精品欧美一区 |