Makale numarası: 157593 - Son Gözden Geçirme: 11 Ağustos 2005 Perşembe - Gözden geçirme: 6.1

Visual C++ ile Map::max_size, Temizle, silme, boyutu STL işlevlerini nasıl kullanılır

Sistem İpucuBu makale, kullandığınızdan farklı bir işletim sistemine yöneliktir. Sizinle ilgili olmayabilecek makale içeriği devre dışı bırakıldı.
Not Microsoft Visual C++ NET (2002), .NET Framework ve yönetilmeyen yerel Windows tarafından sağlanan her iki yönetilen kod modeli desteklenen kod modeli. Bu makaledeki bilgiler için yönetilmeyen Visual C++ geçerli yalnızca kod.

Bu Sayfada

Hepsini aç | Hepsini kapa

Özet

Aşağıdaki örnek kod, Visual C++ ile map::max_size, map::clear, map::empty, map::erase, map::size, map::operator [], map::end, map::begin, map::find, map::iterator, map::value_type STL simgeleri nasıl kullanılır göstermektedir.

Daha fazla bilgi

Gerekli üstbilgisi

   <map>

Prototipler

   size_type max_size() const;
   void clear() const;
   bool empty() const;
   iterator erase(iterator first, iterator last);
   size_type size() const;
   A::reference operator[](const Key& key);    // A is the allocator
   iterator map::begin();
   iterator map::end();
   iterator map::find(const Key& key);
Not Prototipler sınıfı/parametre adları <a0>Üstbilgi</a0> dosyasındaki sürüm eşleşmiyor olabilir. Bazı Okunabilirliği artırmak için değiştirildi.

Açıklama

Aşağıdaki örnek bir haritasını in dizeleri oluşturur ve ilk olarak ay, Ay adları Haritası ile doldurur sonra boşaltır ve, hafta ve gün adlarını karşılık gelen in Haritası ile refills.

Örnek kod

////////////////////////////////////////////////////////////////////// 
// 
// Compile options needed: None
// 
// <filename> :  main.cpp
// 
// Functions:    iterator map::max_size();
//               void clear() const;
//               bool empty() const;
//               iterator erase(iterator first, iterator last);
//               size_type size() const;
//               A::reference operator[](const Key& key);
//               iterator map::begin();
//               iterator map::end();
//               iterator map::find(const Key& key);
// 
// Written by Rick Troemel
// of Microsoft Product Support Services,
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
////////////////////////////////////////////////////////////////////// 

#pragma warning(disable:4786)

#include <iostream>
#include <string>
#include <map>

#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
   #endif

typedef map<string, int, less<string>, allocator<int> > STRING2INT;

