最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實現(xiàn)的泛型List類分享

 更新時間:2014年07月16日 09:19:33   投稿:junjie  
這篇文章主要介紹了C++實現(xiàn)的泛型List類分享,參考C#的List功能實現(xiàn),需要的朋友可以參考下

額,不要說我三心二意:一邊在看.NET和CLR的原理、一邊在看JavaScript、一邊在看Java;有時看算法有時看Unity、Hibernate;有時看Hadoop有時看Redis;現(xiàn)在又開始看C++了。

以前覺得無論什么語言嘛,其實都差不多,核心思想基本一致?,F(xiàn)在又不這么想了,其實語言的選擇對軟件的性能、可靠性、開發(fā)成本之類的關(guān)系很大,所以覺得還是要多接觸一些比較核心的東西——那么自然是C++了。以前在學校學的C++完全是醬油,太水了基本沒啥用,用來用去和C差不多,所以現(xiàn)在要自己學啦。

廢話不說了,第一個任務就是山寨.NET類庫里面的List<T>泛型類,還偷看過它的源代碼。那么現(xiàn)在就開始用C++進行“山寨”吧。(所以這個類的名字就是ListSZ,SZ=山寨,不是“單維零下標”數(shù)組。)

當然剛?cè)胧诌€是碰了不少釘子,最主要的是模版的實現(xiàn)為啥不支持放在cpp里?。扛愕梦艺垓v了老半天。(感謝KC提供技術(shù)支持,所以KC要趕快請我吃飯)

主要實現(xiàn)了如下功能:

1.自動擴容(直接抄的List的實現(xiàn)方式,容量不夠時翻倍,但InsertRange的時候除外);
2.Add添加到末尾,AddRange在末尾添加多個,Insert在中間插入一個或多個;
3.Remove刪除最后一個或其中一個,RemoveRange刪除其中一片。

MakeRoom是在中間生成一片空的區(qū)域,原來的元素全往后移。EnsureCapacity在容量不夠時擴容……

直接貼代碼:

#include <stdexcept>
#include "stdafx.h"
#include <algorithm>

#pragma once
template <typename T> class ListSZ
{
private:
 T* _mArray;
 int _capacity;
 int _elementCount;

 const int DEFAULT_CAPACITY = 8;

 void EnsureCapacity(int newCount)
 {
 if ((__int64) _elementCount + newCount >= INT_MAX)
  throw std::out_of_range("ListSZ supports up to 2^31 - 1 elements.");

 //If _elementCount = _capacity - 1, the buffer is full
 if (_elementCount + newCount > _capacity)
 {

  int new_capacity = _capacity >= INT_MAX / 2 ? INT_MAX : _capacity << 1;

  if (new_capacity < _elementCount + newCount)
  new_capacity = std::min((__int64) INT_MAX, (__int64) (_elementCount + newCount) << 1);

  /*if (new_capacity < _elementCount + newCount)
  new_capacity = ((__int64) (_elementCount + newCount) << 1) >= INT_MAX ? INT_MAX, (_elementCount + newCount) << 1;
*/
  T* new_buffer = new T[new_capacity];
  memcpy(new_buffer, _mArray, sizeof(T) * _elementCount);

  delete [] _mArray;

  _mArray = new_buffer;
  _capacity = new_capacity;
 }
 }

 void MakeRoom(int index, int count)
 {
 if (index >= _elementCount - 1) return;

 EnsureCapacity(count);

 int move_count = _elementCount - index;

 memmove(_mArray + index + count, _mArray + index, move_count * sizeof(T));
 memset(_mArray + index, 0, count * sizeof(T));

 }

public:
 ListSZ() : ListSZ(DEFAULT_CAPACITY){};

 ListSZ(int capacity)
 {
 if (capacity <= 0)
  throw std::invalid_argument("The capacity of the list cannot be less than 1.");

 _capacity = capacity;

 _mArray = new T[_capacity];
 //_mArray = (T*) malloc(sizeof(T) * _capacity);
 memset(_mArray, 0, _capacity * sizeof(T));
 }

 ListSZ(const T* source, int elementCount) : ListSZ(elementCount)
 {
 Insert(source, 0, elementCount, 0);
 }

 ~ListSZ()
 {
 delete [] _mArray;
 }

 T GetElement(int index)
 {
 if (index < 0 || index >= _elementCount)
  throw std::invalid_argument("The index is outsize of the boundary of list.");

 return *(_mArray + index);
 }

 void Add(T value)
 {
 EnsureCapacity(1);

 *(_mArray + _elementCount) = value;
 _elementCount++;
 }

 void AddRange(T* source, int count)
 {
 Insert(source, 0, count, _elementCount);
 }

 void Insert(T value, int index)
 {
 if (index < 0 || index >= _elementCount)
  throw std::invalid_argument("The index is outsize of the boundary of list.");

 MakeRoom(index, 1);

 *(_mArray + index) = value;
 _elementCount++;
 }

 void Insert(const T* source, int elementCount, int insertIndex)
 {
 Insert(source, 0, elementCount, insertIndex);
 }

 void Insert(const T* source, int startIndex, int count, int insertIndex)
 {
 if (count <= 0)
  throw std::invalid_argument("The count of elements to be inserted cannot less than 1.");

 if (insertIndex < 0 || insertIndex > _elementCount)
  throw std::invalid_argument("The target index is outside of the boundary of list.");

 EnsureCapacity(_elementCount + count);

 MakeRoom(insertIndex, count);

 memcpy(_mArray + insertIndex, source + startIndex, count * sizeof(T));

 _elementCount += count;
 }

 T Remove()
 {
 if (_elementCount > 0)
 {
  _elementCount--;
  return *(_mArray + _elementCount);
 }

 return NULL;
 }

 T Remove(int index)
 {
 if (index < 0 || index >= _elementCount)
  throw std::invalid_argument("The index is outsize of the boundary of list.");

 T ret_value = *(_mArray + index);

 RemoveRange(index, 1);

 return ret_value;
 }

 void RemoveRange(int startIndex, int count)
 {
 if (count <= 0)
  throw std::invalid_argument("The removing count must greater than 0.");

 if (startIndex < 0 || startIndex + count >= _elementCount)
  throw std::invalid_argument("The arguments are removing elements outsize the boundary of the list.");

 memmove(_mArray + startIndex, _mArray + startIndex + count, (_elementCount - startIndex - count) * sizeof(T));

 _elementCount -= count;
 }

 inline int Count() { return _elementCount; }
};

