Giter Club home page Giter Club logo

clinkedmap's Introduction

Introduction

A C++ header-only LinkedMap is a map that maintains insertion order, designed based on Java LinkedHashMap. Keywords: linkmap linkhashmap linkedmap linkedhashmap clinkmap clinkhashmap clinkedmap clinkedhashmap.

How to use

Just include the header file in your project

For example:

helloworld.cpp

#include <map>
#include <list>
#include <vector>
#include <chrono>
#include <string>
#include <thread>
#include <iostream>
#include <unordered_map>
#include "CLinkedMap.hpp"

class Base
{
public:
    int age;
    std::string name;
};

int main()
{
    std::cout << "original order: 2 1 4 5 3" << std::endl;
    std::chrono::time_point<std::chrono::steady_clock> start = std::chrono::steady_clock::now();
    CLinkedMap<std::string, Base> linkedHashMap;
    for (int i = 0; i < 100 * 10000; i++)
    {
        linkedHashMap["2"].age = 2;
        linkedHashMap["2"].name = "a2";
        linkedHashMap["1"].age = 1;
        linkedHashMap["1"].name = "a1";
        linkedHashMap["4"].age = 4;
        linkedHashMap["4"].name = "a4";
        linkedHashMap["5"].age = 5;
        linkedHashMap["5"].name = "a5";
        linkedHashMap["3"].age = 3;
        linkedHashMap["3"].name = "a3";
    }
    std::chrono::time_point<std::chrono::steady_clock> end = std::chrono::steady_clock::now();
    std::chrono::duration<double> elapsed = end - start;
    std::cout << "CLinkedMap Elapsed time: " << elapsed.count() << "s" << std::endl;
    for (auto it = linkedHashMap.begin(); it != linkedHashMap.end(); ++it)
    {
        std::cout << it->first << ":" << it->second.name << " " << it->second.age << std::endl;
    }
    return 0;
}

Compile

g++ helloworld.cpp -o helloworld -g -static -Wall -std=c++11

Run

./helloworld

original order: 2 1 4 5 3
CLinkedMap Elapsed time: 0.810576s
2:a2 2
1:a1 1
4:a4 4
5:a5 5
3:a3 3

介绍

只需包含头文件的LinkedMap.hpp是一个可以保持插入顺序的map,它是基于Java LinkedHashMap设计的

如何使用

只需在项目中包含头文件

案例

helloworld.cpp

#include <map>
#include <list>
#include <vector>
#include <chrono>
#include <string>
#include <thread>
#include <iostream>
#include <unordered_map>
#include "CLinkedMap.hpp"

class Base
{
public:
    int age;
    std::string name;
};

int main()
{
    std::cout << "original order: 2 1 4 5 3" << std::endl;
    std::chrono::time_point<std::chrono::steady_clock> start = std::chrono::steady_clock::now();
    CLinkedMap<std::string, Base> linkedHashMap;
    for (int i = 0; i < 100 * 10000; i++)
    {
        linkedHashMap["2"].age = 2;
        linkedHashMap["2"].name = "a2";
        linkedHashMap["1"].age = 1;
        linkedHashMap["1"].name = "a1";
        linkedHashMap["4"].age = 4;
        linkedHashMap["4"].name = "a4";
        linkedHashMap["5"].age = 5;
        linkedHashMap["5"].name = "a5";
        linkedHashMap["3"].age = 3;
        linkedHashMap["3"].name = "a3";
    }
    std::chrono::time_point<std::chrono::steady_clock> end = std::chrono::steady_clock::now();
    std::chrono::duration<double> elapsed = end - start;
    std::cout << "CLinkedMap Elapsed time: " << elapsed.count() << "s" << std::endl;
    for (auto it = linkedHashMap.begin(); it != linkedHashMap.end(); ++it)
    {
        std::cout << it->first << ":" << it->second.name << " " << it->second.age << std::endl;
    }
    return 0;
}

编译

g++ helloworld.cpp -o helloworld -g -static -Wall -std=c++11

运行

./helloworld

original order: 2 1 4 5 3
CLinkedMap Elapsed time: 0.810576s
2:a2 2
1:a1 1
4:a4 4
5:a5 5
3:a3 3

clinkedmap's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.