DotNet The generic container class under is encapsulated in System.Collections.Generic, Widely used .C++ Depend on STL
Implemented generic containers and algorithms . Here's a comparison , On usage only , Do not delve into the principle . The comparison content has an array , Three structures of linked list and dictionary .

One , array

C# use List<T>,C++ With std::vector<T>, Internal implementations are arrays , It's a continuous memory area , insert , Slow delete operation , Fast random access .

operation

C++(STL)

C#(.net)

explain

contain

#include <vector>

using System.Collections.Generic;

C++ Medium using namespace std;

statement

std::vector<int> array;

List<int> array = new List<int>();

with int Type data as an example

Iterator declaration

std::vector<int>::iterator iter=array.begin();

List<int>.Enumerator iter = array.GetEnumerator();

C# Iterators are not commonly used in

insert

array.push_back(4);
array.insert(iter,51);

array.Add(4);
array.Insert(2, 51);

Failure after iterator operation

query

int val=array[0];
val=array.at(1);  // Perform subscript test

int val = array[0];

array.Contains(5); // Include or not 5
array.IndexOf(1);  // element 1 Subscript of

 

delete

array.pop_back();  // Delete last element
array.erase(iter);  // delete iter Elements of location
array.clear();

array.Remove(1);  // delete "1” This element
array.RemoveAt(0);  // delete 0 Sign element
array.Clear();

Failure after iterator operation

size

array.empty();
array.capacity();  // capacity ( According to the current non allocated memory )
array.size();  // Number of elements

array.Count;  // Number of elements
array.Capacity;  // capacity

 

ergodic

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() It's a function

for (int i = 0; i < array.Count; i++){}
while (iter.MoveNext()){}
foreach (int d in array){}

C++ The second common ,C# The third common ( I think so ……)

sort

std::sort(array.begin(),array.end());

array.Sort();

C++ Middle and important #include <algorithm>, above for_each It's also

 

Two , Linked list

C# use LinkedList<T>,C++ With std::list<T>
, Internal implementations are all linked lists , insert , Fast deletion , Slow random access . The operation of linked list is very similar to that of array , There are different places :

1. Any way of subscript access is invalid , Viewing capacity is also invalid , This is determined by the nature of the list . The query operation of linked list should be carried out through iterator . in other words , In the table above “ query ”,“ ergodic ”
The first way ,“ size ” Capacity in is illegal .

2. Subscript cannot be specified when inserting or deleting ,C++ In addition to push_back() External , One more push_front(),C# Not available in Add(),Insert(), To use
AddBefore()/AddAfter()/AddFirst()/AddLast().

3. Sort in C++ Used directly in list.sort().

4. std::list<T> To add a header file :#include <list>

 

Three , Dictionaries

C# Used in Dictionary<TKey,TValue>,C++ use std::map<TK,TV>.map The internal implementation of red black tree ,Dictionary
The implementation of is a hash table .DotNet Dictionary class structure implemented by tree is also available in , call SortedDictionary
, It doesn't seem to use much , It's not as efficient as a hash table , But it can keep the inserted data in order . The following comparison is to retrieve integers through strings , For the convenience of writing ,C++ Middle string is used directly LPCTSTR, also
typedef std::map<LPCTSTR,int> map_type;

operation

C++(STL)

C#(.net)

explain

contain

#include <map>

using System.Collections.Generic;

 

statement

map_type map;
map_type::iterator iter=map.begin();

Dictionary<string, int> map = new Dictionary<string, int>();

If customized Key type ,C++ Overloaded comparison operator required in ;C# Equal comparator can be customized in

insert

map[_T("first")]=5;
map.insert(map_type::value_type(_T("second"),2));
map.insert(map_type::value_type(_T("second"),3));    // No coverage , No exception

map.Add("first", 5);
map["second"] = 2;  // This operation has been inserted
//map.Add("second", 3);    // Repetitive abnormality

 

delete

map.erase(iter);  //iter Valid and not equal to map.end()
map.erase(_T("first"));
map.clear();

map.Remove("first");
map.Clear();

 

query

int val=map[_T("second")];  // If not, construct a new one
iter=map.find(_T("first"));
if (iter!=map.end())
    val=iter->second;

int data = map["second"];    // Exception if no
map.ContainsKey("third");
map.ContainsValue(5);
map.TryGetValue("hello", out data);

be careful C++ If this element does not exist, no error will be generated during index and subscript retrieval

ergodic

for (iter=map.begin();iter!=map.end();++iter)
{
    // Delete on traversal
    map.erase(iter++);
}

foreach (KeyValuePair<string, int> pair in map)
{

    // Cannot delete

}

 

size

map.size();
map.empty();  // Empty or not

map.Count;

 

  The above is a comparison of three commonly used data structures , Actually, I'm not familiar with these contents , So if there is any mistake, please forgive me .0

Technology
©2019-2020 Toolsou All rights reserved,
It's unexpected Python Cherry tree (turtle The gorgeous style of Library )Unity3D of UGUI Basics -- Three modes of canvas os Simple use of module computer network --- Basic concepts of computer network ( agreement , system )html Writing about cherry trees , Writing about cherry trees Some East 14 Pay change 16 salary , Sincerity or routine ?Unity-Demo Examples ✨ realization UI- Backpack equipment drag function 06【 Interpretation according to the frame 】 Data range filtering -- awesome java Four functional interfaces ( a key , simple )