作為剛?cè)胧謱懙臇|西算是不錯了。當然不能忘記了我比較關(guān)心的性能問題,于是做了如下測試(都是在release環(huán)境下,且是第二次運行保證不會被JIT編譯):

1.添加500萬個元素到列表里,C#的類庫耗時86毫秒,C++的vector庫耗時64毫秒,山寨類(就是我寫的類)耗時81毫秒。看起來都差不多,因為擴容的時候似乎都是把原來的東西復制到新的地方去。

2.在表頭插入500個元素(在原有500萬個元素的基礎(chǔ)上),C#的類庫和山寨類都基本上耗時4秒左右。vector類沒測試,估計也差不多。

可以看到,經(jīng)過M$手的.NET類庫的性能是很高的,基本上接近C++的原生庫了。至于為什么,List類大量用到了Array.Copy方法,這個方法就是:

[MethodImpl(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail), SecurityCritical]
internal static extern void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, bool reliable);

這個InternalCall和Native Code一樣,都是C++寫的,因此性能差不多。

所以說.NET的程序不一定比C++的慢(當然疊加了各種功能,甚至濫用了各種特性導致性能變低的除外),如果設(shè)計得好的話完全可以放心地用。

最后要說一句,在特定環(huán)境下.NET的程序甚至比C++寫的程序更快,因為JIT會根據(jù)運行平臺(比如CPU的架構(gòu)類型等)生成對應的Native Code,而編譯式的程序就沒有這個優(yōu)勢,除非是針對了特定的平臺做過優(yōu)化,否則為了兼容各平臺只能選用最小的指令集。

無論如何,作為山寨的這個類我認為還不錯(不過不論從風格上還是其他方面貌似我還是.NET的風格),以后在學習C++的時候不斷適應吧。

相關(guān)文章

  • C++ Effective詳解

    C++ Effective詳解

    下面小編就為大家?guī)硪黄狢++ Effective的文章。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-08-08
  • C++中new/delete與malloc/free的區(qū)別小結(jié)

    C++中new/delete與malloc/free的區(qū)別小結(jié)

    本文主要介紹了C++中new/delete與malloc/free的區(qū)別小結(jié), malloc、free是C中的庫函數(shù) new、delete 是C++當中的操作符,讀者可以更好地理解C++中內(nèi)存管理的方式和優(yōu)勢
    2023-08-08
  • 詳解Matlab如何繪制圓角半透明圖例

    詳解Matlab如何繪制圓角半透明圖例

    目前MATLAB的legend圖例是不支持圓角和半透明的,所以本文將自制實現(xiàn)圓角半透明圖例。文中的示例代碼講解詳細,需要的可以參考一下
    2022-05-05
  • C++中關(guān)于constexpr函數(shù)使用及說明

    C++中關(guān)于constexpr函數(shù)使用及說明

    這篇文章主要介紹了C++中關(guān)于constexpr函數(shù)使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • c++使用正則表達式提取關(guān)鍵字的方法

    c++使用正則表達式提取關(guān)鍵字的方法

    這篇文章給大家介紹了c++使用正則表達式提取關(guān)鍵字的方法,相對來說比較簡單,同時給大家提到了c++通過正則表達式提取匹配到的字符串的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧
    2018-08-08
  • C語言popen函數(shù)調(diào)用其他進程返回值示例詳解

    C語言popen函數(shù)調(diào)用其他進程返回值示例詳解

    這篇文章主要為大家介紹了C語言popen函數(shù)調(diào)用其他進程返回值示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • vs2019 Com組件初探之簡單的COM編寫及實現(xiàn)跨語言調(diào)用的方法

    vs2019 Com組件初探之簡單的COM編寫及實現(xiàn)跨語言調(diào)用的方法

    這篇文章主要介紹了vs2019 Com組件初探之簡單的COM編寫及實現(xiàn)跨語言調(diào)用的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • vs2019安裝及簡單處理技巧(超詳細)

    vs2019安裝及簡單處理技巧(超詳細)

    這篇文章主要介紹了vs2019安裝及簡單處理方法,本文是一篇非常詳細的教程,通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • 基于c語言知識點的補遺介紹

    基于c語言知識點的補遺介紹

    本篇文章是對c語言知識點的一些補遺進行詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C++ 中 socket編程實例詳解

    C++ 中 socket編程實例詳解

    這篇文章主要介紹了C++ 中 socket編程實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06

最新評論

彭山县| 花莲市| 甘肃省| 湖北省| 江门市| 永兴县| 岳阳县| 潢川县| 定远县| 海口市| 宿松县| 寿阳县| 天津市| 三原县| 旺苍县| 新余市| 军事| 博乐市| 久治县| 峨眉山市| 芜湖市| 苗栗县| 清水县| 巴中市| 建平县| 泸定县| 高淳县| 昭通市| 阿鲁科尔沁旗| 新化县| 左云县| 郑州市| 乌恰县| 惠州市| 嘉义市| 石台县| 贞丰县| 湟中县| 安陆市| 加查县| 广河县|