high efficiency vector using right value reference and std::move

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
class Element {
private:
    int number;
public:
    Element() : number(0) {
        cout << "ctor" << endl;
    }
    Element(int num) : number(num) {
        cout << "ctor" << endl;
    }
    Element(const Element& e) : number(e.number) {
        cout << "copy ctor" << endl;
    }
    Element(Element&& e) : number(e.number) {
        cout << "right value ctor" << endl;
    }
    ~Element() {
        cout << "dtor" << endl;
    }
    void operator=(const Element& item) {
        number = item.number;
    }
    bool operator==(const Element& item) {
        return (number == item.number);
    }
    void operator()() {
        cout << number;
    }
    int GetNumber() {
        return number;
    }
};

template<typename T>
class Vector {
private:
    T* items;
    int count;
public:
    Vector() : count{ 0 }, items{ nullptr } {

    }
    Vector(const Vector& vector) : count{vector.count} {
        items = static_cast<T*>(malloc(sizeof(T) * count));
        memcpy(items, vector.items, sizeof(T) * count);
    }
    Vector(Vector&& vector) :count{ vector.count }, items{ vector.items } {
        vector.items = nullptr;
        vector.count = 0;
    }
    ~Vector() {
    }
    T& operator[](int index){
        if (index<0||index>=count) {
            cout<<"invalid index"<<endl;
            return items[0];
        }
        return items[index];
    }
    int returnCount(){
        return count;
    }
    void Clear() {
        for (int i = 0; i < count; i++)
        {
            items[i].~T();
        }
        count = 0;
        items = nullptr;
    }

    void Add(const T& item) {
        T* newItems = static_cast<T*>(malloc(sizeof(T) * (count + 1)));
        int i;
        for (i = 0; i < count; i++)
        {
            new(&newItems[i])T(move(items[i]));
        }
        new(&newItems[count])T(move(item));
        for (int i = 0; i < count; i++)
        {
            items[i].~T();
        }
        count++;
        items = newItems;
    }
    bool Insert(const T& item,int index) {
        if (index < 0 || index >= count)
        {
            return false;
        }
        T* newItems = static_cast<T*>(malloc(sizeof(T) * (count + 1)));
        int i;
        for (i = 0; i < index; i++)
        {
            new(&newItems[i])T(move(items[i]));
        }
        new(&newItems[index])T(move(item));
        for (i = index; i < count; i++)
        {
            new(&newItems[i+1])T(move(items[i]));
        }
        for (i = 0; i < count; i++)
        {
            items[i].~T();
        }
        count++;
        items = newItems;
        return true;
    }
    bool Remove(int index) {
        if (index < 0 || index >= count)
        {
            return false;
        }
        T* newItems = static_cast<T*>(malloc(sizeof(T) * (count - 1)));
        int i;
        for (i = 0; i < index; i++)
        {
            new(&newItems[i])T(move(items[i]));
        }
        for (i = index + 1; i < count; i++)
        {
            new(&newItems[i-1])T(move(items[i]));
        }
        for (i = 0; i < count; i++)
        {
            items[i].~T();
        }
        count--;
        items = newItems;
        return true;
    }
    int Contains(const T& item) {
        for (int i = 0; i < count; i++)
        {
            if (items[i] == item)
            {
                return i;
            }
        }
        return -1;
    }
};

template<typename T>
void PrintVector(Vector<T>& v) {
    int count = v.returnCount();
    for (int i = 0; i < count; i++)
    {
        v[i]();
        cout << " ";
    }
    cout << endl;
}

int main() {
    Vector<Element> v;
    for (int i = 0; i < 4; i++) {
        Element e(i);
        v.Add(e);
    }
    PrintVector(v);
    Element e2(4);
    if (!v.Insert(e2, 10))
    {
        v.Insert(e2, 2);
    }
    PrintVector(v);
    if (!v.Remove(10))
    {
        v.Remove(2);
    }
    PrintVector(v);
    Element e3(1), e4(10);
    cout << v.Contains(e3) << endl;
    cout << v.Contains(e4) << endl;
    Vector<Element> v2(v);
    Vector<Element> v3(move(v2));
    PrintVector(v3);
    v2.Add(e3);
    PrintVector(v2);
    return 0;
}