void main()
{
    STRING2INT MyMap;
    STRING2INT::iterator MyIterator;
    string MyBuffer;

    // print the maximum number of <key,data> pairs that MyMap can hold
    cout << "MyMap is capable of holding " << MyMap.max_size()
         << " <string,int> pairs" << endl;

    if(!MyMap.empty())
        cout << "MyMap has " << MyMap.size() << " entries" << endl;
    else
        cout << "MyMap is empty" << endl;
        cout << "Entering new entries in MyMap" << endl;
    // Fill in MyMap with the months of the year, mapped to their number
    // January - 1, February - 2, etc. using operator[].
    MyMap["January"] = 1;
    MyMap["February"] = 2;
    MyMap["March"] = 3;
    MyMap["April"] = 4;
    MyMap["May"] = 5;
    MyMap["June"] = 6;
    MyMap["July"] = 7;
    MyMap["August"] = 8;
    MyMap["September"] = 9;
    MyMap["October"] = 10;
    MyMap["November"] = 11;
    MyMap["December"] = 12;

    if(!MyMap.empty())
        cout << "MyMap has " << MyMap.size() << " entries" << endl;
    else
        cout << "MyMap is empty" << endl;

    // Ask the user for a month of the year and print the number
    // that corresponds to the month entered
   MyIterator = MyMap.end();
    while(MyIterator == MyMap.end()){
        cout << "Enter a Month :";
        cin >> MyBuffer;
        if((MyIterator = MyMap.find(MyBuffer)) != MyMap.end())
            cout << (*MyIterator).first << " is Month Number "
                 << (*MyIterator).second << endl;
        else
            cout << "Enter a Valid Month (example: March)" << endl;
    }

    // empty MyMap - note that clear simply calls erase(begin(),end());
    MyMap.clear();

    if(!MyMap.empty())
        cout << "MyMap has " << MyMap.size() << " entries" << endl;
    else
        cout << "MyMap is empty" << endl;
        cout << "Entering new entries in MyMap" << endl;
    // Fill MyMap with the days of the week, each mapped to an int
    MyMap["Monday"] = 1;
    MyMap["Tuesday"] = 2;
    MyMap["Wednesday"] = 3;
    MyMap["Thursday"] = 4;
    MyMap["Friday"] = 5;
    MyMap["Saturday"] = 6;
    MyMap["Sunday"] = 7;

    if(!MyMap.empty())
        cout << "MyMap has " << MyMap.size() << " entries" << endl;
    else
        cout << "MyMap is empty" << endl;

    // Ask the user for a day of the week and print the number
    // that corresponds to the day entered
    MyIterator = MyMap.end();
    while(MyIterator == MyMap.end()){
        cout << "Enter a Day of the Week:";
        cin >> MyBuffer;
        if((MyIterator = MyMap.find(MyBuffer)) != MyMap.end())
            cout << (*MyIterator).first << " is Day Number "
                 << (*MyIterator).second << endl;
        else
            cout <<"Enter a Valid Day of the Week(example: Monday)"<< endl;
    }
// Now clear MyMap again - this time using erase instead of clear
    MyMap.erase(MyMap.begin(), MyMap.end());

    if(!MyMap.empty())
        cout << "MyMap has " << MyMap.size() << " entries" << endl;
    else
        cout << "MyMap is empty" << endl;

}
Çıktı program (user [] içinde gösterilen girileceğini):
MyMap is capable of holding 1073741823 <string,int> pairs
MyMap is empty
Entering new entries in MyMap
MyMap has 12 entries
Enter a Month :[April]
April is Month Number 4
MyMap is empty
Entering new entries in MyMap
MyMap has 7 entries
Enter a Day of the Week:[Friday]
Enter a Valid Day of the Week(example: Monday)
Enter a Day of the Week:[Friday]
Friday is Day Number 5
MyMap is empty

Referanslar

Map::max_size aynı bilgi, Temizle, silme, boyut, aşağıdaki MSDN Web sitesini ziyaret edin:
http://msdn.microsoft.com/en-us/library/4w160sd5.aspx (http://msdn.microsoft.com/en-us/library/4w160sd5.aspx)

Bu makaledeki bilginin uygulandığı durum:
  • The Standard C++ Library, Ne zaman ne ile kullanilir:
    • Microsoft Visual C++ 4.2 Enterprise Edition
    • Microsoft Visual C++ 5.0 Enterprise Edition
    • Microsoft Visual C++ 6.0 Enterprise Edition
    • Microsoft Visual C++ 4.2 Professional Edition
    • Microsoft Visual C++ 5.0 Professional Edition
    • Microsoft Visual C++ 6.0 Professional Edition
    • Microsoft Visual C++, 32-bit Learning Edition 6.0
    • Microsoft Visual C++ .NET 2002 Standard Edition
    • Microsoft Visual C++ .NET 2003 Standard Edition
Anahtar Kelimeler: 
kbmt kbhowto kbcode kbinfo KB157593 KbMttr
Otomatik TercümeOtomatik Tercüme
ÖNEMLİ: Bu makale, bir kişi tarafından çevrilmek yerine, Microsoft makine-çevirisi yazılımı ile çevrilmiştir. Microsoft size hem kişiler tarafından çevrilmiş, hem de makine-çevrisi ile çevrilmiş makaleler sunar. Böylelikle, bilgi bankamızdaki tüm makalelere, kendi dilinizde ulaşmış olursunuz. Bununla birlikte, makine tarafından çevrilmiş makaleler mükemmel değildir. Bir yabancının sizin dilinizde konuşurken yapabileceği hatalar gibi, makale; kelime dağarcığı, söz dizim kuralları veya dil bilgisi açısından yanlışlar içerebilir. Microsoft, içeriğin yanlış çevrimi veya onun müşteri tarafından kullanımından doğan; kusur, hata veya zarardan sorumlu değildir. Microsoft ayrıca makine çevirisi yazılımını sıkça güncellemektedir.
Makalenin İngilizcesi aşağıdaki gibidir:157593  (http://support.microsoft.com/kb/157593/en-us/ )