当前位置:网站首页>map<QString, bool> 的使用记录
map<QString, bool> 的使用记录
2022-04-23 11:15:00 【还债大湿兄】
我在应用中的例子如下:
map<QString, bool> m_allSkakFilepath;
auto itpath = m_allSkakFilepath.find(skaPaths.at(n).c_str());
if (itpath !=m_allSkakFilepath.end())
{
m_allSkakFilepath[skaPaths.at(n).c_str()] = false;
}
else
{
m_allSkakFilepath[skaPaths.at(n).c_str()] = true;
}
此map主要应用场合:
一般是有很多文件。并需要查找文件是否有效,要不被用过,如果用过则标志位做为true,也可用在很多组件是否被使用过,总之不管在什么场合,就是为了查找方便
以上那段代码是添加删除,map的好处是,主要上面的key值没有在map中,直接给值就会增加一个key值
下面再给出一段遍历删除的代码,以下代码为删除map中值为true,结合上面那段的意义就是用过的值全部清除记得清清后iter++:
map<QString, bool>::iterator iter=m_allSkakFilepath.begin();
for(;iter!=m_allSkakFilepath.end();){
if((*iter).second==true){
m_allSkakFilepath.erase(iter++);
}
else{
++iter;
}
}
map的清除操作:
//清除方式1:直接将map删除了
m_allSkakFilepath.clear();
//map.clear()只是把map清空了,但是内存没有释放,如果要释放内存不止是要clear()掉,还要和一个空的map来进行swap,将内存释放
//注意map中如果元素不是基本类型,也要进行内存释放,如指针,vector要尤其注意,否则map占的内存太大,会造成程序崩溃
//清除方式2用map.erase(map.begin(),map.end())比较好 清空元素
m_allSkakFilepath.erase(m_allSkakFilepath.begin(),m_allSkakFilepath.end());
//清除方式3
map<QString, bool> empty_map1;
m_allSkakFilepath.swap(empty_map1);
输出map大小
输出map大小
int nsize=m_allSkakFilepath.size();
map的排序说明:map本身默认按照key值升序排序,个人觉得够用了
如果你想倒序遍历可参考如下(用reverse_iterator指针):
for( map<QString, bool>::reverse_iterator rit=m_allSkakFilepath.rbegin();rit!=m_allSkakFilepath.rend();rit++)
cout<<(*rit).first<<","<<(*rit).second<<endl;
修改map中的值,修改比较简单,只要在其中的key设成相应的值便可以了,如下:直接设置key3从false变成true
map<QString, bool> m_allSkakFilepath;
m_allSkakFilepath.insert(("key1", false));
m_allSkakFilepath.insert(("key2", false));
m_allSkakFilepath.insert(("key3", false));
m_allSkakFilepath.insert(("key4", true));
{
map<QString, bool>::const_iterator iteMap = m_allSkakFilepath.begin();
cout << "==============旧值结果输出=============" << endl;
for(; iteMap != m_allSkakFilepath.end(); ++ iteMap)
{
cout << m_allSkakFilepath->first;
cout << ":";
cout << m_allSkakFilepath->second << endl;
}
}
//修改
m_allSkakFilepath.insert(("key3", true));
{
map<QString, bool>::const_iterator iteMap = m_allSkakFilepath.begin();
cout << "==============新值结果输出=============" << endl;
for(; iteMap != m_allSkakFilepath.end(); ++ iteMap)
{
cout << m_allSkakFilepath->first;
cout << ":";
cout << m_allSkakFilepath->second << endl;
}
}
版权声明
本文为[还债大湿兄]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_30377315/article/details/121946439
边栏推荐
- 小程序 支付
- web三大组件(Servlet,Filter,Listener)
- Which company is good for opening futures accounts? Who can recommend several safe and reliable futures companies?
- Solve the problem of "suncertpathbuilderexception: unable to find valid certification path to requested target"
- Implementation of partition table of existing data table by MySQL
- Get things technology network optimization - CDN resource request Optimization Practice
- colab
- MySQL8. 0 upgraded stepping on the pit Adventure
- CUMCM 2021-B:乙醇偶合制備C4烯烴(2)
- Cumcm 2021 - B: préparation d'oléfines C4 par couplage éthanol (2)
猜你喜欢
Source insight 4.0 FAQs
The songbird document editor will be open source: starting with but not limited to markdown
26. Delete duplicates in ordered array
Visual Road (XII) detailed explanation of collection class
进程间通信 -- 消息队列
Promise details
Visual solutions to common problems (VIII) mathematical formulas
MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
比深度学习更值得信赖的模型ART
MIT:用无监督为世界上每个像素都打上标签!人类:再也不用为1小时视频花800个小时了
随机推荐
Detailed explanation of how to smoothly go online after MySQL table splitting
The songbird document editor will be open source: starting with but not limited to markdown
ffmpeg命令行常用参数
C语言之结构体(进阶篇)
妊娠箱和分娩箱的区别
remote: Support for password authentication was removed on August 13, 2021.
MBA-day6 逻辑学-假言推理练习题
Which company is good for opening futures accounts? Who can recommend several safe and reliable futures companies?
Learn go language 0x03: understand the dependency between variables and initialization order
Alarm scene recognition
采用百度飞桨EasyDL完成指定目标识别
Detailed explanation of integer data type tinyint in MySQL
vm设置静态虚拟机
MBA - day5 mathématiques - Questions d'application - Questions d'ingénierie
2022爱分析· 工业互联网厂商全景报告
MBA-day5数学-应用题-工程问题
学习 Go 语言 0x02:对切片 Slice 的理解
Common parameters of ffmpeg command line
Mysql8. 0 installation guide
Typora operation skill description (I) md