need STL set in insertion order

后端 未结 6 403
谎友^
谎友^ 2020-12-31 02:27

How to store elements in set in insertion order. for example.

setmyset;

myset.insert(\"stack\");
myset.insert(\"overflow\");

6条回答
  •  一生所求
    2020-12-31 02:57

    what you need is this, very simple and a standard library. Example online compiler link: http://cpp.sh/7hsxo

    #include 
    #include 
    #include  
    static std::unordered_set myset;
    
    
    int main()
    {
        myset.insert("blah");
        myset.insert("blah2");
        myset.insert("blah3");
    
        int count = 0;
        for ( auto local_it = myset.begin(); local_it!= myset.end(); ++local_it ) {
              printf("index: [%d]: %s\n", count,  (*local_it).c_str());
              count++;
        }
        printf("\n");
        for ( unsigned i = 0; i < myset.bucket_count(); ++i) {
            for ( auto local_it = myset.begin(i); local_it!= myset.end(i); ++local_it )
                  printf("bucket: [%d]: %s\n", i,  (*local_it).c_str());
        }
    }
    

提交回复
热议问题