如果元素是在C++std::map中设置的
If element is set in C++ std::map?
如何确定std::map存储中的元素是否已设置?示例:
#include <map>
#include <string>
using namespace std;
map<string, FOO_class> storage;
storage["foo_el"] = FOO_class();
有类似if (storage.isset("foo_el"))
的东西吗?
if (storage.count("foo_el"))
count()
返回容器中项目的出现次数,但映射中每个键只能出现一次。因此,如果存在该项,则storage.count("foo_el")
为1,否则为0。
尝试storage.find("foo_el") != storage.end();
。
std::map运算符[]很讨厌:如果不存在,它会创建一个条目,请先使用map::find。
如果您想插入或修改
std::pair<map::iterator, bool> insert = map.insert(map::value_type(a, b));
if( ! insert.second) {
// Modify insert.first
}
您还可以在插入新的键值对时检查迭代器:
std::map<char,int> mymap;
mymap.insert ( std::pair<char,int>('a',100) );
std::pair<std::map<char,int>::iterator,bool> ret;
ret = mymap.insert ( std::pair<char,int>('a',500) );
if (ret.second==false) {
std::cout << "element is already existed";
std::cout << " with a value of " << ret.first->second << 'n';
}
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在C++/Linux中设置单调时钟的一些技巧
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何使用 gdb 设置 std::map 变量
- 将 std::map 项设置为 CListBox 的项数据
- 无法在 std::map<std::string,std::shared_ptr 中设置值<class>>
- C 设置STD :: MAP值为结构实例的指针
- 映射/设置迭代器在 std::map 中不可递增
- 如何设置和使用 std::map<void*, MyClass*>
- 在 map<int 中插入,设置<int>>
- 如果元素是在C++std::map中设置的
- C++ std::map<std::string, std::set<std::string>> .如何循环设置值?
- 不能设置std::map元素
- 是否可以将默认构造函数设置为"std::map<T1,T2>"值?
- C++ 映射/设置迭代器不可取消引用 --- map<const char *,字符串>
- 转换 map<int C++,请将<int>>设置为 map<int, 矢量<int>>
- 为什么不能为类型的函数参数设置默认值<map>?
- 在 std::map 的元素上设置数据断点
- 在 std::map 中设置所有值