当前位置:编程学习 > C/C++ >>

Map使用之关联数组

背景:在C++ Standard Library中,Map容器使用pair(Key/Value)存储元素,而且不允许重复元素。如果给定一个Key,存在着唯一的对应的Value,Key与Value存在着某种关联,由此,可将Map作为关联数组使用。
 
 
 
 
现举例如下
 
Test OS: Windows 7
 
Test IDE: Visual Studio 2005
 
Code Example:
 
 
[cpp]  
#include "stdafx.h"   
#include <iostream>   
#include <map>   
#include <string>   
using namespace std;  
  
int _tmain(int argc, _TCHAR* argv[])  
{  
    /* Create map / associative map 
     * - keys are strings 
     * - values are float 
     */  
    typedef map<string, float> StringFloatMap;  
      
    // create empty container   
    StringFloatMap stocks;  
    // insert some elements   
    stocks["TXKG"] = 276.50;  
    stocks["GOOG"] = 700.34;  
    stocks["AAPL"] = 498.26;  
    stocks["BMW"] = 823.23;  
    stocks["NONE"] = 0.72;  
    stocks["NOKIA"] = 12.25;  
  
    // print all elements   
    StringFloatMap::iterator pos;  
    for(pos = stocks.begin(); pos != stocks.end(); ++pos)  
    {  
        cout << "stock:" << pos->first << "\t"  
            << "price:" << pos->second << endl;  
    }  
    cout << endl;  
  
    // all stocks doubled   
    for(pos = stocks.begin(); pos != stocks.end(); ++pos)  
    {  
        pos->second *= 2;  
    }  
  
    // print all elements   
    for(pos = stocks.begin(); pos != stocks.end(); ++pos)  
    {  
        cout << "stock:" << pos->first << "\t"  
            << "price:" << pos->second << endl;  
    }  
    cout << endl;  
  
    // rename key from "AAPL" to "APPLE"   
    stocks["APPLE"] = stocks["AAPL"];  
    stocks.erase("AAPL");  
  
    // print all elements   
    for(pos = stocks.begin(); pos != stocks.end(); ++pos)  
    {  
        cout << "stock:" << pos->first << "\t"  
            << "price:" << pos->second << endl;  
    }  
    cout << endl;  
  
    system("pause");  
    return 0;  
}  
 
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{
/* Create map / associative map
* - keys are strings
* - values are float
*/
typedef map<string, float> StringFloatMap;
 
// create empty container
StringFloatMap stocks;
// insert some elements
stocks["TXKG"] = 276.50;
stocks["GOOG"] = 700.34;
stocks["AAPL"] = 498.26;
stocks["BMW"] = 823.23;
stocks["NONE"] = 0.72;
stocks["NOKIA"] = 12.25;
 
// print all elements
StringFloatMap::iterator pos;
for(pos = stocks.begin(); pos != stocks.end(); ++pos)
{
cout << "stock:" << pos->first << "\t"
<< "price:" << pos->second << endl;
}
cout << endl;
 
// all stocks doubled
for(pos = stocks.begin(); pos != stocks.end(); ++pos)
{
pos->second *= 2;
}
 
// print all elements
for(pos = stocks.begin(); pos != stocks.end(); ++pos)
{
cout << "stock:" << pos->first << "\t"
<< "price:" << pos->second << endl;
}
cout << endl;
 
// rename key from "AAPL" to "APPLE"
stocks["APPLE"] = stocks["AAPL"];
stocks.erase("AAPL");
 
// print all elements
for(pos = stocks.begin(); pos != stocks.end(); ++pos)
{
cout << "stock:" << pos->first << "\t"
<< "price:" << pos->second << endl;
}
cout << endl;
 
system("pause");
return 0;
}
Test Result as follow:
 
 
 
 
 
 
 
可见,使用pair第一元素类型作为下标,寻址到该Key对应的Value的引用,可以很方便地操纵元素。
 
这里需要注意:
 
当使用Key作为下标时,如果Map中已经存在该元素,则返回该元素Value的引用,如果不存在,则会插入一个以下标为Key的元素,并使用Value对应数据类型的Default构造函数初始化Value。即上例中中如果使用语句stocks["abcd"]  = 5.0,则插入Key为“abcd”的元素,并以float 0 初始化,紧接着,将该元素的Value赋值为5.0。
 
 
 
 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,