cpp get map last element
void util::print_map(const std::map<int,std::string> &mp) { std::cout << get_time_now() << ",std::this_thread::get_id()= " << std::this_thread::get_id() << ", started in " << std::string(__FILE__) << "," << std::string(__FUNCTION__) << "," << std::to_string(__LINE__) << std::endl; auto itr=mp.begin(); while(itr!=mp.end()) { std::cout<<"Key="<<itr->first<<",value="<<itr->second<<std::endl; itr=std::next(itr,1000000); } int len=mp.size(); int idx=0; for(auto itr=mp.begin();itr!=mp.end();itr++) { if(++idx==len) { std::cout<<"Last element,Key="<<itr->first<<",value="<<itr->second<<std::endl; } } std::cout << get_time_now() << ",std::this_thread::get_id()= " << std::this_thread::get_id() << ", finished in " << std::string(__FILE__) << "," << std::string(__FUNCTION__) << "," << std::to_string(__LINE__) << std::endl; }
The critical way located at retrieve the map elements count via size(),then iterate the map util to the end while increment the third temperatory variable,while the medium variable add 1 equals to the size of the map.in the for loop,the itr is the last element
int len=mp.size(); int idx=0; for(auto itr=mp.begin();itr!=mp.end();itr++) { if(++idx==len) { std::cout<<"Last element,Key="<<itr->first<<",value="<<itr->second<<std::endl; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2022-03-19 C++original fatorial and optimized factorial method
2022-03-19 C++ optimized fibonacci algorithm
2022-03-19 C++ quick sort ascendingly and descendingly
2022-03-19 C++ write file and read file via fstream
2022-03-19 C++ vector iterator std::ref(vec)
2022-03-19 C++ selection sort.quick sort
2020-03-19 TripleDESCryptoServiceProvider CryptoStream