来自MSDN, MAP, 映射, 可以将任意的类型进行 二维的映射,是非常有用的。 比如我们可以通过索引来查找东西。
可以通过 名字查找 路径, 可以通过 任何东西 查找到 另一个东西。
//下面再给出常见的根据字符串查字符串的方法。
#include <string>
#include <map>
using namespace std;
int main(int argc,char** argv)
{
typedef pir<string,string> tpair;
map<string,string> tmap;
tmap.insert(tpair("IBM","Microsoft"));
tmap.insert(tpair("Internatial Busines", "Intel"));
这样,我们就可以根据 tmap[string("IBM")] 来查找到Microsoft, 是不是很好呢?
}
// map_insert.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int>::iterator m1_pIter, m2_pIter;
map <int, int> m1, m2;
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.insert ( Int_Pair ( 4, 40 ) );
cout << "The original key values of m1 =";
for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
cout << " " << m1_pIter -> first;
cout << "." << endl;
cout << "The original mapped values of m1 =";
for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
cout << " " << m1_pIter -> second;
cout << "." << endl;
pair< map<int,int>::iterator, bool > pr;
pr = m1.insert ( Int_Pair ( 1, 10 ) );
if( pr.second == true )
{
cout << "The element 10 was inserted in m1 successfully." << endl;
}
else
{
cout << "The element 10 already exists in m1/n"
<< "with a key value of ( (pr.first) -> first ) = "
<< ( pr.first ) -> first
<< "." << endl;
}
// The hint version of insert
m1.insert( --m1.end( ), Int_Pair ( 5, 50 ) );
cout << "After the insertions, the key values of m1 =";
for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
cout << " " << m1_pIter -> first;
cout << "," << endl;
cout << "and the mapped values of m1 =";
for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
cout << " " << m1_pIter -> second;
cout << "." << endl;
m2.insert ( Int_Pair ( 10, 100 ) );
// The templatized version inserting a range
m2.insert( ++m1.begin( ), --m1.end( ) );
cout << "After the insertions, the key values of m2 =";
for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
cout << " " << m2_pIter -> first;
cout << "," << endl;
cout << "and the mapped values of m2 =";
for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
cout << " " << m2_pIter -> second;
cout << "." << endl;
}