我定义了一个map
#include <string>
#include <iostream>
#include <map>
using namespace std;
void main()
{
map<string,string> MyMap;// 第一个string是key,第二个string是value
...
}
问题是:
1) 怎么往 MyMap 里面添加
元素
?
2) 怎么删除 MyMap 里面已有的元素?
3) 怎么知道 MyMap 的元素个数?
4) 如何根据键(key)查找相应的值(value)?
如设:mymap[]
5) 如何根据值(value)查找相应的键(key)?
6) 如何根据序号索引来查找相应的键key 和 对应的值 value ?
7) 如何为map分配新的空间?是否是每添加一个元素就必须先为它申请空间?
以上都是关于map使用的一些基本问题,请大家讨论一下吧。 问题点数:50、回复次数:7Top
1 楼B2China(海陆空天电磁)回复于 2004-09-02 15:45:30 得分 0
4) 例如:如果有一个元素的key ="key1", 要查找它的value 是多少?
5) 例如:如果有一个元素的value ="value1", 要查找它的key 是多少?
Top
2 楼maxcode(人弋石马)回复于 2004-09-02 16:01:18 得分 20
问题太多了
看看这个也许会有用
Using a Map as an Associative Array
The following example shows the use of a map as an associative array. The map is used as a stock chart. The elements of the map are pairs in which the key is the name of the stock and the value is its price:
// cont/mapl.cpp
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
/*create map/associative array
*-keys are strings
*-values are floats
*/
typedef map<string,float> StringFloatMap;
StringFloatMap stocks; // create empty container
//insert some elements
stocks["BASF"] = 369.50;
stocks["VW"] = 413.50;
stocks["Daimler"] = 819.00;
stocks["BMW"] = 834.00;
stocks["Siemens"] = 842.20;
//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;
//boom (all prices 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 "VW" to "Volkswagen"
*-only provided by exchanging element
*/
stocks["Volkswagen"] = stocks["VW"];
stocks.erase("VW");
//print all elements
for (pos = stocks.begin(); pos != stocks.end(); ++pos) {
cout << "stock: " << pos->first << "/t"
<< "price: " << pos->second << endl;
}
}
The program has the following output:
stock: BASF price: 369.5
stock: BMW price: 834
stock: Daimler price: 819
stock: Siemens price: 842.2
stock: VW price: 413.5
stock: BASF price: 739
stock: BMW price: 1668
stock: Daimler price: 1638
stock: Siemens price: 1684.4
stock: VW price: 827
stock: BASF price: 739
stock: BMW price: 1668
stock: Daimler price: 1638
stock: Siemens price: 1684.4
stock: Volkswagen price: 827
Using a Multimap as a Dictionary
The following example shows how to use a multimap as a dictionary:
// cont/mmap1.cpp
#include <iostream>
#include <map>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//define multimap type as string/string dictionary
typedef multimap<string,string> StrStrMMap;
//create empty dictionary
StrStrMMap dict;
//insert some elements in random order
dict.insert(make_pair("day","Tag"));
dict.insert(make_pair("strange","fremd"));
dict.insert(make_pair("car","Auto"));
dict.insert(make_pair("smart","elegant"));
dict.insert(make_pair("trait","Merkmal"));
dict.insert(make_pair("strange","seltsam"));
dict.insert(make_pair("smart","raffiniert"));
dict.insert(make_pair("smart","klug"));
dict.insert(make_pair("clever","raffiniert"));
//print all elements
StrStrMMap::iterator pos;
cout.setf (ios::left, ios::adjustfield);
cout << ' ' << setw(10) << "english "
<< "german " << endl;
cout << setfil('-') << setw(20) << ""
<< setfil(' ') << endl;
for (pos = dict.begin(); pos != dict.end(); ++pos) {
cout << ' ' << setw(10) << pos>first.c_str()
<< pos->second << endl;
}
cout << endl;
//print all values for key "smart"
string word("smart");
cout << word << ": " << endl;
for (pos = dict.lower_bound(word);
pos != dict.upper_bound(word); ++pos) {
cout << " " << pos->second << endl;
}
//print all keys for value "raffiniert"
word = ("raffiniert");
cout << word << ": " << endl;
for (pos = dict.begin(); pos != dict.end(); ++pos) {
if (pos->second == word) {
cout << " " << pos->first << endl;
}
}
}
The program has the following output:
english german
--------------------
car Auto
clever raffiniert
day Tag
smart elegant
smart raffiniert
smart klug
strange fremd
strange seltsam
trait Merkmal
smart:
elegant
raffiniert
klug
raffiniert:
clever
smart
Top
3 楼B2China(海陆空天电磁)回复于 2004-09-02 16:31:47 得分 0
//定义
map<int, string> mykey; //第一个是Key,第二个是Value
//插入
mykey.insert(map<int, string>::value_type(1, "hello"));
//访问
cout << mykey[1] << endl; //方括号里面是Key的值,如果Key是字符串,加引号就好了
//叠代
mykey::iterator it;
for (it = mykey.begin(); it! = mykey.end(); ++it)
cout << "Key: " << it->first << "/tValue: " << it->second;
//删除
mykey::iterator it1;
for (it1 = mykey.begin(); it1! = mykey.end(); ++it1)
mykey.erase(it1);
我现在不知道怎么查找。根据key查找value或根据value查找key,或根据索引查找Top
4 楼Rave(胡言)回复于 2004-09-02 17:10:50 得分 10
[quote]
//删除
mykey::iterator it1;
for (it1 = mykey.begin(); it1! = mykey.end(); ++it1)
mykey.erase(it1);
[/quote]
对it1所指元素实施erase(), 会
导致
it1无效, 不再是一个合法的iterator
删除要特别小心.
这样改:
mykey::iterator it1;
for (it1 = mykey.begin(); it1! = mykey.end();)
mykey.erase(it1++);
it1++ 会将 it1移向下一个元素,但返回其原始值的一个副本, 所以不用担心it1是否合法.
Top
5 楼pc2s(火麒麟)回复于 2004-09-02 21:28:55 得分 0
to:B2China(海陆空天电磁) ( ) 信誉:100
从我的回复里面粘过来~~~~~~~
//查找Map中是否存在inKey这个Key
for (map<int, string>::iterator it = mykey.begin(); it!=mykey.end(); ++it)
if (it->first == inKey) cout << "Found" << endl;
查找Value,就把it->first换成it->second就可以了Top
6 楼pc2s(火麒麟)回复于 2004-09-02 21:29:52 得分 10
to:B2China(海陆空天电磁) ( ) 信誉:100
我的意思是,居然从我给别人的回复中粘贴过来回复别人~~~~
还一点儿都不改动Top
7 楼daylove(爱晶如梦)(昨夜西风调碧树,独上高楼,望尽天涯路……)回复于 2004-09-03 10:04:21 得分 10
建议看看侯捷的:Effective C++ 2/e
你会豁然开朗。
------------------------------------------------------《爱晶如梦》
『唯我独坐寒江』
『难为春城美景』
『秋黛依影随行』
『紫晶轻舞我心』