[C++/object c]_[初級]_[std::map容器的使用總結和NSDictionary詞典使用總結]
阿新 • • 發佈:2019-02-19
map容器
場景:map的元素是一對對的“關鍵字―值”組合,“關鍵字”用於搜尋,而“值”用來表示我們要存取的資料。
在map容器中,每個關鍵字只能出現一次,不能重複
multimapvoid TestMap() { typedef map<int,string> gAAMap; gAAMap m; cout << "map ========="<< endl; m.insert(pair<int,string>(1,"aaaaa")); m.insert(pair<int,string>(2,"bbbbb")); m.insert(pair<int,string>(1,"ccccc")); m.insert(pair<int,string>(3,"ddddd")); cout <<"輸出m中的元素個數:"<< m.size() << endl; gAAMap::iterator it = m.begin(); while (it != m.end()) //按順序逐個輸出gAAMap中的元素 { cout << (*it).first << ": " << (*it).second << endl; it++; } pair<gAAMap::iterator,gAAMap::iterator> values = m.equal_range(1); //關鍵字查詢 gAAMap::iterator b = values.first; while(b != values.second) { cout << "map equal_range: " << (*b).second << endl; //獲取key對應的值,只有一個 ++b; } }
場景:multimap與map不同之處在於其允許出現相同的元素。
multimap容器的使用: 關鍵字允許重複,按照關鍵字排序,通常在一對多的關係中使用。如一個學生可以選多門課
執行結果:void TestMultMap() { pair<int,string> t(9,"Asia"); cout << t.first << " " << t.second << endl ; typedef multimap<int,string> gAAMultiMap; gAAMultiMap mmap; cout << "Multmap ========="<< endl; mmap.insert(pair<int,string>(1,"aaaaa")); mmap.insert(pair<int,string>(2,"bbbbb")); mmap.insert(pair<int,string>(1,"ccccc")); mmap.insert(pair<int,string>(3,"ddddd")); cout <<"輸出mmap中的元素個數:"<< mmap.size() << endl; gAAMultiMap::iterator it = mmap.begin(); while (it != mmap.end()) //按順序逐個輸出gAAMultiMap中的元素 { cout << (*it).first << ": " << (*it).second << endl; it++; } pair<gAAMultiMap::iterator,gAAMultiMap::iterator> values = mmap.equal_range(1); <span style="font-family: Arial, Helvetica, sans-serif;">//關鍵字查詢</span> gAAMultiMap::iterator b = values.first; while(b != values.second) { cout << "multimap equal_range: " << (*b).second << endl; <span style="font-family: Arial, Helvetica, sans-serif;">//獲取key對應的值,值可以有多個</span> ++b; } }
結論:map和multimap相對比,map只能是一對一的關係,multimap可以是一對多的關係。
NSDictionary
場景:建立不可變詞典,靜態定義key和value。
void TestDictionary()
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
//初始化詞典的兩種方式。 //[NSDictionary dictionaryWithObjectsAndKeys:..] : 使用鍵值對兒直接建立詞典物件,結尾必需使用nil標誌結束。 //[NSDictionary initWithObjectsAndKeys:..] :使用鍵值對兒初始化詞典物件,結尾必需使用nil標誌結束。 NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"aaaaa",@"1",@"bbbbb",@"2", @"ccccc",@"1",@"ddddd",@"3",nil]; //得到詞典的數量 int count = [dictionary count]; NSLog(@"詞典的數量為: %d",count); NSLog(@"詞典的所有關鍵字和值為: %@",dictionary); //得到詞典中所有KEY值 NSEnumerator * enumeratorKey = [dictionary keyEnumerator]; //快速列舉遍歷所有KEY的值 //for (NSObject *object in enumeratorKey) { // NSLog(@"遍歷KEY的值: %@",object); //} //得到詞典中所有Value值 NSEnumerator * enumeratorValue = [dictionary objectEnumerator]; //快速列舉遍歷所有Value的值 //通過KEY找到value NSObject *object = [dictionary objectForKey:@"1"]; if (object != nil) { NSLog(@"通過KEY找到的value是: %@",object); } [pool drain]; }
NSMutableDictionary
場景:建立可變詞典,動態新增key和value,可以動態刪除詞典裡面的資料
void TestMutableDictionary()
{
NSAutoreleasePool *pool =[NSAutoreleasePool new];
NSMutableDictionary *dictionary = [NSMutableDictionary new];
[dictionary setObject:@"aaaaa" forKey:@"1"];
[dictionary setObject:@"bbbbb" forKey:@"2"];
[dictionary setObject:@"ccccc" forKey:@"1"];
[dictionary setObject:@"ddddd" forKey:@"3"];
NSLog(@"詞典的所有關鍵字和值為: %@",dictionary);
//通過KEY找到value
NSObject *object = [dictionary objectForKey:@"1"];
if (object != nil) {
NSLog(@"通過KEY找到的value是: %@",object);
}
[dictionary removeObjectForKey:@"2"] ;//刪除掉詞典中指定KEY的資料 。
NSLog(@"詞典剩餘後的關鍵字和值為: %@",dictionary);
[dictionary removeAllObjects];// 刪除掉詞典中的所有資料。
NSLog(@"詞典刪除所有關鍵字和值為: %@",dictionary);
[dictionary release];
[pool drain];
}
執行結果:
結論: NSDictionary 關鍵字不允許重複,並且key相同的,只把最後相同的一個key對應的value的值存入詞典中。