c++ map erase start location and specified count items

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// main.cpp
#include <algorithm>
#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#include <queue>
#include <sstream>
#include <thread>
#include <uuid/uuid.h>
#include <vector>
 
std::string get_time_now()
{
    std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
    time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now);
    struct tm tm_info = *localtime(&raw_time);
    auto seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
    auto mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
    auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
    auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch());
    std::stringstream ss;
    ss << std::put_time(&tm_info, "%Y%m%d%H%M%S")
       << "_"
       << std::setw(3) << std::setfill('0') << (mills.count() - seconds.count() * 1000)
       << std::setw(3) << std::setfill('0') << (micros.count() - mills.count() * 1000)
       << std::setw(3) << std::setfill('0') << (nanos.count() - micros.count() * 1000);
    return ss.str();
}
 
char *uuid_value = (char *)malloc(40);
char *get_uuid_value()
{
    uuid_t new_uuid;
    uuid_generate(new_uuid);
    uuid_unparse(new_uuid, uuid_value);
    return uuid_value;
}
 
void map_erase(const int &len,const int&length)
{
    std::map<int,std::string> _mp;
    for(int i=0;i<len;i++)
    {
        _mp.insert(std::pair<int,std::string>(i+1,get_uuid_value()));
    }
    std::cout<<"Map size:"<<_mp.size()<<std::endl;
    auto start=_mp.begin();
    std::map<int,std::string>::iterator itr=_mp.begin();
    for(int i=0;i<10;i++)
    {
        ++itr;
    }
    _mp.erase(start,itr);
    std::cout<<"Map size:"<<_mp.size()<<std::endl;
}
 
int main(int args, char **argv)
{
    map_erase(atoi(argv[1]),atoi(argv[2]));
    std::cout << get_time_now() << ",thread id:" << std::this_thread::get_id() << ",in " << __FUNCTION__ << std::endl;
}

  

1
g++-12 -std=c++2a -I. *.cpp -o h1 -luuid -lpthread

  

Run

 

posted @   FredGrit  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示