DotNet下的泛型容器類封裝在System.Collections.Generic,使用的十分廣泛。C++則靠STL實現(xiàn)了泛型容器與算法。下面對二者做一個對比,只談用法,不深究原理。對比的內容有數(shù)組、鏈表和字典三種結構。 一、數(shù)組 C#使用List<T>,C++用的是std::vector<T>,內部實現(xiàn)都是數(shù)組,也就是一塊連續(xù)的內存區(qū)域,插入、刪除操作慢,隨機訪問速度快。 操作 | C++(STL) | C#(.net) | 說明 | 包含 | #include <vector> | using System.Collections.Generic; | C++中也可以using namespace std; | 聲明 | std::vector<int> array; | List<int> array = new List<int>(); | 以int型數(shù)據為例 | 迭代器聲明 | std::vector<int>::iterator iter=array.begin(); | List<int>.Enumerator iter = array.GetEnumerator(); | C#中迭代器不常用 | 插入 | array.push_back(4); array.insert(iter,51); | array.Add(4); array.Insert(2, 51); | 迭代器操作后失效 | 查詢 | int val=array[0]; val=array.at(1); //進行下標檢測 | int val = array[0]; array.Contains(5); //是否包含5 array.IndexOf(1); //元素1的下標 | | 刪除 | array.pop_back(); //刪除最后的元素 array.erase(iter); //刪除iter位置的元素 array.clear(); | array.Remove(1); //刪除"1”這個元素 array.RemoveAt(0); //刪除0號元素 array.Clear(); | 迭代器操作后失效 | 大小 | array.empty(); array.capacity(); //容量(根據當前非配的內存) array.size(); //元素數(shù) | array.Count; //元素數(shù) array.Capacity; //容量 | | 遍歷 | for (std::vector<int>::size_type i=0;i<array.size();++i){} for (iter=array.begin();iter!=array.end();iter++){} for_each(array.begin(),array.end(),func); //func()是函數(shù) | for (int i = 0; i < array.Count; i++){} while (iter.MoveNext()){} foreach (int d in array){} | C++中第二種常用,C#中第三種常用(我是這么覺得……) | 排序 | std::sort(array.begin(),array.end()); | array.Sort(); | C++中要#include <algorithm>,上面的for_each也是 |
二、鏈表 C#使用LinkedList<T>,C++用的是std::list<T>,內部實現(xiàn)都是鏈表,插入、刪除速度快,隨機訪問速度慢。鏈表的操作與數(shù)組十分相似,不同的地方大致有: 1. 任何通過下標訪問的方式都是無效的,查看容量也是無效的,這是由鏈表的性質決定的。對鏈表的查詢操作要通過迭代器進行。也就是說,上表中“查詢”、“遍歷”的第一種方法、“大小”中的容量都是非法操作。 2. 插入刪除的時候也不能指定下標,C++中除了push_back()外,多了一個push_front(),C#中不能使用Add()、Insert(),要使用AddBefore()/AddAfter()/AddFirst()/AddLast()。 3. 排序在C++中直接用list.sort()。 4. std::list<T>要加頭文件:#include <list> 三、字典 C#中使用Dictionary<TKey,TValue>,C++使用std::map<TK,TV>。map的內部實現(xiàn)是紅黑樹,Dictionary的實現(xiàn)是哈希表。DotNet中也有用樹實現(xiàn)的字典類結構,叫SortedDictionary,似乎用得不多,效率也沒有哈希表高,不過可以保持插入的數(shù)據是有序的。下面的對比是通過字符串來檢索整數(shù),為了寫起來方便,C++中字符串直接用了LPCTSTR,并且typedef std::map<LPCTSTR,int> map_type; 操作 | C++(STL) | C#(.net) | 說明 | 包含 | #include <map> | using System.Collections.Generic; | | 聲明 | map_type map; map_type::iterator iter=map.begin(); | Dictionary<string, int> map = new Dictionary<string, int>(); | 如果是自定義的Key類型,C++中需要重載比較運算符;C#中可自定義相等比較器 | 插入 | map[_T("first")]=5; map.insert(map_type::value_type(_T("second"),2)); map.insert(map_type::value_type(_T("second"),3)); //不覆蓋,不異常 | map.Add("first", 5); map["second"] = 2; //這種操作已經進行了插入 //map.Add("second", 3); //重復異常 | | 刪除 | map.erase(iter); //iter有效且不等于map.end() map.erase(_T("first")); map.clear(); | map.Remove("first"); map.Clear(); | | 查詢 | int val=map[_T("second")]; //沒有則構造新的 iter=map.find(_T("first")); if (iter!=map.end()) val=iter->second; | int data = map["second"]; //不存在則異常 map.ContainsKey("third"); map.ContainsValue(5); map.TryGetValue("hello", out data); | 注意C++中下標檢索時如果不存在這個元素也不產生錯誤 | 遍歷 | for (iter=map.begin();iter!=map.end();++iter) { //遍歷時刪除 map.erase(iter++); } | foreach (KeyValuePair<string, int> pair in map) { //不能進行刪除操作 } | | 大小 | map.size(); map.empty(); //是否為空 | map.Count; | |
以上便是三種常用的數(shù)據結構的用法對比,其實我對這些內容不熟悉。
|