Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
Her bir öğenin hem veri değerine hem de sıralama anahtarına sahip olduğu bir çift olan bir koleksiyondaki verileri alma ve depolama için kullanılır. Anahtarın değeri benzersizdir ve verileri otomatik olarak sıralamak için kullanılır.
Bir eşlemdeki bir öğenin değeri doğrudan değiştirilemez. Anahtar değeri sabittir ve değiştirilemez. Bunun yerine, eski öğelerle ilişkili anahtar değerleri silinmeli ve yeni öğeler için yeni anahtar değerleri eklenmelidir.
Sözdizimi
template <class Key,
class Type,
class Traits = less<Key>,
class Allocator=allocator<pair <const Key, Type>>>
class map;
Parametreler
Key
içinde mapdepolanacak anahtar veri türü.
Type
içinde mapdepolanacak öğe veri türü.
Traits
içinde göreli düzenlerini mapbelirlemek için iki öğe değerini sıralama anahtarları olarak karşılaştırabilen bir işlev nesnesi sağlayan tür. Bu bağımsız değişken isteğe bağlıdır ve ikili koşul less<Key> varsayılan değerdir.
C++14'te, tür parametresi olmayan koşulu belirterek std::less<> heterojen aramayı etkinleştirebilirsiniz. Daha fazla bilgi için bkz . İlişkili Kapsayıcılarda Heterojen Arama.
Allocator
Eşlemin bellek ayırmayı ve ayırmayı kaldırma hakkındaki ayrıntıları içeren saklı ayırıcı nesnesini gösteren tür. Bu bağımsız değişken isteğe bağlıdır ve varsayılan değerdir allocator<pair<const Key, Type> >.
Açıklamalar
C++ Standart Kitaplığı eşleme sınıfı:
Öğe değerlerini etkin olarak alan değişken boyutunun bir kapsayıcısı ilişkili anahtar değerlerine dayalıdır.
Çift yönlüdür, çünkü öğelerine erişmek için çift yönlü yineleyiciler sağlar.
Sıralıdır, çünkü öğeleri belirtilen karşılaştırma işlevine uygun olarak anahtar değerlere göre sıralanır.
Eşsiz. çünkü öğelerinin her biri benzersiz bir anahtara sahip olmalıdır.
Bir çift ilişkilendirilebilir kapsayıcıdır, çünkü veri öğelerinin değerleri kendi anahtar değerlerinden farklıdır.
Sağladığı işlevsellik genel olduğundan ve öğeden veya anahtar türünden bağımsız olduğundan sınıf şablonu. Öğeler ve anahtarlar için kullanılan veri türleri, karşılaştırma işlevi ve ayırıcı ile birlikte bir sınıf şablonunda parametreler olarak belirtilir.
Eşleme sınıfı tarafından sağlanan yineleyici çift yönlü bir yineleyicidir, ancak insert ve map sınıf üyesi işlevleri, işlev gereksinimleri çift yönlü yineleyiciler sınıfı tarafından garanti edilenlerden daha az olan şablon parametreleri olarak daha zayıf bir giriş yineleyicisi alan sürümlere sahiptir. Farklı yineleyici kavramları, işlevselliklerindeki iyileştirmelerle ilgilidir. Her yineleyici kavramı kendi gereksinimler kümesine sahiptir ve onunla çalışan algoritmalar bu gereksinimler ile sınırlı olmalıdır. Bir giriş yineleyicinin bazı nesnelere başvurusu kaldırılabilir ve dizideki sonraki yineleyiciye artırılabilir.
Kapsayıcı türünün seçimini uygulamanın gerektirdiği arama ve ekleme türüne dayalı yapmanız önerilir. İlişkilendirilebilir kapsayıcılar, arama, ekleme ve kaldırma işlemleri için en iyi duruma getirilir. Bu işlemleri açıkça destekleyen üye işlevleri, bunları kapsayıcıdaki öğe sayısının logaritmasıyla orantılı en kötü durumda yapar. Öğelerin eklenmesi hiçbir yineleyiciyi geçersiz kılmaz; öğelerin kaldırılması yalnızca özellikle kaldırılan öğeleri gösteren yineleyicileri geçersiz kılar.
Eşlemi, değerleri anahtarlarıyla ilişkilendiren koşullar uygulama tarafından karşılandığında seçimin ilişkili kapsayıcısı yapmanız önerilir. Bu tür bir yapı modeli, tanımlar sağlayan ilişkilendirmiş dize değerlerine sahip benzersiz oluşan anahtar sözcüklerin sıralı bir listesidir. Bir sözcüğün birden fazla doğru tanımı varsa ve bu nedenle anahtar benzersiz değilse, çok eşlemeli bir kapsayıcı tercih edilebilir. Yalnızca sözcüklerin listesi depolanmaktaysa, uygun kapsayıcı bir küme olacaktır. Sözcüklerin birden çok defa geçmelerine izin verilirse, bir çoklu küme uygun olacaktır.
Eşleme, türünde key_comparebir saklı işlev nesnesi çağırarak denetlediği öğeleri sıralar. Bu depolanan nesne, yöntemini çağırarak key_comp erişilen bir karşılaştırma işlevidir. Genel olarak, belirli iki öğe, birinin diğerinden küçük olup olmadığını veya eşdeğer olup olmadığını belirlemek için karşılaştırılır. Tüm öğeler karşılaştırıldığında, denk olmayan öğelerin sıralı bir dizisi oluşturulur.
Not
Karşılaştırma işlevi standart matematiksel anlamda katı bir zayıf sıralama sevk eden ikili bir koşuldur. İkili koşul f(x,y), x ve y adlı iki bağımsız değişken nesnesine ve veya truedönüş değerine false sahip bir işlev nesnesidir. İkili koşul esnek değilse, antisymetrik ve geçişliyse ve denklik geçişliyse, hem f(x,y) hem de f(y,x) olduğunda iki x ve y nesnesinin eşdeğer olarak tanımlandığı kümeye uygulanan sıralama katı bir zayıf sıralamadır false. Anahtarlar arasındaki eşitliğinin daha güçlü koşulu bu denkliğin yerini alırsa, sıralama (içindeki tüm öğelerin birbirine göre sıralanması anlamında) toplam haline gelir ve eşleşen anahtarlar birbirinden ayırt edilemez olacaktır.
C++14'te, tür parametresi olmayan veya std::less<> koşulunu std::greater<> belirterek heterojen aramayı etkinleştirebilirsiniz. Daha fazla bilgi için bkz . İlişkili Kapsayıcılarda Heterojen Arama.
Üyeler
Oluşturucular
| Veri Akışı Adı | Açıklama |
|---|---|
map |
Belirli bir boyutta veya belirli bir değerin öğeleriyle ya da belirli bir veya başka bir allocator eşlemenin kopyası olarak bir liste oluşturur. |
Tür tanımları
| Veri Akışı Adı | Açıklama |
|---|---|
allocator_type |
Eşleme nesnesinin allocator sınıfı için bir typedef. |
const_iterator |
içindeki bir öğeyi constokuyabilen çift yönlü bir yineleyici için typedefmap. |
const_pointer |
Haritadaki bir öğeye yönelik bir const işaretçi için typedef. |
const_reference |
Okuma ve işlem yapma const amacıyla bir eşlemede depolanan bir const öğeye başvuru için typedef. |
const_reverse_iterator |
içindeki constherhangi map bir öğeyi okuyabilen çift yönlü bir yineleyici sağlayan tür. |
difference_type |
Yineleyiciler tarafından gösterilen öğeler arasındaki bir aralıktaki eşlemin öğelerinin işaretli bir tamsayı typedef'i. |
iterator |
Eşlem içindeki herhangi bir öğeyi okuyabilen veya değiştirebilen çift yönlü yineleyiciler için typedef. |
key_compare |
içindeki iki öğeye mapgöre sırasını belirlemek için iki sıralama anahtarını karşılaştırabilen bir işlev nesnesi için typedef. |
key_type |
Eşlemin her öğesinde depolanan sıralama anahtarı için bir typedef. |
mapped_type |
Bir eşlemin her öğesinde depolanan veriler için bir typedef. |
pointer |
Haritadaki bir öğeye yönelik bir const işaretçi için typedef. |
reference |
Bir eşlem içinde depolan bir öğeye başvuru için bir typedef. |
reverse_iterator |
Ters döndürülmüş eşlem içindeki bir öğeyi okuyabilen veya değiştirebilen çift yönlü yineleyiciler için typedef. |
size_type |
Bir eşlemdeki öğe sayısı için işaretsiz tamsayı typedef'i. |
value_type |
Bir eşlem içinde bir öğe olarak depolanan nesne türü için bir typedef. |
Üye işlevleri
| Üye işlevi | Açıklama |
|---|---|
at |
Belirtilen anahtar değerine sahip bir öğe bulur. |
begin |
içindeki ilk öğeye mapişaret eden bir yineleyici döndürür. |
cbegin |
içindeki ilk öğeye mapişaret eden bir sabit yineleyici döndürür. |
cend |
Bir sabit past-the-end yineleyici döndürür. |
clear |
öğesinin maptüm öğelerini siler. |
contains
C++20 |
içinde mapbelirtilen anahtara sahip bir öğe olup olmadığını denetleyin. |
count |
Anahtarı bir parametre içinde belirtilen anahtarla eşleşen eşlem içindeki öğelerin sayısını döndürür. |
crbegin |
Ters çevrilmiş mapiçindeki ilk öğeye işaret eden bir sabit yineleyici döndürür. |
crend |
Ters çevrilmiş mapbir içindeki son öğeden sonraki konuma işaret eden bir sabit yineleyici döndürür. |
emplace |
içine yerinde mapbir öğe ekler. |
emplace_hint |
bir yerleştirme ipucuyla içine yerinde mapinşa edilmiş bir öğe ekler. |
empty |
boşsa true döndürürmap. |
end |
past-the-end yineleyici döndürür. |
equal_range |
Yineleyicilerin bir çiftini döndürür. Çiftteki ilk yineleyici, belirtilen anahtardan büyük bir anahtara sahip bir içindeki ilk öğeye map işaret eder. Çiftteki ikinci yineleyici, anahtarına eşit veya anahtardan daha büyük bir anahtarla içindeki ilk öğeyi map gösterir. |
erase |
Bir eşlemdeki bir öğe veya öğe aralığını belirtilen konumdan kaldırır. |
find |
Belirtilen anahtara eşit bir anahtara sahip bir öğenin map konumuna işaret eden bir yineleyici döndürür. |
get_allocator |
nesnesini oluşturmak allocator için kullanılan nesnesinin mapbir kopyasını döndürür. |
insert |
Belirtilen konumda öğesine map bir öğe veya öğe aralığı ekler. |
key_comp |
bir içindeki anahtarları sıralamak için kullanılan karşılaştırma nesnesinin bir mapkopyasını döndürür. |
lower_bound |
Bir yineleyiciyi, belirtilen anahtarın değerine eşit veya bundan daha büyük bir anahtar değerine sahip bir içindeki ilk öğeye map döndürür. |
max_size |
değerinin uzunluk üst sınırını mapdöndürür. |
rbegin |
ters çevrilmiş mapiçindeki ilk öğeyi gösteren bir yineleyici döndürür. |
rend |
Ters çevrilmiş mapbir içindeki son öğeden sonraki konumu gösteren bir yineleyici döndürür. |
size |
içindeki mapöğe sayısını döndürür. |
swap |
İki eşlemin öğelerini birbiriyle değiştirir. |
upper_bound |
Belirtilen anahtardan daha büyük bir anahtar değerine sahip bir içindeki ilk öğeye map bir yineleyici döndürür. |
value_comp |
içindeki öğe değerlerini sıralamak için kullanılan karşılaştırma nesnesinin bir mapkopyasını alır. |
İşleçler
| Veri Akışı Adı | Açıklama |
|---|---|
operator[] |
Belirtilen bir anahtar değere sahip bir eşleme bir öğe ekler. |
operator= |
Bir eşlemin öğelerini başka bir eşlem kopyasıyla değiştirir. |
allocator_type
Eşleme nesnesi için ayırıcı sınıfını temsil eden bir tür.
typedef Allocator allocator_type;
Örnek
kullanan get_allocatorbir örnek için allocator_type örneğine bakın.
at
Belirtilen anahtar değere sahip bir öğe bulur.
Type& at(const Key& key);
const Type& at(const Key& key) const;
Parametreler
key
Bulunacak anahtar değeri.
Dönüş Değeri
Bulunan öğenin veri değerine başvuru.
Açıklamalar
Bağımsız değişken anahtarı değeri bulunamazsa, işlev sınıfı out_of_range sınıfına ait bir nesnesi oluşturur.
Örnek
// map_at.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
typedef std::map<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));
// find and show elements
std::cout << "c1.at('a') == " << c1.at('a') << std::endl;
std::cout << "c1.at('b') == " << c1.at('b') << std::endl;
std::cout << "c1.at('c') == " << c1.at('c') << std::endl;
return (0);
}
begin
içindeki ilk öğeyi mapele alan bir yineleyici döndürür.
const_iterator begin() const;
iterator begin();
Dönüş Değeri
boş bir eşlemenin başarılı olduğu veya konumundaki ilk öğeyi map ele alan çift yönlü bir yineleyici.
Örnek
// map_begin.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int> m1;
map <int, int> :: iterator m1_Iter;
map <int, int> :: const_iterator m1_cIter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 0, 0 ) );
m1.insert ( Int_Pair ( 1, 1 ) );
m1.insert ( Int_Pair ( 2, 4 ) );
m1_cIter = m1.begin ( );
cout << "The first element of m1 is " << m1_cIter -> first << endl;
m1_Iter = m1.begin ( );
m1.erase ( m1_Iter );
// The following 2 lines would err because the iterator is const
// m1_cIter = m1.begin ( );
// m1.erase ( m1_cIter );
m1_cIter = m1.begin( );
cout << "The first element of m1 is now " << m1_cIter -> first << endl;
}
The first element of m1 is 0
The first element of m1 is now 1
cbegin
Bir const aralıktaki son öğenin hemen ötesindeki konumu ele alan bir yineleyici döndürür.
const_iterator cbegin() const;
Dönüş Değeri
Aralıktaki const ilk öğeyi veya boş aralığın sonunun hemen ötesindeki konumu (boş cbegin() == cend()aralık için) ele alan çift yönlü yineleyici.
Açıklamalar
değerinin cbegindönüş değeriyle, aralıktaki öğeler değiştirilemez.
Dönüş değerinin olduğunu begin()garanti etmek için üye işlevinin const_iterator yerine bu üye işlevini kullanabilirsiniz. Genellikle, aşağıdaki örnekte gösterildiği gibi tür kesinti anahtar sözcüğüyle auto birlikte kullanılır. Örnekte, ve Containerdestekleyen herhangi bir türde const değiştirilebilir (non- begin()) kapsayıcısı olduğunu düşününcbegin().
auto i1 = Container.begin();
// i1 is Container<T>::iterator
auto i2 = Container.cbegin();
// i2 is Container<T>::const_iterator
cend
Bir const aralıktaki son öğenin hemen ötesindeki konumu ele alan bir yineleyici döndürür.
const_iterator cend() const;
Dönüş Değeri
Aralığın const sonunun hemen ötesine işaret eden çift yönlü erişim yineleyicisi.
Açıklamalar
cend bir yineleyicinin aralığının sonunu geçip geçmediğini test etmek için kullanılır.
Dönüş değerinin olduğunu end()garanti etmek için üye işlevinin const_iterator yerine bu üye işlevini kullanabilirsiniz. Genellikle, aşağıdaki örnekte gösterildiği gibi tür kesinti anahtar sözcüğüyle auto birlikte kullanılır. Örnekte, ve Containerdestekleyen herhangi bir türde const değiştirilebilir (non- end()) kapsayıcısı olduğunu düşününcend().
auto i1 = Container.end();
// i1 is Container<T>::iterator
auto i2 = Container.cend();
// i2 is Container<T>::const_iterator
tarafından cend döndürülen değer başvurulmamalıdır.
clear
Eşlemin tüm öğelerini siler.
void clear();
Örnek
Aşağıdaki örnekte üye işlevinin kullanımı gösterilmektedir map::clear .
// map_clear.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main()
{
using namespace std;
map<int, int> m1;
map<int, int>::size_type i;
typedef pair<int, int> Int_Pair;
m1.insert(Int_Pair(1, 1));
m1.insert(Int_Pair(2, 4));
i = m1.size();
cout << "The size of the map is initially "
<< i << "." << endl;
m1.clear();
i = m1.size();
cout << "The size of the map after clearing is "
<< i << "." << endl;
}
The size of the map is initially 2.
The size of the map after clearing is 0.
const_iterator
içindeki bir öğeyi constokuyabilen çift yönlü bir yineleyici sağlayan türmap.
typedef implementation-defined const_iterator;
Açıklamalar
Bir öğenin değerini değiştirmek için bir tür const_iterator kullanılamaz.
const_iterator eşlemesi tarafından tanımlanan öğesi, ilk üyesi öğenin anahtarı olan ve ikinci üyesi öğesi tarafından tutulan eşlenmiş datum olan türündeki value_typenesneleri pair<constKey, Type>olan öğelere işaret eder.
Eşlemedeki bir const_iteratorcIter öğeye işaret eden bir başvuruyu geri almak için işlecini -> kullanın.
öğesinin anahtarının değerine erişmek için , cIter (* >) ile eşdeğer olan -firstcIterkullanın.
first.
Öğesinin eşlenmiş veri kümesinin değerine erişmek için , cIter (* >) ile eşdeğer olan -secondcIterkullanın.
second.
Örnek
kullanan beginbir örnek için const_iterator örneğine bakın.
const_pointer
Haritadaki bir const öğeye işaretçi sağlayan tür.
typedef typename allocator_type::const_pointer const_pointer;
Açıklamalar
Bir öğenin değerini değiştirmek için bir tür const_pointer kullanılamaz.
Çoğu durumda, iterator bir eşleme nesnesindeki öğelere erişmek için kullanılmalıdır.
const_reference
Okuma ve işlem yapma const amacıyla haritada depolanan bir const öğeye başvuru sağlayan tür.
typedef typename allocator_type::const_reference const_reference;
Örnek
// map_const_ref.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int> m1;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
// Declare and initialize a const_reference &Ref1
// to the key of the first element
const int &Ref1 = ( m1.begin( ) -> first );
// The following line would cause an error as the
// non-const_reference can't be used to access the key
// int &Ref1 = ( m1.begin( ) -> first );
cout << "The key of first element in the map is "
<< Ref1 << "." << endl;
// Declare and initialize a reference &Ref2
// to the data value of the first element
int &Ref2 = ( m1.begin( ) -> second );
cout << "The data value of first element in the map is "
<< Ref2 << "." << endl;
}
The key of first element in the map is 1.
The data value of first element in the map is 10.
const_reverse_iterator
içindeki constherhangi map bir öğeyi okuyabilen çift yönlü bir yineleyici sağlayan tür.
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Açıklamalar
Bir tür const_reverse_iterator bir öğenin değerini değiştiremez ve eşlem boyunca tersten yinelemek için kullanılır.
const_reverse_iterator eşlemesi tarafından tanımlanan öğesi, ilk üyesi öğenin anahtarı olan ve ikinci üyesi öğesi tarafından tutulan eşlenmiş datum olan türündeki value_typenesneleri pair<const Key, Type>olan öğelere işaret eder.
Eşlemedeki bir const_reverse_iterator crIter öğeye işaret eden bir başvuruyu geri almak için işlecini -> kullanın.
öğesinin anahtarının değerine erişmek için , crIter (* >)first ile eşdeğer olan -crIterfirstkullanın.
Öğesinin eşlenmiş veri kümesinin değerine erişmek için , crIter (* >)second ile eşdeğer olan -crIterfirstkullanın.
Örnek
bildirme ve kullanma rendörneği için const_reverse_iterator örneğine bakın.
count
Anahtarı parametre tarafından belirtilen bir anahtarla eşleşen bir eşlemedeki öğelerin sayısını döndürür.
size_type count(const Key& key) const;
Parametreler
key
Eşlemeden eşleştirilecek öğelerin anahtar değeri.
Dönüş Değeri
Eşleme, sıralama anahtarı parametre anahtarıyla eşleşen bir öğe içeriyorsa 1; Eşleme eşleşen anahtara sahip bir öğe içermiyorsa 0.
Açıklamalar
Üye işlevi aralıktaki x öğelerinin sayısını döndürür
[ lower_bound(anahtar), upper_bound(anahtar) )
eşleme durumunda 0 veya 1'dir ve bu benzersiz bir ilişkilendirici kapsayıcıdır.
Örnek
Aşağıdaki örnekte üye işlevinin kullanımı gösterilmektedir map::count .
// map_count.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main()
{
using namespace std;
map<int, int> m1;
map<int, int>::size_type i;
typedef pair<int, int> Int_Pair;
m1.insert(Int_Pair(1, 1));
m1.insert(Int_Pair(2, 1));
m1.insert(Int_Pair(1, 4));
m1.insert(Int_Pair(2, 1));
// Keys must be unique in map, so duplicates are ignored
i = m1.count(1);
cout << "The number of elements in m1 with a sort key of 1 is: "
<< i << "." << endl;
i = m1.count(2);
cout << "The number of elements in m1 with a sort key of 2 is: "
<< i << "." << endl;
i = m1.count(3);
cout << "The number of elements in m1 with a sort key of 3 is: "
<< i << "." << endl;
}
The number of elements in m1 with a sort key of 1 is: 1.
The number of elements in m1 with a sort key of 2 is: 1.
The number of elements in m1 with a sort key of 3 is: 0.
contains
içinde mapbelirtilen anahtara sahip bir öğe olup olmadığını denetler.
bool contains(const Key& key) const;
template<class K> bool contains(const K& key) const;
Parametreler
K
Anahtarın türü.
key
Aranacak öğenin anahtar değeri.
Dönüş Değeri
true öğesi kapsayıcıda bulunursa; false yoksa.
Açıklamalar
contains() C++20 sürümünde yenidir. Bunu kullanmak için veya sonraki derleyici seçeneğini belirtin /std:c++20 .
template<class K> bool contains(const K& key) const yalnızca saydamsa key_compare aşırı yükleme çözümlemeye katılır. Daha fazla bilgi için bkz . İlişkili kapsayıcılarda heterojen arama.
Örnek
// Requires /std:c++20 or later
#include <map>
#include <string>
#include <iostream>
#include <functional>
int main()
{
std::map<int, bool> m = {{0, true},{1, false}};
std::cout << std::boolalpha; // so booleans show as 'true' or 'false'
std::cout << m.contains(1) << '\n';
std::cout << m.contains(2) << '\n';
// call template function
std::map<std::string, int, std::less<>> m2 = {{"ten", 10}, {"twenty", 20}, {"thirty", 30}};
std::cout << m2.contains("ten");
return 0;
}
true
false
true
crbegin
Ters çevrilmiş bir eşlemedeki ilk öğeyi ele alan bir sabit yineleyici döndürür.
const_reverse_iterator crbegin() const;
Dönüş Değeri
Ters çevrilmiş map bir içindeki ilk öğeyi veya ters çevrilmemiş mapiçindeki son öğeyi ele alan en büyük ters çift yönlü yineleyici.
Açıklamalar
crbegin, ile kullanıldığı gibi map ters çevrilmiş begin bir mapile kullanılır.
değerinin crbegindönüş değeriyle map nesne değiştirilemez
crbegin geriye doğru yinelemek map için kullanılabilir.
Örnek
// map_crbegin.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int> m1;
map <int, int> :: const_reverse_iterator m1_crIter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 3, 30 ) );
m1_crIter = m1.crbegin( );
cout << "The first element of the reversed map m1 is "
<< m1_crIter -> first << "." << endl;
}
The first element of the reversed map m1 is 3.
crend
Ters çevrilmiş bir eşlemedeki son öğeyi başaran konumu ele alan bir sabit yineleyici döndürür.
const_reverse_iterator crend() const;
Dönüş Değeri
Ters çevrilmiş bir öğedeki son öğeyi (ters map çevrilmemiş mapöğedeki ilk öğeden önce gelen konum) başarılı olan konumu ele alan en ters çift yönlü yineleyici.
Açıklamalar
crend , aynı bir ile kullanıldığı gibi end ters çevrilmiş bir mapharitayla kullanılır.
dönüş değeriyle crendmap nesne değiştirilemez.
crend , ters yineleyicinin sonuna mapulaşıp ulaşmadığını test etmek için kullanılabilir.
tarafından crend döndürülen değer başvurulmamalıdır.
Örnek
// map_crend.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int> m1;
map <int, int> :: const_reverse_iterator m1_crIter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 3, 30 ) );
m1_crIter = m1.crend( );
m1_crIter--;
cout << "The last element of the reversed map m1 is "
<< m1_crIter -> first << "." << endl;
}
The last element of the reversed map m1 is 1.
difference_type
Yineleyiciler tarafından işaret edilen öğeler arasındaki bir aralıktaki eşlem öğelerinin sayısını göstermek için kullanılabilecek imzalı bir tamsayı türü.
typedef allocator_type::difference_type difference_type;
Açıklamalar
difference_type, kapsayıcının yineleyicileri çıkarılırken veya artırılırken döndürülen türdür.
difference_type genellikle yineleyiciler ile firstarasındaki [ ilk, son)lastöğe sayısını temsil etmek için first kullanılır; tarafından işaret edilen öğeyi ve ile işaret edilen lastöğeyi dahil etmek için değil, öğesine kadar olan öğe aralığını içerir.
Küme gibi geri alınabilir kapsayıcılar tarafından desteklenen çift yönlü yineleyici sınıfını içeren bir giriş yineleyicisinin gereksinimlerini karşılayan tüm yineleyiciler için kullanılabilir olsa difference_type da, yineleyiciler arasındaki çıkarma yalnızca vektör gibi rastgele bir erişim kapsayıcısı tarafından sağlanan rastgele erişim yineleyicileri tarafından desteklenir.
Örnek
// map_diff_type.cpp
// compile with: /EHsc
#include <iostream>
#include <map>
#include <algorithm>
int main( )
{
using namespace std;
map <int, int> m1;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 3, 20 ) );
m1.insert ( Int_Pair ( 2, 30 ) );
map <int, int>::iterator m1_Iter, m1_bIter, m1_eIter;
m1_bIter = m1.begin( );
m1_eIter = m1.end( );
// Count the number of elements in a map
map <int, int>::difference_type df_count = 1;
m1_Iter = m1.begin( );
while ( m1_Iter != m1_eIter)
{
df_count++;
m1_Iter++;
}
cout << "The number of elements in the map m1 is: "
<< df_count << "." << endl;
}
The number of elements in the map m1 is: 4.
emplace
Bir eşlem içine yerinde (kopyalama veya taşıma işlemi yapılmaz) yerleşik bir öğe ekler.
template <class... Args>
pair<iterator, bool>
emplace(
Args&&... args);
Parametreler
args
Değeri eşdeğer sıralanmış bir öğe içermediği sürece eşlemeye eklenecek bir öğe oluşturmak için iletilen bağımsız değişkenler.
Dönüş Değeri
pair
bool Bileşeni true ekleme yapılıp yapılmaması ve false eşlemenin sıralamada eşdeğer değere sahip bir öğe içermesi. Dönüş-değer çiftinin yineleyici bileşeni, bileşen true ise bool yeni eklenen öğeye veya bileşen false ise var olan öğeye bool işaret eder.
bir pairprpr.firstöğesinin yineleyici bileşenine erişmek için kullanın; başvuruyu geri almak için kullanın.*pr.first Bileşene bool erişmek için kullanın pr.second. Örnek için bu makalenin devamında yer alan örnek koda bakın.
Açıklamalar
Bu işlev tarafından hiçbir yineleyici veya başvuru geçersiz kılınmamış.
Yer değiştirme sırasında özel durum oluşursa kapsayıcının durumu değiştirilmez.
value_type Bir öğenin değeri bir çifttir, böylece bir öğenin değeri ilk bileşen anahtar değerine eşit, ikinci bileşen de öğenin veri değerine eşit olan sıralı bir çift olur.
Örnek
// 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: ";
for (const auto& p : m) {
cout << "(" << p.first << ", " << p.second << ") ";
}
cout << endl;
}
int main()
{
map<int, string> m1;
auto ret = m1.emplace(10, "ten");
if (!ret.second){
auto pr = *ret.first;
cout << "Emplace failed, element with key 10 already exists."
<< endl << " The existing element is (" << pr.first << ", " << pr.second << ")"
<< endl;
cout << "map not modified" << endl;
}
else{
cout << "map modified, now contains ";
print(m1);
}
cout << endl;
ret = m1.emplace(10, "one zero");
if (!ret.second){
auto pr = *ret.first;
cout << "Emplace failed, element with key 10 already exists."
<< endl << " The existing element is (" << pr.first << ", " << pr.second << ")"
<< endl;
}
else{
cout << "map modified, now contains ";
print(m1);
}
cout << endl;
}
emplace_hint
Yerleştirme ipucuyla yerinde (hiçbir kopyalama veya taşıma işlemi gerçekleştirilmemiş) bir öğe ekler.
template <class... Args>
iterator emplace_hint(
const_iterator where,
Args&&... args);
Parametreler
args
Eşlem zaten bu öğeyi içermiyorsa veya daha genel olarak, anahtarı eşdeğer olarak sıralanmış bir öğe içermiyorsa, eşlemeye eklenecek bir öğe oluşturmak için iletilen bağımsız değişkenler.
where
Doğru ekleme noktasını aramaya başlanması gereken yer. (Bu nokta hemen önceyse ekleme işlemi logaritmik saat yerine amorti edilmiş sabit sürede gerçekleşebilir.)
Dönüş Değeri
Yeni eklenen öğeye bir yineleyici.
Öğe zaten var olduğundan ekleme başarısız olduysa, var olan öğeye anahtarıyla bir yineleyici döndürür.
Açıklamalar
Bu işlev tarafından hiçbir yineleyici veya başvuru geçersiz kılınmamış.
Yer değiştirme sırasında özel durum oluşursa kapsayıcının durumu değiştirilmez.
value_type Bir öğenin değeri bir çifttir, böylece bir öğenin değeri ilk bileşen anahtar değerine eşit, ikinci bileşen de öğenin veri değerine eşit olan sıralı bir çift olur.
Örnek
// 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;
}
empty
Haritanın boş olup olmadığını sınar.
bool empty() const;
Dönüş Değeri
true harita boşsa; false haritanın yok olup olmadığını gösterir.
Örnek
// map_empty.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int> m1, m2;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 1 ) );
if ( m1.empty( ) )
cout << "The map m1 is empty." << endl;
else
cout << "The map m1 is not empty." << endl;
if ( m2.empty( ) )
cout << "The map m2 is empty." << endl;
else
cout << "The map m2 is not empty." << endl;
}
The map m1 is not empty.
The map m2 is empty.
end
past-the-end yineleyici döndürür.
const_iterator end() const;
iterator end();
Dönüş Değeri
Son zaman yineleyicisi. Harita boşsa, öğesini seçin map::end() == map::begin().
Açıklamalar
end bir yineleyicinin eşlemesinin sonunu geçip geçmediğini test etmek için kullanılır.
tarafından end döndürülen değer başvurulmamalıdır.
Kod örneği için bkz map::find. .
equal_range
Anahtarın ve lower_bound anahtarın upper_bound değerini temsil eden bir çift yineleyici döndürür.
pair <const_iterator, const_iterator> equal_range (const Key& key) const;
pair <iterator, iterator> equal_range (const Key& key);
Parametreler
key
Aranmakta olan eşlemedeki bir öğenin sıralama anahtarıyla karşılaştırılacak bağımsız değişken anahtarı değeri.
Dönüş Değeri
Üye işlevi tarafından döndürülen bir çiftin pr ilk yineleyicisine erişmek için kullanın pr.
önce ve alt sınır yineleyicisini geri almak için *(pr kullanın. önce). Üye işlevi tarafından döndürülen bir çiftin pr ikinci yineleyicisine erişmek için kullanın pr.
ikinci ve üst sınır yineleyicisini geri almak için *(pr kullanın. saniye).
Örnek
// map_equal_range.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
typedef map <int, int, less<int> > IntMap;
IntMap m1;
map <int, int> :: const_iterator m1_RcIter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 3, 30 ) );
pair <IntMap::const_iterator, IntMap::const_iterator> p1, p2;
p1 = m1.equal_range( 2 );
cout << "The lower bound of the element with "
<< "a key of 2 in the map m1 is: "
<< p1.first -> second << "." << endl;
cout << "The upper bound of the element with "
<< "a key of 2 in the map m1 is: "
<< p1.second -> second << "." << endl;
// Compare the upper_bound called directly
m1_RcIter = m1.upper_bound( 2 );
cout << "A direct call of upper_bound( 2 ) gives "
<< m1_RcIter -> second << "," << endl
<< "matching the 2nd element of the pair"
<< " returned by equal_range( 2 )." << endl;
p2 = m1.equal_range( 4 );
// If no match is found for the key,
// both elements of the pair return end( )
if ( ( p2.first == m1.end( ) ) && ( p2.second == m1.end( ) ) )
cout << "The map m1 doesn't have an element "
<< "with a key less than 40." << endl;
else
cout << "The element of map m1 with a key >= 40 is: "
<< p2.first -> first << "." << endl;
}
The lower bound of the element with a key of 2 in the map m1 is: 20.
The upper bound of the element with a key of 2 in the map m1 is: 30.
A direct call of upper_bound( 2 ) gives 30,
matching the 2nd element of the pair returned by equal_range( 2 ).
The map m1 doesn't have an element with a key less than 40.
erase
Belirtilen konumlardan bir öğeyi veya bir eşlemedeki öğe aralığını kaldırır veya belirtilen bir anahtarla eşleşen öğeleri kaldırır.
iterator erase(
const_iterator Where);
iterator erase(
const_iterator First,
const_iterator Last);
size_type erase(
const key_type& Key);
Parametreler
Where
Kaldırılacak öğenin konumu.
First
Kaldırılacak ilk öğenin konumu.
Last
Kaldırılacak son öğenin hemen ötesinde konumlandırın.
Key
Kaldırılacak öğelerin anahtar değeri.
Dönüş Değeri
İlk iki üye işlevi için, kaldırılan öğelerin ötesinde kalan ilk öğeyi belirten çift yönlü bir yineleyici veya böyle bir öğe yoksa eşlemenin sonu olan bir öğe.
Üçüncü üye işlevi için, eşlemeden kaldırılan öğelerin sayısını döndürür.
Örnek
// 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 initializer 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);
}
find
Belirtilen anahtara eşdeğer bir anahtara sahip bir eşlemedeki öğenin konumuna başvuran bir yineleyici döndürür.
iterator find(const Key& key);
const_iterator find(const Key& key) const;
Parametreler
key
Aranmakta olan eşlemedeki bir öğenin sıralama anahtarıyla eşleştirilecek anahtar değeri.
Dönüş Değeri
Belirtilen anahtara sahip bir öğenin konumuna veya anahtar için eşleşme bulunamazsa (map) içindeki map::end() son öğenin başarılı olduğu konuma başvuran bir yineleyici.
Açıklamalar
Üye işlevi, sıralama anahtarı, karşılaştırılabilirlikten küçük bir ilişkiyi temel alan bir sıralamaya neden olan ikili koşul altındaki bağımsız değişken anahtarına eşdeğer olan bir öğeye map başvuran bir yineleyici döndürür.
değerinin dönüş değeri find bir const_iteratoröğesine atanmışsa, eşleme nesnesi değiştirilemez. değerinin dönüş değeri find bir iteratoröğesine atanmışsa, eşleme nesnesi değiştirilebilir
Örnek
// compile with: /EHsc /W4 /MTd
#include <map>
#include <iostream>
#include <vector>
#include <string>
#include <utility> // make_pair()
using namespace std;
template <typename A, typename B> void print_elem(const pair<A, B>& p) {
cout << "(" << p.first << ", " << p.second << ") ";
}
template <typename T> void print_collection(const T& t) {
cout << t.size() << " elements: ";
for (const auto& p : t) {
print_elem(p);
}
cout << endl;
}
template <typename C, class T> void findit(const C& c, T val) {
cout << "Trying find() on value " << val << endl;
auto result = c.find(val);
if (result != c.end()) {
cout << "Element found: "; print_elem(*result); cout << endl;
} else {
cout << "Element not found." << endl;
}
}
int main()
{
map<int, string> m1({ { 40, "Zr" }, { 45, "Rh" } });
cout << "The starting map m1 is (key, value):" << endl;
print_collection(m1);
vector<pair<int, string>> v;
v.push_back(make_pair(43, "Tc"));
v.push_back(make_pair(41, "Nb"));
v.push_back(make_pair(46, "Pd"));
v.push_back(make_pair(42, "Mo"));
v.push_back(make_pair(44, "Ru"));
v.push_back(make_pair(44, "Ru")); // attempt a duplicate
cout << "Inserting the following vector data into m1:" << endl;
print_collection(v);
m1.insert(v.begin(), v.end());
cout << "The modified map m1 is (key, value):" << endl;
print_collection(m1);
cout << endl;
findit(m1, 45);
findit(m1, 6);
}
get_allocator
Eşlemeyi oluşturmak için kullanılan ayırıcı nesnesinin bir kopyasını döndürür.
allocator_type get_allocator() const;
Dönüş Değeri
Harita tarafından kullanılan ayırıcı.
Açıklamalar
Eşleme sınıfı için ayırıcılar, sınıfın depolamayı nasıl yöneteceğini belirtir. C++ Standart Kitaplığı kapsayıcı sınıfları ile sağlanan varsayılan ayırıcılar çoğu programlama gereksinimi için yeterlidir. Kendi ayırıcı sınıfınızı yazmak ve kullanmak gelişmiş bir C++ konusudur.
Örnek
// map_get_allocator.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int>::allocator_type m1_Alloc;
map <int, int>::allocator_type m2_Alloc;
map <int, double>::allocator_type m3_Alloc;
map <int, int>::allocator_type m4_Alloc;
// The following lines declare objects
// that use the default allocator.
map <int, int> m1;
map <int, int, allocator<int> > m2;
map <int, double, allocator<double> > m3;
m1_Alloc = m1.get_allocator( );
m2_Alloc = m2.get_allocator( );
m3_Alloc = m3.get_allocator( );
cout << "The number of integers that can be allocated\n"
<< "before free memory is exhausted: "
<< m2.max_size( ) << ".\n" << endl;
cout << "The number of doubles that can be allocated\n"
<< "before free memory is exhausted: "
<< m3.max_size( ) << ".\n" << endl;
// The following line creates a map m4
// with the allocator of map m1.
map <int, int> m4( less<int>( ), m1_Alloc );
m4_Alloc = m4.get_allocator( );
// Two allocators are interchangeable if
// storage allocated from each can be
// deallocated with the other
if( m1_Alloc == m4_Alloc )
{
cout << "The allocators are interchangeable." << endl;
}
else
{
cout << "The allocators are not interchangeable." << endl;
}
}
insert
Haritaya bir öğe veya öğe aralığı ekler.
// (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);
Parametreler
Val
Anahtarı eşdeğer olarak sıralanmış bir öğe içermediği sürece eşlemeye eklenecek öğenin değeri.
Where
Doğru ekleme noktasını aramaya başlanması gereken yer. (Bu nokta hemen önüne Wheregeçerse ekleme işlemi logaritmik saat yerine amorti edilmiş sabit sürede gerçekleşebilir.)
ValTy
Eşlemenin öğesini value_typeoluşturmak için kullanabileceği bağımsız değişken türünü belirten şablon parametresi ve bağımsız değişken olarak kusursuz iletme.Val
First
Kopyalanacak ilk öğenin konumu.
Last
Kopyalanacak son öğenin hemen ötesindeki konum.
InputIterator
Nesne oluşturmak için kullanılabilecek bir value_type gereksinimlerini karşılayan şablon işlevi bağımsız değişkeni.
IList
initializer_list Öğelerin kopyalandığı yer.
Dönüş Değeri
Tek öğeli üye işlevleri (1) ve (2), ekleme yapıldığında bileşeni true olan pair bir bool döndürür ve eşlemede anahtarı sıralamada eşdeğer bir değere sahip olan bir öğe varsa false döndürür. Dönüş-değer çiftinin yineleyici bileşeni, bileşen true ise bool yeni eklenen öğeye veya bileşen false ise var olan öğeye bool işaret eder.
İpucuyla tek öğeli üye işlevleri (3) ve (4), yeni öğenin eşlem içine eklendiği konumu veya eşdeğer anahtara sahip bir öğe zaten varsa mevcut öğeye işaret eden bir yineleyici döndürür.
Açıklamalar
Bu işlev tarafından hiçbir yineleyici, işaretçi veya başvuru geçersiz kılınmamış.
Yalnızca bir öğenin eklenmesi sırasında özel durum oluşursa kapsayıcının durumu değiştirilmez. Birden çok öğe eklenirken, bir özel durum oluşturulursa kapsayıcı belirtilmemiş ancak geçerli bir durumda bırakılır.
Tek öğeli üye işlevleri tarafından döndürülen bir pairpr öğesinin yineleyici bileşenine erişmek için , öğesini kullanarak pr.firstdöndürülen çift içinde yineleyiciye başvuruyu kaldırın, kullanın *pr.firstve size bir öğe sağlayın. Bileşene bool erişmek için kullanın pr.second. Örnek için bu makalenin devamında yer alan örnek koda bakın.
value_type Kapsayıcının türü, kapsayıcıya ait olan bir tür tanımıdır ve eşleme map<K, V>::value_type için ise şeklindedirpair<const K, V>. Öğenin değeri, ilk bileşenin anahtar değerine, ikinci bileşenin ise öğenin veri değerine eşit olduğu sıralı bir çifttir.
Aralık üye işlevi (5), öğe değerlerinin dizisini aralıktaki [First, Last)bir yineleyici tarafından ele alınan her öğeye karşılık gelen bir eşleme ekler; bu nedenle eklenmez Last . Kapsayıcı üyesi işlevi end() , kapsayıcıdaki son öğeden hemen sonraki konuma başvurur; örneğin, deyimi m.insert(v.begin(), v.end()); tüm öğelerini v içine meklemeye çalışır. Yalnızca aralıkta benzersiz değerlere sahip öğeler eklenir; yinelenenler yoksayılır. Hangi öğelerin reddedildiğini gözlemlemek için öğesinin tek öğeli sürümlerini insertkullanın.
Başlatıcı listesi üye işlevi (6), öğeleri eşlem içine kopyalamak için bir initializer_list kullanır.
Yerinde inşa edilmiş bir öğenin eklenmesi için (başka bir deyişle, hiçbir kopyalama veya taşıma işlemi gerçekleştirilmez), bkz map::emplace . ve map::emplace_hint.
Örnek
// map_insert.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
#include <string>
#include <vector>
#include <utility> // make_pair()
using namespace std;
template <typename M> void print(const M& m) {
cout << m.size() << " elements: ";
for (const auto& p : m) {
cout << "(" << p.first << ", " << p.second << ") ";
}
cout << endl;
}
int main()
{
// insert single values
map<int, int> m1;
// call insert(const value_type&) version
m1.insert({ 1, 10 });
// call insert(ValTy&&) version
m1.insert(make_pair(2, 20));
cout << "The original key and mapped values of m1 are:" << endl;
print(m1);
// intentionally attempt a duplicate, single element
auto ret = m1.insert(make_pair(1, 111));
if (!ret.second){
auto pr = *ret.first;
cout << "Insert failed, element with key value 1 already exists."
<< endl << " The existing element is (" << pr.first << ", " << pr.second << ")"
<< endl;
}
else{
cout << "The modified key and mapped values of m1 are:" << endl;
print(m1);
}
cout << endl;
// single element, with hint
m1.insert(m1.end(), make_pair(3, 30));
cout << "The modified key and mapped values of m1 are:" << endl;
print(m1);
cout << endl;
// The templatized version inserting a jumbled range
map<int, int> m2;
vector<pair<int, int>> v;
v.push_back(make_pair(43, 294));
v.push_back(make_pair(41, 262));
v.push_back(make_pair(45, 330));
v.push_back(make_pair(42, 277));
v.push_back(make_pair(44, 311));
cout << "Inserting the following vector data into m2:" << endl;
print(v);
m2.insert(v.begin(), v.end());
cout << "The modified key and mapped values of m2 are:" << endl;
print(m2);
cout << endl;
// The templatized versions move-constructing elements
map<int, string> m3;
pair<int, string> ip1(475, "blue"), ip2(510, "green");
// single element
m3.insert(move(ip1));
cout << "After the first move insertion, m3 contains:" << endl;
print(m3);
// single element with hint
m3.insert(m3.end(), move(ip2));
cout << "After the second move insertion, m3 contains:" << endl;
print(m3);
cout << endl;
map<int, int> m4;
// Insert the elements from an initializer_list
m4.insert({ { 4, 44 }, { 2, 22 }, { 3, 33 }, { 1, 11 }, { 5, 55 } });
cout << "After initializer_list insertion, m4 contains:" << endl;
print(m4);
cout << endl;
}
iterator
Bir eşlemedeki herhangi bir öğeyi okuyabilen veya değiştirebilen çift yönlü bir yineleyici sağlayan tür.
typedef implementation-defined iterator;
Açıklamalar
Eşleme tarafından tanımlanan yineleyici, ilk üyesi öğenin anahtarı olan ve ikinci üyesi öğesi tarafından tutulan eşlenmiş datum olan türündeki nesneleri value_typepair<const Key, Type>olan öğeleri işaret eder.
Eşlemedeki bir öğeye işaret eden yineleyici yineleyicinin başvurularını geri almak için işlecini -> kullanın.
öğesinin anahtarının değerine erişmek için, Iter->firstile eşdeğer (*Iter).firstolan kullanın. öğesinin eşlenmiş veri kümesinin değerine erişmek için öğesine Iter->secondeşdeğer (*Iter).secondolan kullanın.
Örnek
nasıl bildirilip kullanılacağına beginilişkin bir örnek için iterator örneğine bakın.
key_comp
Haritadaki anahtarları sıralamak için kullanılan karşılaştırma nesnesinin bir kopyasını alır.
key_compare key_comp() const;
Dönüş Değeri
Bir eşlemenin öğelerini sıralamak için kullandığı işlev nesnesini döndürür.
Açıklamalar
Depolanan nesne üye işlevini tanımlar
bool operator(const Key& left, const Key& right);
true
left döndürür ve sıralama düzeninde bu değere right eşit değildir.
Örnek
// map_key_comp.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int, less<int> > m1;
map <int, int, less<int> >::key_compare kc1 = m1.key_comp( ) ;
bool result1 = kc1( 2, 3 ) ;
if( result1 == true )
{
cout << "kc1( 2,3 ) returns value of true, "
<< "where kc1 is the function object of m1."
<< endl;
}
else
{
cout << "kc1( 2,3 ) returns value of false "
<< "where kc1 is the function object of m1."
<< endl;
}
map <int, int, greater<int> > m2;
map <int, int, greater<int> >::key_compare kc2 = m2.key_comp( );
bool result2 = kc2( 2, 3 ) ;
if( result2 == true )
{
cout << "kc2( 2,3 ) returns value of true, "
<< "where kc2 is the function object of m2."
<< endl;
}
else
{
cout << "kc2( 2,3 ) returns value of false, "
<< "where kc2 is the function object of m2."
<< endl;
}
}
kc1( 2,3 ) returns value of true, where kc1 is the function object of m1.
kc2( 2,3 ) returns value of false, where kc2 is the function object of m2.
key_compare
içindeki iki öğeye mapgöre sırasını belirlemek için iki sıralama anahtarını karşılaştırabilen bir işlev nesnesi sağlayan tür.
typedef Traits key_compare;
Açıklamalar
key_compare , şablon parametresinin Traitseş anlamlısıdır.
hakkında Traitsdaha fazla bilgi için Sınıfmap bakın.
Örnek
nasıl bildirilip kullanılacağına key_compilişkin bir örnek için key_compare örneğine bakın.
key_type
Eşlemenin her öğesinde depolanan sıralama anahtarını açıklayan bir tür.
typedef Key key_type;
Açıklamalar
key_type , şablon parametresinin Keyeş anlamlısıdır.
hakkında Keydaha fazla bilgi için Sınıfmap bakın.
Örnek
nasıl bildirilip kullanılacağına value_typeilişkin bir örnek için key_type örneğine bakın.
lower_bound
Bir yineleyiciyi, belirtilen anahtarın değerine eşit veya ondan daha büyük bir anahtar değeriyle eşlemedeki ilk öğeye döndürür.
iterator lower_bound(const Key& key);
const_iterator lower_bound(const Key& key) const;
Parametreler
key
Aranmakta olan eşlemedeki bir öğenin sıralama anahtarıyla karşılaştırılacak bağımsız değişken anahtarı değeri.
Dönüş Değeri
Bir iterator öğenin konumunu, bağımsız değişken anahtarına eşit veya ondan büyük bir anahtarla veya anahtar için eşleşme bulunamazsa içindeki son öğeyi const_iterator tamamlayan konumu ele alan bir veyamap.
değerinin dönüş değeri lower_bound bir const_iteratoröğesine atanmışsa, eşleme nesnesi değiştirilemez. değerinin dönüş değeri lower_bound bir iteratoröğesine atanmışsa, eşleme nesnesi değiştirilebilir.
Örnek
// map_lower_bound.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int> m1;
map <int, int> :: const_iterator m1_AcIter, m1_RcIter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 3, 30 ) );
m1_RcIter = m1.lower_bound( 2 );
cout << "The first element of map m1 with a key of 2 is: "
<< m1_RcIter -> second << "." << endl;
// If no match is found for this key, end( ) is returned
m1_RcIter = m1. lower_bound ( 4 );
if ( m1_RcIter == m1.end( ) )
cout << "The map m1 doesn't have an element "
<< "with a key of 4." << endl;
else
cout << "The element of map m1 with a key of 4 is: "
<< m1_RcIter -> second << "." << endl;
// The element at a specific location in the map can be found
// using a dereferenced iterator addressing the location
m1_AcIter = m1.end( );
m1_AcIter--;
m1_RcIter = m1. lower_bound ( m1_AcIter -> first );
cout << "The element of m1 with a key matching "
<< "that of the last element is: "
<< m1_RcIter -> second << "." << endl;
}
The first element of map m1 with a key of 2 is: 20.
The map m1 doesn't have an element with a key of 4.
The element of m1 with a key matching that of the last element is: 30.
map
Boş olan veya başka bir haritanın tümünün veya bir bölümünün kopyası olan bir harita oluşturur.
map();
explicit map(
const Traits& Comp);
map(
const Traits& Comp,
const Allocator& Al);
map(
const map& Right);
map(
map&& Right);
map(
initializer_list<value_type> IList);
map(
initializer_list<value_type> IList,
const Traits& Comp);
map(
initializer_list<value_type> IList,
const Traits& Comp,
const Allocator& Allocator);
template <class InputIterator>
map(
InputIterator First,
InputIterator Last);
template <class InputIterator>
map(
InputIterator First,
InputIterator Last,
const Traits& Comp);
template <class InputIterator>
map(
InputIterator First,
InputIterator Last,
const Traits& Comp,
const Allocator& Al);
Parametreler
Al
Varsayılan olarak Allocatorolarak olan bu eşleme nesnesi için kullanılacak depolama ayırıcı sınıfı.
Comp
türündeki const Traits öğeleri mapsıralamak için kullanılan karşılaştırma işlevidir ve varsayılan olarak hash_compareolarak kullanılır.
Right
Oluşturulacak kümenin bir kopyası olduğu harita.
First
Kopyalanacak öğe aralığındaki ilk öğenin konumu.
Last
Kopyalanacak öğe aralığının ötesindeki ilk öğenin konumu.
IList
Öğelerin kopyalandığı initializer_list.
Açıklamalar
Tüm oluşturucular, eşleme için bellek depolamasını yöneten ve daha sonra çağrılarak get_allocatordöndürülebilen bir ayırıcı nesnesi türünü depolar. Ayırıcı parametresi genellikle sınıf bildirimlerinde ve alternatif ayırıcıları değiştirmek için kullanılan önişlem makrolarında atlanır.
Tüm oluşturucular eşlemelerini başlatır.
Tüm oluşturucular, eşlemenin anahtarları arasında bir düzen oluşturmak için kullanılan ve daha sonra çağrılarak key_compdöndürülebilen Traits türünde bir işlev nesnesi depolar.
İlk üç oluşturucu boş bir başlangıç eşlemesi, ikincisi öğelerin sırasını oluştururken kullanılacak karşılaştırma işlevinin türünü (Comp) ve kullanılacak ayırıcı türünü (Al) açıkça belirten üçüncü bir eşleme belirtir. Anahtar sözcük explicit , belirli tür otomatik tür dönüştürmelerini gizler.
Dördüncü oluşturucu, eşlemenin Rightbir kopyasını belirtir.
Beşinci oluşturucu, öğesini taşıyarak Rightharitanın bir kopyasını belirtir.
6., 7. ve 8. oluşturucular, üyeleri kopyalamak için bir initializer_list kullanır.
Sonraki üç oluşturucu, sınıfın [First, Last) ve ayırıcının karşılaştırma işlevinin türünü belirtirken açıklığı artırarak bir eşleme aralığını Traits kopyalar.
Örnek
// map_map.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main()
{
using namespace std;
typedef pair <int, int> Int_Pair;
map <int, int>::iterator m1_Iter, m3_Iter, m4_Iter, m5_Iter, m6_Iter, m7_Iter;
map <int, int, less<int> >::iterator m2_Iter;
// Create an empty map m0 of key type integer
map <int, int> m0;
// Create an empty map m1 with the key comparison
// function of less than, then insert 4 elements
map <int, int, less<int> > m1;
m1.insert(Int_Pair(1, 10));
m1.insert(Int_Pair(2, 20));
m1.insert(Int_Pair(3, 30));
m1.insert(Int_Pair(4, 40));
// Create an empty map m2 with the key comparison
// function of greater than, then insert 2 elements
map <int, int, less<int> > m2;
m2.insert(Int_Pair(1, 10));
m2.insert(Int_Pair(2, 20));
// Create a map m3 with the
// allocator of map m1
map <int, int>::allocator_type m1_Alloc;
m1_Alloc = m1.get_allocator();
map <int, int> m3(less<int>(), m1_Alloc);
m3.insert(Int_Pair(3, 30));
// Create a copy, map m4, of map m1
map <int, int> m4(m1);
// Create a map m5 by copying the range m1[ first, last)
map <int, int>::const_iterator m1_bcIter, m1_ecIter;
m1_bcIter = m1.begin();
m1_ecIter = m1.begin();
m1_ecIter++;
m1_ecIter++;
map <int, int> m5(m1_bcIter, m1_ecIter);
// Create a map m6 by copying the range m4[ first, last)
// and with the allocator of map m2
map <int, int>::allocator_type m2_Alloc;
m2_Alloc = m2.get_allocator();
map <int, int> m6(m4.begin(), ++m4.begin(), less<int>(), m2_Alloc);
cout << "m1 =";
for (auto i : m1)
cout << i.first << " " << i.second << ", ";
cout << endl;
cout << "m2 =";
for(auto i : m2)
cout << i.first << " " << i.second << ", ";
cout << endl;
cout << "m3 =";
for (auto i : m3)
cout << i.first << " " << i.second << ", ";
cout << endl;
cout << "m4 =";
for (auto i : m4)
cout << i.first << " " << i.second << ", ";
cout << endl;
cout << "m5 =";
for (auto i : m5)
cout << i.first << " " << i.second << ", ";
cout << endl;
cout << "m6 =";
for (auto i : m6)
cout << i.first << " " << i.second << ", ";
cout << endl;
// Create a map m7 by moving m5
cout << "m7 =";
map<int, int> m7(move(m5));
for (auto i : m7)
cout << i.first << " " << i.second << ", ";
cout << endl;
// Create a map m8 by copying in an initializer_list
map<int, int> m8{ { { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 } } };
cout << "m8: = ";
for (auto i : m8)
cout << i.first << " " << i.second << ", ";
cout << endl;
// Create a map m9 with an initializer_list and a comparator
map<int, int> m9({ { 5, 5 }, { 6, 6 }, { 7, 7 }, { 8, 8 } }, less<int>());
cout << "m9: = ";
for (auto i : m9)
cout << i.first << " " << i.second << ", ";
cout << endl;
// Create a map m10 with an initializer_list, a comparator, and an allocator
map<int, int> m10({ { 9, 9 }, { 10, 10 }, { 11, 11 }, { 12, 12 } }, less<int>(), m9.get_allocator());
cout << "m10: = ";
for (auto i : m10)
cout << i.first << " " << i.second << ", ";
cout << endl;
}
mapped_type
Bir haritada depolanan verileri temsil eden bir tür.
typedef Type mapped_type;
Açıklamalar
türü mapped_type , sınıfın Tür şablonu parametresinin eş anlamlısıdır.
hakkında Typedaha fazla bilgi için Sınıfmap bakın.
Örnek
nasıl bildirilip kullanılacağına value_typeilişkin bir örnek için mapped_type örneğine bakın.
max_size
Eşlem öğesinin maksimum uzunluğunu döndürür.
size_type max_size() const;
Dönüş Değeri
Haritanın mümkün olan en uzun uzunluğu.
Örnek
// map_max_size.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int> m1;
map <int, int> :: size_type i;
i = m1.max_size( );
cout << "The maximum possible length "
<< "of the map is " << i << "."
<< endl << "(Magnitude is machine specific.)";
}
operator[]
Belirtilen bir anahtar değere sahip bir eşleme bir öğe ekler.
Type& operator[](const Key& key);
Type& operator[](Key&& key);
Parametreler
key
Eklenecek öğenin anahtar değeri.
Dönüş Değeri
Eklenen öğenin veri değerine başvuru.
Açıklamalar
Bağımsız değişken anahtarı değeri bulunamazsa, veri türünün varsayılan değeriyle birlikte eklenir.
operator[]öğesinin değerinin anahtar mdeğeri m[key] = DataValue; ile birlikte olduğu bir DataValue eşlemeye mapped_typekey öğe eklemek için kullanılabilir.
Öğe eklemek için kullanılırken operator[] , döndürülen başvuru eklemenin önceden var olan bir öğeyi değiştirdiğini veya yeni bir öğe oluşturduğunu göstermez. üye işlevleri find ve insert belirtilen anahtara sahip bir öğenin eklemeden önce zaten mevcut olup olmadığını belirlemek için kullanılabilir.
Örnek
// map_op_insert.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
#include <string>
int main( )
{
using namespace std;
typedef pair <const int, int> cInt2Int;
map <int, int> m1;
map <int, int> :: iterator pIter;
// Insert a data value of 10 with a key of 1
// into a map using the operator[] member function
m1[ 1 ] = 10;
// Compare other ways to insert objects into a map
m1.insert ( map <int, int> :: value_type ( 2, 20 ) );
m1.insert ( cInt2Int ( 3, 30 ) );
cout << "The keys of the mapped elements are:";
for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
cout << " " << pIter -> first;
cout << "." << endl;
cout << "The values of the mapped elements are:";
for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
cout << " " << pIter -> second;
cout << "." << endl;
// If the key already exists, operator[]
// changes the value of the datum in the element
m1[ 2 ] = 40;
// operator[] will also insert the value of the data
// type's default constructor if the value is unspecified
m1[5];
cout << "The keys of the mapped elements are now:";
for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
cout << " " << pIter -> first;
cout << "." << endl;
cout << "The values of the mapped elements are now:";
for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
cout << " " << pIter -> second;
cout << "." << endl;
// insert by moving key
map<string, int> c2;
string str("abc");
cout << "c2[move(str)] == " << c2[move(str)] << endl;
cout << "c2["abc"] == " << c2["abc"] << endl;
return (0);
}
The keys of the mapped elements are: 1 2 3.
The values of the mapped elements are: 10 20 30.
The keys of the mapped elements are now: 1 2 3 5.
The values of the mapped elements are now: 10 40 30 0.
c2[move(str)] == 0
c2["abc"] == 1
operator=
Bir eşlemin öğelerini başka bir eşlem kopyasıyla değiştirir.
map& operator=(const map& right);
map& operator=(map&& right);
Parametreler
right
içine map kopyalanan map.
Açıklamalar
içindeki mapoperator= mevcut öğeleri sildikten sonra içeriğini right kopyalayarak veya haritaya taşır.
Örnek
// map_operator_as.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map<int, int> v1, v2, v3;
map<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;
}
pointer
Haritadaki bir öğeye işaretçi sağlayan tür.
typedef typename allocator_type::pointer pointer;
Açıklamalar
Bir öğenin değerini değiştirmek için bir tür pointer kullanılabilir.
Çoğu durumda, iterator bir eşleme nesnesindeki öğelere erişmek için kullanılmalıdır.
rbegin
Ters çevrilmiş bir eşlemedeki ilk öğeyi ele alan bir yineleyici döndürür.
const_reverse_iterator rbegin() const;
reverse_iterator rbegin();
Dönüş Değeri
Ters çevrilmiş bir eşlemedeki ilk öğeyi veya ters çevrilmemiş eşlemedeki son öğeyi ele alan ters çift yönlü yineleyici.
Açıklamalar
rbegin aynı bir haritayla kullanıldığı gibi begin ters çevrilmiş bir haritayla kullanılır.
değerinin dönüş değeri rbegin öğesine const_reverse_iteratoratanmışsa, eşleme nesnesi değiştirilemez. değerinin dönüş değeri rbegin öğesine reverse_iteratoratanmışsa, eşleme nesnesi değiştirilebilir.
rbegin bir haritayı geriye doğru yinelemek için kullanılabilir.
Örnek
// map_rbegin.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int> m1;
map <int, int> :: iterator m1_Iter;
map <int, int> :: reverse_iterator m1_rIter;
map <int, int> :: const_reverse_iterator m1_crIter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 3, 30 ) );
m1_rIter = m1.rbegin( );
cout << "The first element of the reversed map m1 is "
<< m1_rIter -> first << "." << endl;
// begin can be used to start an iteration
// through a map in a forward order
cout << "The map is: ";
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end( ); m1_Iter++)
cout << m1_Iter -> first << " ";
cout << "." << endl;
// rbegin can be used to start an iteration
// through a map in a reverse order
cout << "The reversed map is: ";
for ( m1_rIter = m1.rbegin( ) ; m1_rIter != m1.rend( ); m1_rIter++)
cout << m1_rIter -> first << " ";
cout << "." << endl;
// A map element can be erased by dereferencing to its key
m1_rIter = m1.rbegin( );
m1.erase ( m1_rIter -> first );
m1_rIter = m1.rbegin( );
cout << "After the erasure, the first element "
<< "in the reversed map is "
<< m1_rIter -> first << "." << endl;
}
The first element of the reversed map m1 is 3.
The map is: 1 2 3 .
The reversed map is: 3 2 1 .
After the erasure, the first element in the reversed map is 2.
reference
Bir eşlemede depolanan bir öğeye başvuru sağlayan bir tür.
typedef typename allocator_type::reference reference;
Örnek
// map_reference.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int> m1;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
// Declare and initialize a const_reference &Ref1
// to the key of the first element
const int &Ref1 = ( m1.begin( ) -> first );
// The following line would cause an error because the
// non-const_reference can't be used to access the key
// int &Ref1 = ( m1.begin( ) -> first );
cout << "The key of first element in the map is "
<< Ref1 << "." << endl;
// Declare and initialize a reference &Ref2
// to the data value of the first element
int &Ref2 = ( m1.begin( ) -> second );
cout << "The data value of first element in the map is "
<< Ref2 << "." << endl;
//The non-const_reference can be used to modify the
//data value of the first element
Ref2 = Ref2 + 5;
cout << "The modified data value of first element is "
<< Ref2 << "." << endl;
}
The key of first element in the map is 1.
The data value of first element in the map is 10.
The modified data value of first element is 15.
rend
Ters çevrilmiş bir eşlemedeki son öğeden sonra gelen konumu ele alan bir yineleyici döndürür.
const_reverse_iterator rend() const;
reverse_iterator rend();
Dönüş Değeri
Ters çevrilmiş bir eşlemedeki son öğeden (ters eşlemedeki ilk öğeden önce gelen konum) başarılı konumu ele alan ters çift yönlü yineleyici.
Açıklamalar
rend aynı bir haritayla kullanıldığı gibi end ters çevrilmiş bir haritayla kullanılır.
değerinin dönüş değeri rend öğesine const_reverse_iteratoratanmışsa, eşleme nesnesi değiştirilemez. değerinin dönüş değeri rend öğesine reverse_iteratoratanmışsa, eşleme nesnesi değiştirilebilir.
rend bir ters yineleyicinin haritanın sonuna ulaşıp ulaşmadığını test etmek için kullanılabilir.
tarafından rend döndürülen değer başvurulmamalıdır.
Örnek
// map_rend.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int> m1;
map <int, int> :: iterator m1_Iter;
map <int, int> :: reverse_iterator m1_rIter;
map <int, int> :: const_reverse_iterator m1_crIter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 3, 30 ) );
m1_rIter = m1.rend( );
m1_rIter--;
cout << "The last element of the reversed map m1 is "
<< m1_rIter -> first << "." << endl;
// begin can be used to start an iteration
// through a map in a forward order
cout << "The map is: ";
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end( ); m1_Iter++)
cout << m1_Iter -> first << " ";
cout << "." << endl;
// rbegin can be used to start an iteration
// through a map in a reverse order
cout << "The reversed map is: ";
for ( m1_rIter = m1.rbegin( ) ; m1_rIter != m1.rend( ); m1_rIter++)
cout << m1_rIter -> first << " ";
cout << "." << endl;
// A map element can be erased by dereferencing to its key
m1_rIter = --m1.rend( );
m1.erase ( m1_rIter -> first );
m1_rIter = m1.rend( );
m1_rIter--;
cout << "After the erasure, the last element "
<< "in the reversed map is "
<< m1_rIter -> first << "." << endl;
}
The last element of the reversed map m1 is 1.
The map is: 1 2 3 .
The reversed map is: 3 2 1 .
After the erasure, the last element in the reversed map is 2.
reverse_iterator
Ters çevrilmiş bir haritadaki bir öğeyi okuyabilen veya değiştirebilen çift yönlü bir yineleyici sağlayan tür.
typedef std::reverse_iterator<iterator> reverse_iterator;
Açıklamalar
Bir tür reverse_iterator bir öğenin değerini değiştiremez ve eşlem boyunca tersten yinelemek için kullanılır.
reverse_iterator eşlemesi tarafından tanımlanan öğesi, ilk üyesi öğenin anahtarı olan ve ikinci üyesi öğesi tarafından tutulan eşlenmiş datum olan türündeki value_typenesneleri pair<const Key, Type>olan öğelere işaret eder.
Eşlemedeki bir reverse_iteratoröğeye işaret eden bir rIter başvurusundan yararlanmak için işlecini -> kullanın.
öğesinin anahtarının değerine erişmek için , (* rIter) ile eşdeğer olan ->first kullanın rIter .
önce. öğesinin eşlenmiş veri kümesinin değerine erişmek için -secondrIter> ve bu değer (* rIter) ile eşdeğerdir.
önce.
Örnek
nasıl bildirilip kullanılacağına rbeginilişkin bir örnek için reverse_iterator örneğine bakın.
size
içindeki mapöğe sayısını döndürür.
size_type size() const;
Dönüş Değeri
Haritanın geçerli uzunluğu.
Örnek
Aşağıdaki örnekte üye işlevinin kullanımı gösterilmektedir map::size .
// map_size.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main()
{
using namespace std;
map<int, int> m1, m2;
map<int, int>::size_type i;
typedef pair<int, int> Int_Pair;
m1.insert(Int_Pair(1, 1));
i = m1.size();
cout << "The map length is " << i << "." << endl;
m1.insert(Int_Pair(2, 4));
i = m1.size();
cout << "The map length is now " << i << "." << endl;
}
The map length is 1.
The map length is now 2.
size_type
Bir eşlemedeki öğelerin sayısını temsil eden işaretsiz bir tamsayı türü.
typedef typename allocator_type::size_type size_type;
Örnek
bildirme ve kullanma sizeörneği için size_type örneğine bakın.
değiş tokuş etmek
İki eşlemin öğelerini birbiriyle değiştirir.
void swap(
map<Key, Type, Traits, Allocator>& right);
Parametreler
right
Hedef eşlemeyle değiştirilecek öğeleri sağlayan bağımsız değişken eşlemesi.
Açıklamalar
Üye işlevi, öğeleri değiştirilmekte olan iki eşlemedeki öğeleri belirten hiçbir başvuruyu, işaretçiyi veya yineleyiciyi geçersiz kılmaz.
Örnek
// map_swap.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int> m1, m2, m3;
map <int, int>::iterator m1_Iter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 3, 30 ) );
m2.insert ( Int_Pair ( 10, 100 ) );
m2.insert ( Int_Pair ( 20, 200 ) );
m3.insert ( Int_Pair ( 30, 300 ) );
cout << "The original map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout << "." << endl;
// This is the member function version of swap
//m2 is said to be the argument map; m1 the target map
m1.swap( m2 );
cout << "After swapping with m2, map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout << "." << endl;
// This is the specialized template version of swap
swap( m1, m3 );
cout << "After swapping with m3, map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout << "." << endl;
}
The original map m1 is: 10 20 30.
After swapping with m2, map m1 is: 100 200.
After swapping with m3, map m1 is: 300.
upper_bound
Bir anahtarın belirtilen anahtardan daha büyük bir değere sahip olduğu bir eşlemedeki ilk öğeye bir yineleyici döndürür.
iterator upper_bound(const Key& key);
const_iterator upper_bound(const Key& key) const;
Parametreler
key
Aranmakta olan eşlemedeki bir öğenin sıralama anahtarı değeriyle karşılaştırılacak bağımsız değişken anahtarı değeri.
Dönüş Değeri
Bir iterator öğenin konumunu, bağımsız değişken anahtarından daha büyük bir anahtarla veya anahtar için eşleşme bulunamazsa içindeki son öğeyi const_iterator tamamlayan konumu ele alan bir veyamap.
Dönüş değeri bir const_iteratoröğesine atanmışsa eşleme nesnesi değiştirilemez. Dönüş değeri bir iteratoröğesine atanmışsa, eşleme nesnesi değiştirilebilir.
Örnek
// map_upper_bound.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int> m1;
map <int, int> :: const_iterator m1_AcIter, m1_RcIter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 3, 30 ) );
m1_RcIter = m1.upper_bound( 2 );
cout << "The first element of map m1 with a key "
<< "greater than 2 is: "
<< m1_RcIter -> second << "." << endl;
// If no match is found for the key, end is returned
m1_RcIter = m1. upper_bound ( 4 );
if ( m1_RcIter == m1.end( ) )
cout << "The map m1 doesn't have an element "
<< "with a key greater than 4." << endl;
else
cout << "The element of map m1 with a key > 4 is: "
<< m1_RcIter -> second << "." << endl;
// The element at a specific location in the map can be found
// using a dereferenced iterator addressing the location
m1_AcIter = m1.begin( );
m1_RcIter = m1. upper_bound ( m1_AcIter -> first );
cout << "The 1st element of m1 with a key greater than\n"
<< "that of the initial element of m1 is: "
<< m1_RcIter -> second << "." << endl;
}
The first element of map m1 with a key greater than 2 is: 30.
The map m1 doesn't have an element with a key greater than 4.
The 1st element of m1 with a key greater than
that of the initial element of m1 is: 20.
value_comp
Üye işlevi, anahtar değerlerini karşılaştırarak bir eşlemedeki öğelerin sırasını belirleyen bir işlev nesnesi döndürür.
value_compare value_comp() const;
Dönüş Değeri
Eşlemenin öğelerini sıralamak için kullandığı karşılaştırma işlevi nesnesini döndürür.
Açıklamalar
M eşlemesi için, e1(, burada k1 ve k1 kendi türüne ait anahtarlarıdır ve d1 ve d2 kendi türüne value_type ait verileridir. Depolanan nesne üye işlevini tanımlar
bool operator( value_type& left, value_type& right);
anahtar değeri true önceyse ve sıralama düzenindeki anahtar değerine left eşit değilse döndürürright.
Örnek
// map_value_comp.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int, less<int> > m1;
map <int, int, less<int> >::value_compare vc1 = m1.value_comp( );
pair< map<int,int>::iterator, bool > pr1, pr2;
pr1= m1.insert ( map <int, int> :: value_type ( 1, 10 ) );
pr2= m1.insert ( map <int, int> :: value_type ( 2, 5 ) );
if( vc1( *pr1.first, *pr2.first ) == true )
{
cout << "The element ( 1,10 ) precedes the element ( 2,5 )."
<< endl;
}
else
{
cout << "The element ( 1,10 ) does not precede the element ( 2,5 )."
<< endl;
}
if(vc1( *pr2.first, *pr1.first ) == true )
{
cout << "The element ( 2,5 ) precedes the element ( 1,10 )."
<< endl;
}
else
{
cout << "The element ( 2,5 ) does not precede the element ( 1,10 )."
<< endl;
}
}
The element ( 1,10 ) precedes the element ( 2,5 ).
The element ( 2,5 ) does not precede the element ( 1,10 ).
value_type
Bir eşlemede öğe olarak depolanan nesnenin türü.
typedef pair<const Key, Type> value_type;
Örnek
// map_value_type.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
typedef pair <const int, int> cInt2Int;
map <int, int> m1;
map <int, int> :: key_type key1;
map <int, int> :: mapped_type mapped1;
map <int, int> :: value_type value1;
map <int, int> :: iterator pIter;
// value_type can be used to pass the correct type
// explicitly to avoid implicit type conversion
m1.insert ( map <int, int> :: value_type ( 1, 10 ) );
// Compare other ways to insert objects into a map
m1.insert ( cInt2Int ( 2, 20 ) );
m1[ 3 ] = 30;
// Initializing key1 and mapped1
key1 = ( m1.begin( ) -> first );
mapped1 = ( m1.begin( ) -> second );
cout << "The key of first element in the map is "
<< key1 << "." << endl;
cout << "The data value of first element in the map is "
<< mapped1 << "." << endl;
// The following line would cause an error because
// the value_type isn't assignable
// value1 = cInt2Int ( 4, 40 );
cout << "The keys of the mapped elements are:";
for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
cout << " " << pIter -> first;
cout << "." << endl;
cout << "The values of the mapped elements are:";
for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
cout << " " << pIter -> second;
cout << "." << endl;
}
Ayrıca bkz.
Konteynerler
C++ Standart Kitaplığında İş Parçacığı Güvenliği
C++ Standart Kitaplığı Başvurusu