文章編號: 157593 - 上次校閱: 2005年8月11日 - 版次: 6.1

如何使用 map::max_size 清楚、 橡皮擦大小 STL 函式,在 Visual C++

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。
附註Microsoft Visual C++ NET (2002) 支援.NET Framework 與不受管理的原生 Windows 所提供的兩個受管理的程式碼模型的程式碼模型。在本文資訊適用於不受管理的 Visual C++ 程式碼只。

在此頁中

全部展開 | 全部摺疊

結論

下列範例程式碼,說明如何使用 Visual C++ 中的 [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 符號。

其他相關資訊

所需的標頭

   <map>

原型

   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);
附註類別/參數名稱,在原型中的可能不符合在標頭檔版本。已修改某些改善可讀性。

描述

下面的範例會建立字串 ints 的地圖和它第一次使用填滿的月份數字的月份名稱對應,然後清空並 refills 與對應 ints 週日期名稱的對應。

範例程式碼

////////////////////////////////////////////////////////////////////// 
// 
// 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;

}
程式輸出 (使用者輸入 [] 內所示):
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

?考

如相同需資訊 map::max_size,清除、 清除、 調整大小,請造訪下列 MSDN 網站:
http://msdn.microsoft.com/en-us/library/4w160sd5.aspx (http://msdn.microsoft.com/en-us/library/4w160sd5.aspx)

這篇文章中的資訊適用於:
  • The Standard C++ Library?應用於:
    • 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
關鍵字:?
kbmt kbhowto kbcode kbinfo KB157593 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:157593? (http://support.microsoft.com/kb/157593/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。