【原】C++与C#对常用数据结构封装的对比,STL vs System.Collections.Generic

DotNet下的泛型容器类封装在System.Collections.Generic,使用的十分广泛。C++则靠STL实现了泛型容器与算法。下面对二者做一个对比,只谈用法,不深究原理。对比的内容有数组、链表和字典三种结构。

一、数组

C#使用List<T>,C++用的是std::vector<T>,内部实现都是数组,也就是一块连续的内存区域,插入、删除操作慢,随机访问速度快。

操作

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型数据为例

迭代器声明

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(); //元素数

array.Count; //元素数

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()是函数

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>,内部实现都是链表,插入、删除速度快,随机访问速度慢。链表的操作与数组十分相似,不同的地方大致有:

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的内部实现是红黑树,Dictionary的实现是哈希表。DotNet中也有用树实现的字典类结构,叫SortedDictionary,似乎用得不多,效率也没有哈希表高,不过可以保持插入的数据是有序的。下面的对比是通过字符串来检索整数,为了写起来方便,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;

以上便是三种常用的数据结构的用法对比,其实我对这些内容不熟悉,所以有错误的地方请见谅。