// // //template<class Key, //class Ty, //class Hash = std::hash<Key>, //class Pred = std::equal_to<Key>, //class Alloc = std::allocator<Key> > //class unordered_multimap; //對象通過調(diào)用兩個存儲對象,即一個 unordered_multimap::key_equal 類型的比較函數(shù)對象和一個 unordered_multimap::hasher 類型的哈希函數(shù)對象,對它控制的序列進(jìn)行排序。 //可以通過調(diào)用成員函數(shù) unordered_multimap::key_eq() 訪問第一個存儲對象;通過調(diào)用成員函數(shù) unordered_multimap::hash_function() 訪問第二個存儲對象。 //具體而言,對于所有 Key 類型的值 X 和 Y,key_eq()(X, Y) 調(diào)用將僅在兩個參數(shù)值擁有等效順序時返回 true;hash_function()(keyval) 調(diào)用將生成 size_t 類型的值的分布。 與模板類 unordered_map 類 不同,模板類 unordered_multimap 的對象不確保 key_eq()(X, Y) 對于受控序列的任意兩個元素始終為 false。(鍵不需要唯一。) // //此對象還存儲最大加載因子,用于指定每個存儲桶的元素的最大所需平均數(shù)量。 如果插入元素導(dǎo)致 unordered_multimap::load_factor() 超出最大加載因子,容器將增加存儲桶的數(shù)量并根據(jù)需要重新生成哈希表。 // //受控序列中元素的實際順序取決于哈希函數(shù)、比較函數(shù)、插入順序、最大加載因子和存儲桶的當(dāng)前數(shù)量。 通常無法預(yù)測受控序列中的元素順序。 但是,可以始終確保具有等效順序的任何元素子集在受控序列中相鄰。 // //對象通過 unordered_multimap::allocator_type 類型的存儲分配器對象為其控制的序列分配并釋放存儲。 此分配器對象必須與 allocator 模板類的對象的外部接口相同。 請注意,已分配容器對象時,不復(fù)制存儲的分配器對象。 // //此模板類描述用于控制std::pair<const Key, Ty>類型的變長元素序列的對象。序列由哈希函數(shù)弱排序,哈希函數(shù)將此序列分區(qū)到稱為存儲桶的有序序列集中。在每個存儲桶中,比較函數(shù)將確定任一元素對是否具有等效順序。每個元素存儲兩個對象,包括一個排序鍵和一個值。序列以允許查找、插入和移除任意元素的方式表示,并包含與序列中的元素數(shù)量無關(guān)的多個操作(常量時間),至少在所有存儲桶長度大致相等時如此。在最壞情況下,當(dāng)所有元素位于一個存儲桶中時,操作數(shù)量與序列中的元素數(shù)量成比例(線性時間)。此外,插入元素不會使迭代器失效,移除元素僅會使指向已移除元素的迭代器失效。 // // // //1.unordered_multimap::allocator_type 用于管理存儲的分配器的類型。 // //typedef Alloc allocator_type; //備注//-------------------------------------------------------------------------------- // 類型是一個Alloc模板參數(shù)分配的同義詞。 //示例//-------------------------------------------------------------------------------- // // #include <unordered_map> #include <iostream> typedef std::unordered_multimap<char, int> Mymap; typedef std::allocator<std::pair<const char, int> > Myalloc; int main() { Mymap c1; Mymap::allocator_type al = c1.get_allocator(); std::cout << "al == std::allocator() is " << std::boolalpha << (al == Myalloc()) << std::endl; return (0); } //al == std::allocator() 為 true //2,unordered_multimap::const_iterator受控序列的常量迭代器的類型。 // //typedef T1 const_iterator; //此類型描述為可用作受控序列的常量向前迭代器的對象。 在此處描述為實現(xiàn)定義的類型 T1 的同義詞。 //#include <unordered_map> //#include <iostream> // // typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // return (0); //} // ////unordered_multimap::const_local_iterator受控序列的常量存儲桶迭代器的類型。 ////typedef T5 const_local_iterator; // ////備注//-------------------------------------------------------------------------------- //// ////描述類型可以作為常數(shù)的前向迭代數(shù)。對于存儲桶的對象。 //// 該類型描述了一個對象,該對象可以作為一個常數(shù)向前的一個桶。這里描述為實現(xiàn)定義型T5的同義詞。 ////示例//-------------------------------------------------------------------------------- // // // //#include <unordered_map> //#include <iostream> // // typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // inspect bucket containing 'a' // Mymap::const_local_iterator lit = c1.begin(c1.bucket('a')); // std::cout << " [" << lit->first << ", " << lit->second << "]"; // // return (0); //} //// std_tr1__unordered_map__unordered_multimap_const_local_iterator.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // inspect bucket containing 'a' // Mymap::const_local_iterator lit = c1.begin(c1.bucket('a')); // std::cout << " [" << lit->first << ", " << lit->second << "]"; // // return (0); //} // // // ////unordered_multimap::const_pointer元素的常量指針的類型。 ////typedef Alloc::const_pointer const_pointer;該類型將可作為常量指針的對象描述為受控序列中的元素。 // //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::iterator it = c1.begin(); // it != c1.end(); ++it) // { // Mymap::const_pointer p = &*it; // std::cout << " [" << p->first << ", " << p->second << "]"; // } // std::cout << std::endl; // // return (0); //} // // ////unordered_multimap::const_reference元素的常量引用的類型。 ////typedef Alloc::const_reference const_reference;類型描述可以充當(dāng)對控制序列元素的常量引用的對象。 //// std_tr1__unordered_map__unordered_multimap_const_reference.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::iterator it = c1.begin(); // it != c1.end(); ++it) // { // Mymap::const_reference ref = *it; // std::cout << " [" << ref.first << ", " << ref.second << "]"; // } // std::cout << std::endl; // // return (0); //} // ////unordered_multimap::difference_type兩個元素間的帶符號距離的類型。 ////帶符號整數(shù)類型描述可以表示任何兩個元素間的差異。地址控件序列的對象。 它介紹此處,實現(xiàn)定義類型 T3的同義詞。 //// std_tr1__unordered_map__unordered_multimap_difference_type.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // compute positive difference // Mymap::difference_type diff = 0; // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // ++diff; // std::cout << "end()-begin() == " << diff << std::endl; // // // compute negative difference // diff = 0; // for (Mymap::const_iterator it = c1.end(); // it != c1.begin(); --it) // --diff; // std::cout << "begin()-end() == " << diff << std::endl; // // return (0); //} // ////unordered_multimap::hasher哈希函數(shù)的類型。 ////typedef Hash hasher; //// std_tr1__unordered_map__unordered_multimap_hasher.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // Mymap::hasher hfn = c1.hash_function(); // std::cout << "hfn('a') == " << hfn('a') << std::endl; // std::cout << "hfn('b') == " << hfn('b') << std::endl; // // return (0); //} ////unordered_multimap::iterator受控序列的迭代器的類型。 ////typedef T0 iterator; ////類型描述了一個對象,該對象可以作為控制序列的前向迭代器。這里描述為實現(xiàn)定義的類型的T0的同義詞。 //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // return (0); //} ////unordered_multimap::key_equal比較函數(shù)的類型。 ////類型是Pred模板參數(shù)的同義詞。 //// std_tr1__unordered_map__unordered_multimap_key_equal.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // Mymap::key_equal cmpfn = c1.key_eq(); // std::cout << "cmpfn('a', 'a') == " // << std::boolalpha << cmpfn('a', 'a') << std::endl; // std::cout << "cmpfn('a', 'b') == " // << std::boolalpha << cmpfn('a', 'b') << std::endl; // // return (0); //} // ////unordered_multimap::key_type排序鍵的類型。 ////typedef Key key_type;類型是Key模板參數(shù)的同義詞。Key模板參數(shù) //// std_tr1__unordered_map__unordered_multimap_key_type.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // add a value and reinspect // Mymap::key_type key = 'd'; // Mymap::mapped_type mapped = 4; // Mymap::value_type val = Mymap::value_type(key, mapped); // c1.insert(val); // // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // return (0); //} // // ////unordered_multimap::local_iterator受控序列的存儲桶迭代器的類型。 ////typedef T4 local_iterator; ////類型描述了一個可以作為一個向前的一個迭代器的對象。這里描述為實現(xiàn)定義型T4的同義詞。 //// std_tr1__unordered_map__unordered_multimap_local_iterator.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // inspect bucket containing 'a' // Mymap::local_iterator lit = c1.begin(c1.bucket('a')); // std::cout << " [" << lit->first << ", " << lit->second << "]"; // // return (0); //} ////unordered_multimap::mapped_type與每個鍵關(guān)聯(lián)的映射值的類型。 ////typedef Ty mapped_type;類型是Ty模板參數(shù)的同義詞。 //// std_tr1__unordered_map__unordered_multimap_mapped_type.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // add a value and reinspect // Mymap::key_type key = 'd'; // Mymap::mapped_type mapped = 4; // Mymap::value_type val = Mymap::value_type(key, mapped); // c1.insert(val); // // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // return (0); //} // ////unordered_multimap::pointer元素的指針的類型。 ////typedef Alloc::pointer pointer;類型來描述可用作指向控制序列的元素的對象。 //// std_tr1__unordered_map__unordered_multimap_pointer.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::iterator it = c1.begin(); // it != c1.end(); ++it) // { // Mymap::pointer p = &*it; // std::cout << " [" << p->first << ", " << p->second << "]"; // } // std::cout << std::endl; // // return (0); //} // ////unordered_multimap::reference元素的引用的類型。 ////typedef Alloc::reference reference;類型描述可以充當(dāng)對控制序列的元素的引用的對象。 //// std_tr1__unordered_map__unordered_multimap_reference.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::iterator it = c1.begin(); // it != c1.end(); ++it) // { // Mymap::reference ref = *it; // std::cout << " [" << ref.first << ", " << ref.second << "]"; // } // std::cout << std::endl; // // return (0); //} ////unordered_multimap::size_type兩個元素間的無符號距離的類型。 ////typedef T2 size_type; 無符號整數(shù)類型描述一個可以表示任意控制序列長度的對象。這里描述為實現(xiàn)定義的T2型的同義詞。 //// std_tr1__unordered_map__unordered_multimap_size_type.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // Mymap::size_type sz = c1.size(); // // std::cout << "size == " << sz << std::endl; // // return (0); //} ////unordered_multimap::value_type元素的類型。 ////typedef std::pair<const Key, Ty> value_type;描述控制類型序列的元素。 ////// std_tr1__unordered_map__unordered_multimap_value_type.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // add a value and reinspect // Mymap::key_type key = 'd'; // Mymap::mapped_type mapped = 4; // Mymap::value_type val = Mymap::value_type(key, mapped); // c1.insert(val); // // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // return (0); //} ////成員函數(shù)說明 //// ////unordered_multimap::begin 指定控制序列或存儲桶的開頭。 ////iterator begin(); ////const_iterator begin() const; ////local_iterator begin(size_type nbucket);//nbucket 存儲數(shù)字 ////const_local_iterator begin(size_type nbucket) const; ////前兩點成員函數(shù)返回序列中的第一個元素的前向迭代器(或受一個空序列。結(jié)束)。 最后兩個成員函數(shù)返回指向在存儲桶 nbucket 的第一個元素用于向前迭代器(或受空的存儲桶。結(jié)束)。 ////前兩個成員函數(shù)返回一個前向迭代器,該迭代器指向序列的第一個元素(或在空序列的末端)。最后兩個成員函數(shù)返回前向迭代器在存儲桶nbucket第一元點(或只是超出一個空桶底)。 //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // inspect first two items " [c 3] [b 2]" // Mymap::iterator it2 = c1.begin(); // std::cout << " [" << it2->first << ", " << it2->second << "]"; // ++it2; // std::cout << " [" << it2->first << ", " << it2->second << "]"; // std::cout << std::endl; // // // inspect bucket containing 'a' // Mymap::const_local_iterator lit = c1.begin(c1.bucket('a')); // std::cout << " [" << lit->first << ", " << lit->second << "]"; // // return (0); //} // // //// ////unordered_multimap::bucket獲取鍵值的存儲桶編號。 ////size_type bucket(const Key& keyval) const;//keyval 映射的鍵值。 ////成員函數(shù)返回當(dāng)前數(shù)字存儲桶與鍵值的 keyval。這個函數(shù)返回的桶數(shù)目前對應(yīng)keyval關(guān)鍵值。 //// std_tr1__unordered_map__unordered_multimap_bucket.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // display buckets for keys // Mymap::size_type bs = c1.bucket('a'); // std::cout << "bucket('a') == " << bs << std::endl; // std::cout << "bucket_size(" << bs << ") == " << c1.bucket_size(bs) // << std::endl; // // return (0); //} ////unordered_multimap::bucket_count獲取存儲桶數(shù)。 ////size_type bucket_count() const;成員函數(shù)返回存儲桶的當(dāng)前數(shù)目。 //// std_tr1__unordered_map__unordered_multimap_bucket_count.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // inspect current parameters // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_bucket_count() == " // << c1.max_bucket_count() << std::endl; // std::cout << "max_load_factor() == " // << c1.max_load_factor() << std::endl; // std::cout << std::endl; // // // change max_load_factor and redisplay // c1.max_load_factor(0.10f); // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_bucket_count() == " // << c1.max_bucket_count() << std::endl; // std::cout << "max_load_factor() == " // << c1.max_load_factor() << std::endl; // std::cout << std::endl; // // // rehash and redisplay // c1.rehash(100); // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_bucket_count() == " // << c1.max_bucket_count() << std::endl; // std::cout << "max_load_factor() == " // << c1.max_load_factor() << std::endl; // std::cout << std::endl; // // return (0); //} ////unordered_multimap::bucket_size獲取存儲桶的大小。 ////size_type bucket_size(size_type nbucket) const;//nbucket存儲桶編號。成員函數(shù)將返回存儲桶編號為 nbucket 的大小。 //// std_tr1__unordered_map__unordered_multimap_bucket_size.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // display buckets for keys // Mymap::size_type bs = c1.bucket('a'); // std::cout << "bucket('a') == " << bs << std::endl; // std::cout << "bucket_size(" << bs << ") == " << c1.bucket_size(bs) // << std::endl; // // return (0); //} // // // ////unordered_multimap::cbegin指定受控序列的開頭。 ////返回確定范圍中第一個元素地址的 const 迭代器。 ////const_iterator cbegin() const; ////const 前向訪問迭代器,指向范圍的第一個元素,或剛超出空范圍末尾的位置(對于空范圍,cbegin() == cend())。 ////由于使用 cbegin 的返回值,因此不能修改范圍中的元素。 //// ////可以使用此成員函數(shù)替代 begin() 成員函數(shù),以保證返回值為 const_iterator。 它一般與 auto 類型推導(dǎo)關(guān)鍵字聯(lián)合使用,如下例所示。 在該示例中,將 Container 視為支持 begin() 和 cbegin() 的任何類型的可修改(非 const)的容器。 ////auto i1 = Container.begin(); // i1 is Container<T>::iterator ////auto i2 = Container.cbegin(); // i2 is Container<T>::const_iterator // ////unordered_multimap::cend指定受控序列的末尾。 ////返回一個 const 迭代器,此迭代器用于發(fā)現(xiàn)剛超出范圍中最后一個元素的位置。 ////const_iterator cend() const;//指向剛超出范圍末尾的位置的 const 向前訪問迭代器。 ////cend 用于測試迭代器是否超過了其范圍的末尾。 ////可以使用此成員函數(shù)替代 end() 成員函數(shù),以保證返回值為 const_iterator。 它一般與 auto 類型推導(dǎo)關(guān)鍵字聯(lián)合使用,如下例所示。 在此示例中,將 Container 視為支持 end() 和 cend() 的任何類型的可修改(非 const)容器。 ////auto i1 = Container.end(); // i1 is Container<T>::iterator ////auto i2 = Container.cend(); // i2 is Container<T>::const_iterator // // ////unordered_multimap::clear移除所有元素。 //// // ////void clear(); //成員函數(shù)可調(diào)用 unordered_multimap::erase(unordered_multimap::begin(), unordered_multimap::end())。 //// std_tr1__unordered_map__unordered_multimap_clear.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // clear the container and reinspect // c1.clear(); // std::cout << "size == " << c1.size() << std::endl; // std::cout << "empty() == " << std::boolalpha << c1.empty() << std::endl; // std::cout << std::endl; // // c1.insert(Mymap::value_type('d', 4)); // c1.insert(Mymap::value_type('e', 5)); // // // display contents " [e 5] [d 4]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // std::cout << "size == " << c1.size() << std::endl; // std::cout << "empty() == " << std::boolalpha << c1.empty() << std::endl; // // return (0); //} ////unordered_multimap::count查看與指定鍵的元素的數(shù)目。 ////size_type count(const Key& keyval) const;//keyval 搜索的鍵值。 ////成員函數(shù)返回在劃定的范圍unordered_multimap元素個數(shù)::equal_range(keyval)。 unordered_multimap::equal_range分隔的范圍的(keyval)。 //// std_tr1__unordered_map__unordered_multimap_count.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // std::cout << "count('A') == " << c1.count('A') << std::endl; // std::cout << "count('b') == " << c1.count('b') << std::endl; // std::cout << "count('C') == " << c1.count('C') << std::endl; // // return (0); //} // // ////unordered_multimap::emplace添加就地構(gòu)造的元素。 ////在適當(dāng)?shù)奈恢貌迦霕?gòu)造的元素(不執(zhí)行復(fù)制或移動操作),附帶位置提示。 //template<class... Args> //iterator emplace( // Args&&... args); //template<class... Args> //iterator emplace( // Args&&... args); // ////args傳遞的參數(shù), 用于創(chuàng)建一個待插入unordered_multimap的元素 ////返回值 指向新插入元素的迭代器。 //此函數(shù)的容器元素的引用是無效的,但是,它可能使容器內(nèi)的所有迭代器都無效。 // //value_type 一個元素是一對, 使元素值為一個有序?qū)Γ谝粋€組件與鍵值相同和第二組件與元素數(shù)據(jù)值相同。 // //在插入代碼段時,如果異常被拋出,但沒有出現(xiàn)在容器的哈希函數(shù)中,將不會修改容器。 如果在哈希函數(shù)引發(fā)異常,則結(jié)果是未定義的。 // //有關(guān)代碼示例,請參見 multimap::emplace。 //// multimap_emplace.cpp //// compile with: /EHsc //#include <map> //#include <string> //#include <iostream> // //using namespace std; // //template <typename M> void print(const M& m) { // cout << m.size() << " elements: " << endl; // // for (const auto& p : m) { // cout << "(" << p.first << "," << p.second << ") "; // } // // cout << endl; //} // //int main() //{ // multimap<string, string> m1; // // m1.emplace("Anna", "Accounting"); // m1.emplace("Bob", "Accounting"); // m1.emplace("Carmine", "Engineering"); // // cout << "multimap modified, now contains "; // print(m1); // cout << endl; // // m1.emplace("Bob", "Engineering"); // // cout << "multimap modified, now contains "; // print(m1); // cout << endl; //} // // // ////unordered_multimap::emplace_hint添加就地構(gòu)造的元素,附帶提示。 ////在適當(dāng)?shù)奈恢貌迦霕?gòu)造的元素(不執(zhí)行復(fù)制或移動操作),附帶位置提示。 //template<class... Args> //iterator emplace_hint( // const_iterator where,//where有關(guān)起始位置的提示搜索正確位置插入。 // Args&&... args);//args傳遞的參數(shù), 用于創(chuàng)建一個待插入無序的元素 // //--返回值 指向新插入元素的迭代器。 // //備注 //此函數(shù)的容器元素的引用是無效的,但是,它可能使容器內(nèi)的所有迭代器都無效。 // //在插入代碼段時,如果異常被拋出,但沒有出現(xiàn)在容器的哈希函數(shù)中,將不會修改容器。 如果在哈希函數(shù)引發(fā)異常,則結(jié)果是未定義的。 // //value_type 一個元素是一對, 使元素值為一個有序?qū)Γ谝粋€組件與鍵值相同和第二組件與元素數(shù)據(jù)值相同。 // //有關(guān)代碼示例,請參見 map::emplace_hint。 //// map_emplace.cpp //// compile with: /EHsc //#include <map> //#include <string> //#include <iostream> // //using namespace std; // //template <typename M> void print(const M& m) { // cout << m.size() << " elements: " << endl; // // for (const auto& p : m) { // cout << "(" << p.first << "," << p.second << ") "; // } // // cout << endl; //} // //int main() //{ // map<string, string> m1; // // // Emplace some test data // m1.emplace("Anna", "Accounting"); // m1.emplace("Bob", "Accounting"); // m1.emplace("Carmine", "Engineering"); // // cout << "map starting data: "; // print(m1); // cout << endl; // // // Emplace with hint // // m1.end() should be the "next" element after this emplacement // m1.emplace_hint(m1.end(), "Doug", "Engineering"); // // cout << "map modified, now contains "; // print(m1); // cout << endl; //} // // // // ////unordered_multimap::empty測試元素是否存在。 ////bool empty() const;對于空受控序列,該成員函數(shù)返回 true。 //// std_tr1__unordered_map__unordered_multimap_empty.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // clear the container and reinspect // c1.clear(); // std::cout << "size == " << c1.size() << std::endl; // std::cout << "empty() == " << std::boolalpha << c1.empty() << std::endl; // std::cout << std::endl; // // c1.insert(Mymap::value_type('d', 4)); // c1.insert(Mymap::value_type('e', 5)); // // // display contents " [e 5] [d 4]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // std::cout << "size == " << c1.size() << std::endl; // std::cout << "empty() == " << std::boolalpha << c1.empty() << std::endl; // // return (0); //} // ////unordered_multimap::end指定受控序列的末尾。 ////iterator end(); ////const_iterator end() const; ////local_iterator end(size_type nbucket); ////const_local_iterator end(size_type nbucket) const;//nbucket 存儲桶編號。 //前兩個成員函數(shù)返回一個向前迭代器,它指向剛超出序列末尾的位置。 最后兩個成員函數(shù)返回一個向前迭代器,它指向剛超出存儲桶 nbucket 末尾的位置。 //// std_tr1__unordered_map__unordered_multimap_end.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // inspect last two items " [a 1] [b 2]" // Mymap::iterator it2 = c1.end(); // --it2; // std::cout << " [" << it2->first << ", " << it2->second << "]"; // --it2; // std::cout << " [" << it2->first << ", " << it2->second << "]"; // std::cout << std::endl; // // // inspect bucket containing 'a' // Mymap::const_local_iterator lit = c1.end(c1.bucket('a')); // --lit; // std::cout << " [" << lit->first << ", " << lit->second << "]"; // // return (0); //} // // ////unordered_multimap::equal_range查找與指定鍵匹配的范圍。 //// 查找匹配指定密鑰的范圍。 //std::pair<iterator, iterator> //equal_range(const Key& keyval);// keyval搜索的關(guān)鍵值 //std::pair<const_iterator, const_iterator> //equal_range(const Key& keyval) const; ////這個函數(shù)返回一迭代器對x,[x.first,x.second)劃只是那些元素的控制序列,具有相同的排序keyval。如果沒有這樣的元素存在,這兩個迭代器是end()。 //// 成員函數(shù)返回一對迭代器 X 這樣 [X.first, X.second) 分離具有等效的順序使用 keyval控制的元素序列。 如果不存在此類元素,則兩個迭代器是 end()。 // // //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // display results of failed search // std::pair<Mymap::iterator, Mymap::iterator> pair1 = // c1.equal_range('x'); // std::cout << "equal_range('x'):"; // for (; pair1.first != pair1.second; ++pair1.first) // std::cout << " [" << pair1.first->first // << ", " << pair1.first->second << "]"; // std::cout << std::endl; // // // display results of successful search // pair1 = c1.equal_range('b'); // std::cout << "equal_range('b'):"; // for (; pair1.first != pair1.second; ++pair1.first) // std::cout << " [" << pair1.first->first // << ", " << pair1.first->second << "]"; // std::cout << std::endl; // // return (0); //} ////unordered_multimap::erase移除指定位置處的元素。 ////從指定位置的非有序多重映射中移除一個元素或一定范圍內(nèi)的元映或移除與指定關(guān)鍵值匹配的元素。 //iterator erase( // const_iterator Where // ); //iterator erase( // const_iterator First, // const_iterator Last // ); //size_type erase( // const key_type& Key // ); //Where要移除的元素的位置。 // //First要移除的第一個元素的位置。 // //Last最后一個元素前面的要移除元素的位置。 // //Key要移除元素的關(guān)鍵值。 //對于前兩個成員函數(shù),指定保持在所有元素外的第一個元素中移除一雙向迭代器或如果不存在這樣的元素設(shè)置為末尾的元素。 // //為第三個成員函數(shù),返回從集合中移除元素的數(shù)目。 //// map_erase.cpp //// compile with: /EHsc //#include <map> //#include <string> //#include <iostream> //#include <iterator> // next() and prev() helper functions //#include <utility> // make_pair() // //using namespace std; // //using mymap = map<int, string>; // //void printmap(const mymap& m) { // for (const auto& elem : m) { // cout << " [" << elem.first << ", " << elem.second << "]"; // } // cout << endl << "size() == " << m.size() << endl << endl; //} // //int main() //{ // mymap m1; // // // Fill in some data to test with, one at a time // m1.insert(make_pair(1, "A")); // m1.insert(make_pair(2, "B")); // m1.insert(make_pair(3, "C")); // m1.insert(make_pair(4, "D")); // m1.insert(make_pair(5, "E")); // // cout << "Starting data of map m1 is:" << endl; // printmap(m1); // // The 1st member function removes an element at a given position // m1.erase(next(m1.begin())); // cout << "After the 2nd element is deleted, the map m1 is:" << endl; // printmap(m1); // // // Fill in some data to test with, one at a time, using an intializer list // mymap m2 // { // { 10, "Bob" }, // { 11, "Rob" }, // { 12, "Robert" }, // { 13, "Bert" }, // { 14, "Bobby" } // }; // // cout << "Starting data of map m2 is:" << endl; // printmap(m2); // // The 2nd member function removes elements // // in the range [First, Last) // m2.erase(next(m2.begin()), prev(m2.end())); // cout << "After the middle elements are deleted, the map m2 is:" << endl; // printmap(m2); // // mymap m3; // // // Fill in some data to test with, one at a time, using emplace // m3.emplace(1, "red"); // m3.emplace(2, "yellow"); // m3.emplace(3, "blue"); // m3.emplace(4, "green"); // m3.emplace(5, "orange"); // m3.emplace(6, "purple"); // m3.emplace(7, "pink"); // // cout << "Starting data of map m3 is:" << endl; // printmap(m3); // // The 3rd member function removes elements with a given Key // mymap::size_type count = m3.erase(2); // // The 3rd member function also returns the number of elements removed // cout << "The number of elements removed from m3 is: " << count << "." << endl; // cout << "After the element with a key of 2 is deleted, the map m3 is:" << endl; // printmap(m3); //} // // ////unordered_multimap::find查找與指定鍵匹配的元素。 ////const_iterator find(const Key& keyval) const;//keyval 搜索的鍵值。 //成員函數(shù)返回(keyval).first。 unordered_multimap::equal_range //// std_tr1__unordered_map__unordered_multimap_find.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // try to find and fail // std::cout << "find('A') == " // << std::boolalpha << (c1.find('A') != c1.end()) << std::endl; // // // try to find and succeed // Mymap::iterator it = c1.find('b'); // std::cout << "find('b') == " // << std::boolalpha << (it != c1.end()) // << ": [" << it->first << ", " << it->second << "]" << std::endl; // // return (0); //} // ////unordered_multimap::get_allocator獲取存儲的分配器對象。 ////Alloc get_allocator() const;成員函數(shù)返回存儲的程序分配對象。 //// std_tr1__unordered_map__unordered_multimap_get_allocator.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //typedef std::allocator<std::pair<const char, int> > Myalloc; //int main() //{ // Mymap c1; // // Mymap::allocator_type al = c1.get_allocator(); // std::cout << "al == std::allocator() is " // << std::boolalpha << (al == Myalloc()) << std::endl; // // return (0); //} ////unordered_multimap::hash_function獲取存儲的哈希函數(shù)對象。 ////Hash hash_function() const;成員函數(shù)返回存儲的哈希函數(shù)對象。 //// std_tr1__unordered_map__unordered_multimap_hash_function.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // Mymap::hasher hfn = c1.hash_function(); // std::cout << "hfn('a') == " << hfn('a') << std::endl; // std::cout << "hfn('b') == " << hfn('b') << std::endl; // // return (0); //} ////unordered_multimap::insert添加元素。 ////將一個元素或元素范圍插入到 unordered_multimap 中。 //// (1) single element pair<iterator, bool> insert( const value_type& Val ); // (2) single element, perfect forwarded template<class ValTy> pair<iterator, bool> insert( ValTy&& Val ); // (3) single element with hint iterator insert( const_iterator Where, const value_type& Val ); // (4) single element, perfect forwarded, with hint template<class ValTy> iterator insert( const_iterator Where, ValTy&& Val ); // (5) range template<class InputIterator> void insert( InputIterator First, InputIterator Last ); // (6) initializer list void insert( initializer_list<value_type> IList ); //參數(shù) //描述 // //Val //要插入到 unordered_multimap 中的元素的值。 // //Where //開始搜索正確插入點的位置。 // //ValTy //指定 unordered_multimap 可用于構(gòu)造 value_type 元素的參數(shù)類型并將 Val 作為參數(shù)完美轉(zhuǎn)發(fā)的模板參數(shù)。 // //First //要復(fù)制的第一個元素的位置。 // //Last //要復(fù)制的最后一個元素以外的位置。 // //InputIterator //滿足 輸入迭代器要求的模板函數(shù)參數(shù),該輸入迭代器指向可用于構(gòu)造 value_type 對象的類型的元素。 // //IList //從中復(fù)制元素的 initializer_list。 //單個元素插入成員函數(shù)(1) 和(2) 返回迭代器,該迭代器指向?qū)⑿略夭迦氲?unordered_multimap 中的位置。 // //附帶提示的單個元素成員函數(shù)(3) 和(4) 返回迭代器,該迭代器指向?qū)⑿略夭迦氲?unordered_multimap 中的位置。 //指針或引用不會因為此函數(shù)而失效,但是它可能會使所有指向容器的迭代器都失效。 // //在只插入單個元素的過程中,如果引發(fā)異常,但是異常并未在容器的哈希函數(shù)中發(fā)生,則不會修改該容器的狀態(tài)。 如果在哈希函數(shù)中引發(fā)異常,則未定義此結(jié)果。 在插入多個元素的過程中,如果引發(fā)異常,則會使容器處于未指定但有效的狀態(tài)。 // //容器的 value_type 是屬于該容器的 typedef;對于映射,map<K, V>::value_type 是 pair<const K, V>。 元素的值是一個有序?qū)?,其中第一個組件相當(dāng)于鍵值,第二個組件相當(dāng)于該元素的數(shù)據(jù)值。 // //范圍成員函數(shù)(5) 將元素值序列插入到 unordered_multimap 中,它對應(yīng)于迭代器在范圍[First, Last) 中所處理的每一個元素;因此,不會插入 Last。 容器成員函數(shù) end() 是指容器中最后一個元素之后的位置,例如,m.insert(v.begin(), v.end()); 語句會將 v 的所有元素插入到 m 中。 // //初始值設(shè)定項列表成員函數(shù)(6) 使用 initializer_list 將元素復(fù)制到 unordered_multimap 中。 // //有關(guān)就地構(gòu)造的元素的插入(即不會執(zhí)行復(fù)制或移動操作),請參閱 unordered_multimap::emplace 和 unordered_multimap::emplace_hint。 // //有關(guān)代碼示例,請參閱 multiset::insert。 //// multiset_insert.cpp //// compile with: /EHsc //#include <set> //#include <iostream> //#include <string> //#include <vector> // //using namespace std; // //template <typename S> void print(const S& s) { // cout << s.size() << " elements: "; // // for (const auto& p : s) { // cout << "(" << p << ") "; // } // // cout << endl; //} // //int main() //{ // // // insert single values // multiset<int> s1; // // call insert(const value_type&) version // s1.insert({ 1, 10 }); // // call insert(ValTy&&) version // s1.insert(20); // // cout << "The original multiset values of s1 are:" << endl; // print(s1); // // // intentionally attempt a duplicate, single element // s1.insert(1); // cout << "The modified multiset values of s1 are:" << endl; // print(s1); // cout << endl; // // // single element, with hint // s1.insert(s1.end(), 30); // cout << "The modified multiset values of s1 are:" << endl; // print(s1); // cout << endl; // // // // The templatized version inserting a jumbled range // multiset<int> s2; // vector<int> v; // v.push_back(43); // v.push_back(294); // v.push_back(41); // v.push_back(330); // v.push_back(42); // v.push_back(45); // // cout << "Inserting the following vector data into s2:" << endl; // print(v); // // s2.insert(v.begin(), v.end()); // // cout << "The modified multiset values of s2 are:" << endl; // print(s2); // cout << endl; // // // The templatized versions move-constructing elements // multiset<string> s3; // string str1("blue"), str2("green"); // // // single element // s3.insert(move(str1)); // cout << "After the first move insertion, s3 contains:" << endl; // print(s3); // // // single element with hint // s3.insert(s3.end(), move(str2)); // cout << "After the second move insertion, s3 contains:" << endl; // print(s3); // cout << endl; // // multiset<int> s4; // // Insert the elements from an initializer_list // s4.insert({ 4, 44, 2, 22, 3, 33, 1, 11, 5, 55 }); // cout << "After initializer_list insertion, s4 contains:" << endl; // print(s4); // cout << endl; //} // // // // ////unordered_multimap::key_eq獲取存儲的比較函數(shù)對象。 ////Pred key_eq() const;成員函數(shù)返回存儲的比較函數(shù)對象。 //// std_tr1__unordered_map__unordered_multimap_key_eq.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // Mymap::key_equal cmpfn = c1.key_eq(); // std::cout << "cmpfn('a', 'a') == " // << std::boolalpha << cmpfn('a', 'a') << std::endl; // std::cout << "cmpfn('a', 'b') == " // << std::boolalpha << cmpfn('a', 'b') << std::endl; // // return (0); //} ////unordered_multimap::load_factor對每個存儲桶的平均元素數(shù)進(jìn)行計數(shù)。 ////float load_factor() const; //成員函數(shù)() / (float)返回(float) unordered_multimap::size unordered_multimap::bucket_count,()元素的數(shù)量平均每個存儲桶。 //這個函數(shù)返回(浮動)unordered_multimap::size() / (浮動)unordered_multimap::bucket_count(),每個桶中元素的平均數(shù)量 //// std_tr1__unordered_map__unordered_multimap_load_factor.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // inspect current parameters // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_bucket_count() == " // << c1.max_bucket_count() << std::endl; // std::cout << "max_load_factor() == " // << c1.max_load_factor() << std::endl; // std::cout << std::endl; // // // change max_load_factor and redisplay // c1.max_load_factor(0.10f); // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_bucket_count() == " // << c1.max_bucket_count() << std::endl; // std::cout << "max_load_factor() == " // << c1.max_load_factor() << std::endl; // std::cout << std::endl; // // // rehash and redisplay // c1.rehash(100); // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_bucket_count() == " // << c1.max_bucket_count() << std::endl; // std::cout << "max_load_factor() == " // << c1.max_load_factor() << std::endl; // std::cout << std::endl; // // return (0); //} // ////unordered_multimap::max_bucket_count獲取最大的存儲桶數(shù)。獲取包含的最大項數(shù)。 ////size_type max_bucket_count() const;成員函數(shù)返回當(dāng)前存儲桶中允許的最大次數(shù)。 //// std_tr1__unordered_map__unordered_multimap_max_bucket_count.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // inspect current parameters // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_bucket_count() == " // << c1.max_bucket_count() << std::endl; // std::cout << "max_load_factor() == " // << c1.max_load_factor() << std::endl; // std::cout << std::endl; // // // change max_load_factor and redisplay // c1.max_load_factor(0.10f); // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_bucket_count() == " // << c1.max_bucket_count() << std::endl; // std::cout << "max_load_factor() == " // << c1.max_load_factor() << std::endl; // std::cout << std::endl; // // // rehash and redisplay // c1.rehash(100); // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_bucket_count() == " // << c1.max_bucket_count() << std::endl; // std::cout << "max_load_factor() == " // << c1.max_load_factor() << std::endl; // std::cout << std::endl; // // return (0); //} ////////////////////////////////////////// ////unordered_multimap::max_load_factor獲取或設(shè)置每個存儲桶的最多元素數(shù)。 //float max_load_factor() const; //void max_load_factor(float factor);//factor 最大負(fù)載以便新的因素。 ////第一成員函數(shù)返回存儲的最大負(fù)載因子。二次成員函數(shù)替換了存儲的最大負(fù)載因子。 // // //// std_tr1__unordered_map__unordered_multimap_max_bucket_count.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // inspect current parameters // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_bucket_count() == " // << c1.max_bucket_count() << std::endl; // std::cout << "max_load_factor() == " // << c1.max_load_factor() << std::endl; // std::cout << std::endl; // // // change max_load_factor and redisplay // c1.max_load_factor(0.10f); // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_bucket_count() == " // << c1.max_bucket_count() << std::endl; // std::cout << "max_load_factor() == " // << c1.max_load_factor() << std::endl; // std::cout << std::endl; // // // rehash and redisplay // c1.rehash(100); // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_bucket_count() == " // << c1.max_bucket_count() << std::endl; // std::cout << "max_load_factor() == " // << c1.max_load_factor() << std::endl; // std::cout << std::endl; // // return (0); //} // ////unordered_multimap::max_size獲取受控序列的最大大小。 ////size_type max_size() const;成員函數(shù)返回對象可以控制最長序列的長度 //// std_tr1__unordered_map__unordered_multimap_max_size.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // std::cout << "max_size() == " << c1.max_size() << std::endl; // // return (0); //} ////unordered_multimap::rehash重新生成哈希表。 ////void rehash(size_type nbuckets);//nbuckets 請求的存儲桶數(shù)。 ////成員函數(shù)將存儲桶數(shù)更改為至少 nbuckets并根據(jù)需要重新生成哈希表。 //// std_tr1__unordered_map__unordered_multimap_rehash.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // inspect current parameters // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_load_factor() == " << c1.max_load_factor() << std::endl; // std::cout << std::endl; // // // change max_load_factor and redisplay // c1.max_load_factor(0.10f); // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_load_factor() == " << c1.max_load_factor() << std::endl; // std::cout << std::endl; // // // rehash and redisplay // c1.rehash(100); // std::cout << "bucket_count() == " << c1.bucket_count() << std::endl; // std::cout << "load_factor() == " << c1.load_factor() << std::endl; // std::cout << "max_load_factor() == " << c1.max_load_factor() << std::endl; // // return (0); //} // // ////unordered_multimap::size對元素數(shù)進(jìn)行計數(shù)。 ////計算元素的數(shù)量。 ////size_type size() const;備注:成員函數(shù)返回控制序列的長度。 //// std_tr1__unordered_map__unordered_multimap_size.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // // clear the container and reinspect // c1.clear(); // std::cout << "size == " << c1.size() << std::endl; // std::cout << "empty() == " << std::boolalpha << c1.empty() << std::endl; // std::cout << std::endl; // // c1.insert(Mymap::value_type('d', 4)); // c1.insert(Mymap::value_type('e', 5)); // // // display contents " [e 5] [d 4]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // std::cout << "size == " << c1.size() << std::endl; // std::cout << "empty() == " << std::boolalpha << c1.empty() << std::endl; // // return (0); //} // ////unordered_multimap::swap交換兩個容器的內(nèi)容。 ////void swap(unordered_multimap& right);//right 交換的容器。 ////成員函數(shù)將交換了 *this 和 right之間的控制序列。 unordered_multimap::get_allocator如果() == right.get_allocator(),在常數(shù)的時間執(zhí)行,僅引發(fā)異常導(dǎo)致復(fù)制 Tr類型的對象存儲字符,因此,它不在指定無效兩個控制序列的元素的引用、指針或迭代器。 否則,它將執(zhí)行大量的元素賦值,然后構(gòu)造函數(shù)調(diào)用而與元素數(shù)目在兩個控件的順序。 // // //// std_tr1__unordered_map__unordered_multimap_swap.cpp //// compile with: /EHsc //#include <unordered_map> //#include <iostream> // //typedef std::unordered_multimap<char, int> Mymap; //int main() //{ // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // Mymap c2; // // c2.insert(Mymap::value_type('d', 4)); // c2.insert(Mymap::value_type('e', 5)); // c2.insert(Mymap::value_type('f', 6)); // // c1.swap(c2); // // // display contents " [f 6] [e 5] [d 4]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // swap(c1, c2); // // // display contents " [c 3] [b 2] [a 1]" // for (Mymap::const_iterator it = c1.begin(); // it != c1.end(); ++it) // std::cout << " [" << it->first << ", " << it->second << "]"; // std::cout << std::endl; // // return (0); //} ////unordered_multimap::unordered_multimap構(gòu)造容器對象。 ////unordered_multimap( // const unordered_multimap& Right // ); // explicit unordered_multimap( // size_type Bucket_count = N0, // const Hash& Hash = Hash(), // const Comp& Comp = Pred(), // const Allocator& Al = Alloc() // ); // unordered_multimap( // unordered_multimap&& Right // ); // unordered_multimap( // initializer_list<Type> IList // ); // unordered_multimap( // initializer_list< Type > IList, // size_type Bucket_count // ); // unordered_multimap( // initializer_list< Type > IList, // size_type Bucket_count, // const Hash& Hash // ); // unordered_multimap( // initializer_list< Type > IList, // size_type Bucket_count, // const Hash& Hash, // const Key& Key // ); // unordered_multimap( // initializer_list<Type> IList, // size_type Bucket_count, // const Hash& Hash, // const Key& Key, // const Allocator& Al // ); // template<class InputIterator> // unordered_multimap( // InputIterator first, // InputIterator last, // size_type Bucket_count = N0, // const Hash& Hash = Hash(), // const Comp& Comp = Pred(), // const Allocator& Al = Alloc() // ); // InputIterator 迭代器類型。 // // Al 要存儲的分配器對象。 // // Comp 要存儲的比較函數(shù)對象。 // // Hash 要存儲的哈希函數(shù)對象。 // // Bucket_count 存儲桶的最少數(shù)量。 // // Right 要復(fù)制的容器。 // // IList 從中復(fù)制元素的 initializer_list。 // // 第一個構(gòu)造函數(shù)指定通過 Right 控制的序列副本。 第二個構(gòu)造函數(shù)指定空的受控序列。 第三個構(gòu)造函數(shù)。 通過移動 Right 指定序列副本。 第四、第五、第六、第七和第八個構(gòu)造函數(shù)對成員使用 initializer_list。 第九個構(gòu)造函數(shù)插入元素值[First, Last) 的序列。 // // 所有構(gòu)造函數(shù)還初始化若干存儲的值。 對于復(fù)制構(gòu)造函數(shù),值從 Right 獲取。 否則: // // 存儲桶的最少數(shù)量是參數(shù) Bucket_count(如果有);否則為此處說明的默認(rèn)值 N0。 // // 哈希函數(shù)對象是參數(shù) Hash(如果有);否則為 Hash()。 // // 比較函數(shù)對象是參數(shù) Comp(如果有);否則為 Pred()。 // // 分配器對象是參數(shù) Al(如果有);否則為 Alloc()。 // // // std__unordered_map__unordered_multimap_construct.cpp // // compile with: /EHsc //#include <unordered_map> //#include <iostream> // // using namespace std; // // using Mymap = unordered_multimap<char, int>; // int main() // { // Mymap c1; // // c1.insert(Mymap::value_type('a', 1)); // c1.insert(Mymap::value_type('b', 2)); // c1.insert(Mymap::value_type('c', 3)); // // // display contents " [c 3] [b 2] [a 1]" // for (const auto& c : c1) { // cout << " [" << c.first << ", " << c.second << "]"; // } // cout << endl; // // // Mymap c2(8, // hash<char>(), // equal_to<char>(), // allocator<pair<const char, int> >()); // // c2.insert(Mymap::value_type('d', 4)); // c2.insert(Mymap::value_type('e', 5)); // c2.insert(Mymap::value_type('f', 6)); // // // display contents " [f 6] [e 5] [d 4]" // for (const auto& c : c2) { // cout << " [" << c.first << ", " << c.second << "]"; // } // cout << endl; // // Mymap c3(c1.begin(), // c1.end(), // 8, // hash<char>(), // equal_to<char>(), // allocator<pair<const char, int> >()); // // // display contents " [c 3] [b 2] [a 1]" // for (const auto& c : c3) { // cout << " [" << c.first << ", " << c.second << "]"; // } // cout << endl; // // Mymap c4(move(c3)); // // // display contents " [c 3] [b 2] [a 1]" // for (const auto& c : c4) { // cout << " [" << c.first << ", " << c.second << "]"; // } // cout << endl; // // // Construct with an initializer_list // unordered_multimap<int, char> c5({ { 5, 'g' },{ 6, 'h' },{ 7, 'i' },{ 8, 'j' } }); // for (const auto& c : c5) { // cout << " [" << c.first << ", " << c.second << "]"; // } // cout << endl; // // // Initializer_list plus size // unordered_multimap<int, char> c6({ { 5, 'g' },{ 6, 'h' },{ 7, 'i' },{ 8, 'j' } }, 4); // for (const auto& c : c1) { // cout << " [" << c.first << ", " << c.second << "]"; // } // cout << endl; // // // Initializer_list plus size and hash // unordered_multimap<int, char, tr1::hash<char>> c7( // { { 5, 'g' },{ 6, 'h' },{ 7, 'i' },{ 8, 'j' } }, // 4, // tr1::hash<char>() // ); // // for (const auto& c : c1) { // cout << " [" << c.first << ", " << c.second << "]"; // } // cout << endl; // // // Initializer_list plus size, hash, and key_equal // unordered_multimap<int, char, tr1::hash<char>, equal_to<char>> c8( // { { 5, 'g' },{ 6, 'h' },{ 7, 'i' },{ 8, 'j' } }, // 4, // tr1::hash<char>(), // equal_to<char>() // ); // // for (const auto& c : c1) { // cout << " [" << c.first << ", " << c.second << "]"; // } // cout << endl; // // // Initializer_list plus size, hash, key_equal, and allocator // unordered_multimap<int, char, tr1::hash<char>, equal_to<char>> c9( // { { 5, 'g' },{ 6, 'h' },{ 7, 'i' },{ 8, 'j' } }, // 4, // tr1::hash<char>(), // equal_to<char>(), // allocator<pair<const char, int> >() // ); // // for (const auto& c : c1) { // cout << " [" << c.first << ", " << c.second << "]"; // } // cout << endl; // } //// ////運(yùn)算符說明 //// ////unordered_multimap::operator= 復(fù)制哈希表。 // unordered_multimap& operator=( // const unordered_multimap& _Right // ); // unordered_multimap& operator=( // unordered_multimap&& _Right // ); // _Right // 正在被復(fù)制到 unordered_multimap 中的 unordered_multimap。 // 在清除 unordered_multimap 中的任何現(xiàn)有元素后,operator = 會將 _Right 的內(nèi)容復(fù)制或移動到 unordered_multimap 中。 // // unordered_multimap_operator_as.cpp // // compile with: /EHsc //#include <unordered_multimap> //#include <iostream> // // int main() // { // using namespace std; // unordered_multimap<int, int> v1, v2, v3; // unordered_multimap<int, int>::iterator iter; // // v1.insert(pair<int, int>(1, 10)); // // cout << "v1 = "; // for (iter = v1.begin(); iter != v1.end(); iter++) // cout << iter->second << " "; // cout << endl; // // v2 = v1; // cout << "v2 = "; // for (iter = v2.begin(); iter != v2.end(); iter++) // cout << iter->second << " "; // cout << endl; // // // move v1 into v2 // v2.clear(); // v2 = move(v1); // cout << "v2 = "; // for (iter = v2.begin(); iter != v2.end(); iter++) // cout << iter->second << " "; // cout << endl; // } //
|
|