output:

ctor
copy ctor
dtor
ctor
right value ctor
copy ctor
dtor
dtor
ctor
right value ctor
right value ctor
copy ctor
dtor
dtor
dtor
ctor
right value ctor
right value ctor
right value ctor
copy ctor
dtor
dtor
dtor
dtor
0 1 2 3 
ctor
right value ctor
right value ctor
copy ctor
right value ctor
right value ctor
dtor
dtor
dtor
dtor
0 1 4 2 3 
right value ctor
right value ctor
right value ctor
right value ctor
dtor
dtor
dtor
dtor
dtor
0 1 2 3 
ctor
ctor
1
-1
0 1 2 3 
copy ctor
1 
dtor
dtor
dtor

37 thoughts on “high efficiency vector using right value reference and std::move”

  1. Hello buddy. It was hard to find this site in google.

    It’s not even in top 10. You should focus on hq links from top websites in your niche.

    I know of a very effective free method to get strong links and instant traffic.
    The best thing about this method is that you start getting clicks right away.

    For more info search in google for; masitsu’s tricks

  2. Hello. I see that you don’t update your website too often.
    I know that writing content is boring and time consuming.
    But did you know that there is a tool that allows you to create new articles using
    existing content (from article directories or other pages from your niche)?
    And it does it very well. The new articles are unique and pass the copyscape test.
    Search in google and try: miftolo’s tools

  3. 當蛋白線埋入皮膚後,皮下組織會將蛋白線視為異物,啟動異物反應,因此刺激膠原蛋白生長,且可促進新陳代謝,更新老化肌膚,所以客戶在術後也會發覺膚質變得較透亮白皙。膠原蛋白提拉線像一個“磁力線”一樣,將埋入處附近的肌肉和脂肪固定在原處,不會往下墜,其後會吸引皮下組織往蛋白線集中部位移動,就會逐漸產生提拉的緊實效果。也被用於填充美容的微整形,蛋白線材就好比蓋房子的鋼骨支架,先埋入皮膚當作基底結構,然後再注入好比水泥的玻尿酸或自體脂肪,因為有吸附力不易擴散,就可穩定固定住填充物,讓立體支撐力效果更好。

  4. 9合1升級保護,提升全面防護!!! 相比之前四合一的疫苗只能預防四種hpv病毒,70的相關癌症。 九合一可以預防9種hpv病毒,可預防高達90以上的子宮頸癌、外陰癌、陰道癌和肛門癌及癌前病變等。 HPV9合1子宮頸癌疫苗 Gardasil 功效 100 預防高危致癌的 HPV 16、18、31、33、45、52 及 58 型號 (可減低 90 患子宮頸癌、、90-95 肛門癌、85-90 外陰癌、80-85 陰道癌及相關癌前病變的風險) 100 減低引致生殖器官濕疣 (俗稱「椰菜花」) 的 HPV 6、11 型的感染 (可減低超過 90 患生殖器官濕疣的風險) 男性方面,能減低患上肛門癌、生殖器官濕疣 (俗稱「椰菜花」) 及傳播 HPV 病毒的風險 注射位置紅腫及痛、輕微發燒和頭痛,至今未有嚴重副作用記錄

  5. I see you don’t monetize your page, don’t waste
    your traffic, you can earn additional bucks every month.
    You can use the best adsense alternative for any type of
    website (they approve all websites), for more info simply
    search in gooogle: boorfe’s tips monetize your website

  6. 4款定妝噴霧 瀟灑告別脫妝煩惱 Marie Claire (HK) Edition 夏天脫妝不外乎是因為流汗及出油,如果想個妝容可以持久一點兼減低脫妝機會率,那你就需要購買一枝定妝噴霧旁身了。 定妝噴霧固名思意是用來維持妝容完整度的東西,於完妝後噴上去,像幫你蓋上一塊保護膜,有效為

  7. Thank you for the sensible critique. Me & my neighbor were just preparing to do some research about this. We got a grab a book from our area library but I think I learned more from this post. I am very glad to see such great info being shared freely out there.

  8. Great post. I was checking constatly this blog annd I’m
    impressed! Extremely helpful information specially the lwst part 🙂 I care for such information much.

    I was looking for this certain information for a very
    long time. Thank you and good luck.

  9. I’m at Liverpool University beautyheat The Fed has telegraphed its intentions to pare back itsmonthly purchases of $85 billion in bonds at its two-day meetingthat ends next Wednesday. The scale of the tapering and what FedChairman Ben Bernanke might say at his press conference are keyhere, but the steady messaging in the last few months means nextweek probably won’t see carnage in the markets. order levitrex “We have the right to stand in front of the world to say that all the people of Egypt have the right to choose their president, parliament and constitution. No military force has the right to one day wake up and suddenly decide who runs the government,” al-Beltaji continued. sildenafil comprar mexico “I don’t feel like I’ll regret this…I think I’ve come to a conclusion where I don’t think that Des is the love of my life,” Brooks told the host. “I gave it everything that I had and I love so many things about Desiree, but I’m not in love with her.” bhagan’s drugs price plaza Officials in at least 45 U.S. states have confirmed that recipients of deportation relief under that program are eligible for licenses or have been issuing licenses to people in the group, according to the National Immigration Law Center. cervidil vs prostin Mohammad Salaheddine, an activist in Syria, told SSG that “the dead were loaded into large pick-up trucks by the hundreds and were taken to three large mass graves in Zamalka, Ain Turma and Arbeen. The bodies were placed inside, and dirt was bulldozed to bury them.”

  10. Jordan 12 Gym Redhttp://www.jordan12gymred.us.com/
    nike factory outlet store onlinehttp://www.nikefactoryoutletstoreonline.com/
    nike factory outlet store onlinehttp://www.nikefactoryoutletstoreonline.us/
    Nike Outlet satorehttp://www.nikestores.us.com/
    retro jordan 33http://www.jordan33.us/
    Yeezy Shoeshttp://www.cheapjerseysfromchina.us/
    nfl jerseyshttp://www.customnfljerseys.us/
    jordan 11 concord 2018http://www.jordan11concord.us.com/
    Jordan 12 Gym Red 2018http://www.jordan12gymred.us/
    Red Jordan 12http://www.redjordan12.us/
    Yeezyshttp://www.yeezy.com.co/
    Yeezyshttp://www.yeezys.us.com/
    Yeezyhttp://www.yeezysupply.us.com/
    Yeezy Sneakershttp://www.yeezy-shoes.us.com/
    Yeezy Boosthttp://www.yeezy-boost350.com/
    Yeezy Boosthttp://www.yeezyboost350.us.com/
    Yeezy Blue Tinthttp://www.yeezybluetint.com/
    Yeezy 500http://www.yeezy500utilityblack.com/
    Adidas Yeezy 500http://www.yeezy500utilityblack.us/
    Vapor Maxhttp://www.vapor-max.org.uk/
    Salomon Shoeshttp://www.salomon-shoes.org.uk/
    Salomon Shoeshttp://www.salomons.me.uk/
    Salomon UKhttp://www.salomonspeedcross4.org.uk/
    Off White Air Jordan 1http://www.offwhitejordan1.com/
    Vapor Maxhttp://www.nikevapormax.org.uk/
    Nike React Element 87http://www.nikereactelement87.us.com/
    React Element 87http://www.nikereactelement87.us/
    Nike Air Vapormax Plushttp://www.nikeplus.us/
    Nike Outlethttp://www.nike–outlet.us/
    Nike Outlethttp://www.nikeoutletstoreonlineshopping.us/
    Nike Outlethttp://www.nikeoutletonlineshopping.us/
    NBA Jerseyshttp://www.nikenbajerseys.us/
    Air Max Nikehttp://www.nikeairmax.us/
    Air Max 2017http://www.max2017.us/
    Jordan Shoes 2018http://www.jordan-com.com/
    Jordan 11 Concord 2018http://www.jordan11-concord.com/
    Cheap Yeezy Boosthttp://www.cs7boots1.com/
    Cheap NBA Jerseyshttp://www.cheapnba-jerseys.us/
    Birkenstock Sandalshttp://www.birkenstocksandalsuk.me.uk/
    Basketball Jerseyhttp://www.basketball-jersey.us/
    Balenciaga UKhttp://www.balenciaga.me.uk/
    Balenciagahttp://www.balenciagauk.org.uk/
    Balenciaga UKhttp://www.balenciagatriples.org.uk/
    Balenciagahttp://www.birkenstocks.me.uk/
    Balenciagahttp://www.balenciagatrainers.org.uk/
    Air Max 270http://www.airmax270.org.uk/
    Adidas Yeezyshttp://www.adidasyeezyshoes.org.uk/
    Adidas Yeezy Shoeshttp://www.adidasyeezyshoes.org.uk/

  11. Air Jordan 12 Gym Redhttp://www.jordan12gymred.us.com/
    nike factory outlethttp://www.nikefactoryoutletstoreonline.com/
    nike factory outlethttp://www.nikefactoryoutletstoreonline.us/
    Nike Outlet satorehttp://www.nikestores.us.com/
    jordan 33http://www.jordan33.us/
    Yeezy Shoeshttp://www.cheapjerseysfromchina.us/
    custom nfl jerseyshttp://www.customnfljerseys.us/
    jordan 11 concord 2018http://www.jordan11concord.us.com/
    Jordan 12 Gym Red 2018http://www.jordan12gymred.us/
    Jordan 12 Gym Redhttp://www.redjordan12.us/
    Yeezyshttp://www.yeezy.com.co/
    Yeezyshttp://www.yeezys.us.com/
    Yeezy Supplyhttp://www.yeezysupply.us.com/
    Yeezy Shoeshttp://www.yeezy-shoes.us.com/
    Yeezy Boosthttp://www.yeezy-boost350.com/
    Yeezy Boosthttp://www.yeezyboost350.us.com/
    Yeezy Blue Tinthttp://www.yeezybluetint.com/
    Yeezy 500 Utility Blackhttp://www.yeezy500utilityblack.com/
    Yeezy 500 Utility Blackhttp://www.yeezy500utilityblack.us/
    Nike Air VaporMaxhttp://www.vapor-max.org.uk/
    Salomonhttp://www.salomon-shoes.org.uk/
    Salomon UKhttp://www.salomons.me.uk/
    Salomon Shoeshttp://www.salomonspeedcross4.org.uk/
    Off White Jordan 1http://www.offwhitejordan1.com/
    Nike VaporMaxhttp://www.nikevapormax.org.uk/
    React Element 87http://www.nikereactelement87.us.com/
    React Element 87http://www.nikereactelement87.us/
    Nike Vapormax Plushttp://www.nikeplus.us/
    Nike Outlet Storehttp://www.nike–outlet.us/
    Nike Outlet Storehttp://www.nikeoutletstoreonlineshopping.us/
    Nike Outlet Storehttp://www.nikeoutletonlineshopping.us/
    NBA Jerseyshttp://www.nikenbajerseys.us/
    Air Max 97http://www.nikeairmax.us/
    Air Max Nikehttp://www.max2017.us/
    Air Jordan Shoeshttp://www.jordan-com.com/
    Jordan 11 Concordhttp://www.jordan11-concord.com/
    Cheap Yeezy Shoeshttp://www.cs7boots1.com/
    Wholesale Cheap NBA Jerseyshttp://www.cheapnba-jerseys.us/
    Birkenstock Sandalshttp://www.birkenstocksandalsuk.me.uk/
    Basketball Jerseyhttp://www.basketball-jersey.us/
    Balenciagahttp://www.balenciaga.me.uk/
    Balenciagahttp://www.balenciagauk.org.uk/
    Balenciaga UKhttp://www.balenciagatriples.org.uk/
    Balenciagahttp://www.birkenstocks.me.uk/
    Balenciaga Trainershttp://www.balenciagatrainers.org.uk/
    Nike Air Max 270http://www.airmax270.org.uk/
    Yeezyhttp://www.adidasyeezyshoes.org.uk/
    Adidas Yeezy Shoeshttp://www.adidasyeezyshoes.org.uk/

  12. Jordan 12 Gym Redhttp://www.jordan12gymred.us.com/
    Nike Outlet satorehttp://www.nikefactoryoutletstoreonline.com/
    nike factory outlethttp://www.nikefactoryoutletstoreonline.us/
    Nike Outlethttp://www.nikestores.us.com/
    retro jordan 33http://www.jordan33.us/
    Yeezyshttp://www.cheapjerseysfromchina.us/
    cheap custom nfl jerseyshttp://www.customnfljerseys.us/
    jordan 11 concordhttp://www.jordan11concord.us.com/
    Jordan 12 Gym Red 2018http://www.jordan12gymred.us/
    Red Jordan 12http://www.redjordan12.us/
    Yeezyhttp://www.yeezy.com.co/
    Yeezyshttp://www.yeezys.us.com/
    Yeezy Shoeshttp://www.yeezysupply.us.com/
    Yeezy Shoeshttp://www.yeezy-shoes.us.com/
    Yeezy Boosthttp://www.yeezy-boost350.com/
    Yeezy Boost 350 V2http://www.yeezyboost350.us.com/
    Yeezy Boost 350 V2 Blue Tinthttp://www.yeezybluetint.com/
    Yeezy 500http://www.yeezy500utilityblack.com/
    Adidas Yeezy 500http://www.yeezy500utilityblack.us/
    Nike VaporMaxhttp://www.vapor-max.org.uk/
    Salomonhttp://www.salomon-shoes.org.uk/
    Salomon Shoeshttp://www.salomons.me.uk/
    Salomon UKhttp://www.salomonspeedcross4.org.uk/
    Off White Jordan 1http://www.offwhitejordan1.com/
    Nike VaporMaxhttp://www.nikevapormax.org.uk/
    Nike React Element 87http://www.nikereactelement87.us.com/
    Nike Element 87http://www.nikereactelement87.us/
    Nike Vapormax Plushttp://www.nikeplus.us/
    Nike Outlethttp://www.nike–outlet.us/
    Nike Outlet Storehttp://www.nikeoutletstoreonlineshopping.us/
    Nike Outlethttp://www.nikeoutletonlineshopping.us/
    NBA Jerseyshttp://www.nikenbajerseys.us/
    Air Max 97http://www.nikeairmax.us/
    Air Max Nikehttp://www.max2017.us/
    Air Jordan Shoeshttp://www.jordan-com.com/
    Jordan 11 Concordhttp://www.jordan11-concord.com/
    Cheap Yeezy Boosthttp://www.cs7boots1.com/
    Cheap NBA Jerseyshttp://www.cheapnba-jerseys.us/
    Birkenstock UKhttp://www.birkenstocksandalsuk.me.uk/
    NBA Jerseyshttp://www.basketball-jersey.us/
    Balenciagahttp://www.balenciaga.me.uk/
    Balenciagahttp://www.balenciagauk.org.uk/
    Balenciagahttp://www.balenciagatriples.org.uk/
    Balenciagahttp://www.birkenstocks.me.uk/
    Balenciagahttp://www.balenciagatrainers.org.uk/
    Air Max 270http://www.airmax270.org.uk/
    Yeezyhttp://www.adidasyeezyshoes.org.uk/
    Adidas Yeezyhttp://www.adidasyeezyshoes.org.uk/

  13. Thank you for the sensible critique. Me & my neighbor were just preparing to do a little research on this. We got a grab a book from our area library but I think I learned more from this post. I am very glad to see such magnificent information being shared freely out there.

  14. Hi. I have checked your billyu.me and i see you’ve got some duplicate content
    so probably it is the reason that you don’t rank hi in google.
    But you can fix this issue fast. There is a tool that creates
    articles like human, just search in google: miftolo’s tools

Leave a Reply

Your email address will not be published. Required fields are marked *