C++与C#对常用数据结构封装的对比(STL vs System.Collections.Generic)
来源:互联网 发布:淘宝预售如何合并付款 编辑:程序博客网 时间:2024/06/05 14:52
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); //进行下标检测
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){}
排序
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();
插入
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);
遍历
for (iter=map.begin();iter!=map.end();++iter)
{
//遍历时删除
map.erase(iter++);
}
foreach (KeyValuePair<string, int> pair in map)
{
//不能进行删除操作
}
大小
map.size();
map.empty(); //是否为空
以上便是三种常用的数据结构的用法对比,其实我对这些内容不熟悉,所以有错误的地方请见谅。
转自:http://www.cnblogs.com/cocoaleaves/archive/2009/05/30/1492269.html
- C++与C#对常用数据结构封装的对比(STL vs System.Collections.Generic)
- C++与C#对常用数据结构封装的对比(STL vs System.Collections.Generic)
- C#学习之System.Collections.Generic 与 System.Collections 记录
- System.Collections.Generic
- ForEach :对 System.Collections.Generic.List<T> 的每个元素执行指定操作
- 《程序员的第一年》---------- C#中System.Collections.Generic.SortedDictionary 的使用
- C#泛型集合 using System.Collections.Generic
- C#泛型集合 using System.Collections.Generic
- C#泛型集合 using System.Collections.Generic
- System.Collections、System.Collections.Generic、System.Collections.Specialized
- System.Collections.Generic 命名空间
- System.Collections.Generic命名空间
- System.Collections.Generic 命名空间
- System.Collections.Generic 命名空间
- ASP.Net 之 System.Collections.Generic 下的容器类
- 定义泛型集合的命名空间:System.Collections.Generic
- C++/CLI与C#常用语法对比
- C# 常用数据结构简单总结(与C++对比)
- Clone Graph
- 用Animation-list实现逐帧动画
- vim使用技巧
- 像QQ一样输入表情
- linux基本命令学习(2)--find
- C++与C#对常用数据结构封装的对比(STL vs System.Collections.Generic)
- 诡异的 "密码取回" 邮件问题
- js 继承
- 【甘道夫】Hadoop2.4.1尝鲜部署+完整版配置文件
- POJ_3190_Stall Reservations
- PAT数组-11. 猴子选大王(20)
- EJB学习笔记01,了解企业级开发!
- linux虚拟机上网配置
- jQuery源码分析——jQuery对象怎么来的