C++;地图添加了新元素 我在C++图中有一个基本的查询, map<char,string> mymap; mymap['a']="an element"; mymap['b']="another element"; mymap['c']=mymap['b']; cout << "mymap['a'] is " << mymap['a'] << endl; cout << "mymap['b'] is " << mymap['b'] << endl; cout << "mymap['c'] is " << mymap['c'] << endl; cout << "mymap['d'] is " << mymap['d'] << endl; map-mymap; mymap['a']=“一个元素”; mymap['b']=“另一个元素”; mymap['c']=mymap['b']; cout

C++;地图添加了新元素 我在C++图中有一个基本的查询, map<char,string> mymap; mymap['a']="an element"; mymap['b']="another element"; mymap['c']=mymap['b']; cout << "mymap['a'] is " << mymap['a'] << endl; cout << "mymap['b'] is " << mymap['b'] << endl; cout << "mymap['c'] is " << mymap['c'] << endl; cout << "mymap['d'] is " << mymap['d'] << endl; map-mymap; mymap['a']=“一个元素”; mymap['b']=“另一个元素”; mymap['c']=mymap['b']; cout,c++,linux,C++,Linux,如果不希望默认插入,则应使用map.find而不是操作符[] 在容器中搜索以x为键的元素,如果找到,则返回迭代器,否则它将返回迭代器到map::end(容器末尾的元素)如果不希望默认插入,则应使用map.find而不是运算符[] 在容器中搜索以x为键的元素,如果找到,则返回迭代器,否则它将返回迭代器到map::end(容器末尾的元素)这是std::map的预期和记录的行为。为了检查某些项目是否存在,请使用。这是std::map的预期和记录行为。为了检查某个项目是否存在,请使用。map::it

如果不希望默认插入,则应使用
map.find
而不是
操作符[]


在容器中搜索以x为键的元素,如果找到,则返回迭代器,否则它将返回迭代器到map::end(容器末尾的元素)

如果不希望默认插入,则应使用
map.find
而不是
运算符[]


在容器中搜索以x为键的元素,如果找到,则返回迭代器,否则它将返回迭代器到map::end(容器末尾的元素)

这是
std::map
的预期和记录的行为。为了检查某些项目是否存在,请使用。

这是
std::map
的预期和记录行为。为了检查某个项目是否存在,请使用。

map::iterator mIte=mymap.find('d');
if(mIte!=mymap.end())
cout
map::迭代器mIte=mymap.find('d');
if(mIte!=mymap.end())

不能使用map的成员函数
迭代器find(const key\u type&k)
仅用于查询。 地图的操作符[]有一些“特殊效果”

data_type& operator[](const key_type& k)
返回对与特定键关联的对象的引用。如果映射尚未包含此类对象,则运算符[]将插入默认对象数据类型()。[3]

有关更多参考资料,请访问

使用map的成员函数
迭代器find(const key\u type&k)
仅用于查询。 地图的操作符[]有一些“特殊效果”

data_type& operator[](const key_type& k)
返回对与特定键关联的对象的引用。如果映射尚未包含此类对象,则运算符[]将插入默认对象数据类型()。[3]

有关更多参考资料,请访问
这不仅是记录在案的行为,而且稍加思考就很容易看出原因。索引操作只有两个合理的结果:要么返回映射中的有效项,要么抛出异常。如果不抛出异常,那么唯一的选择就是创建一个新条目。我不知道为什么标准选择了一个而不是另一个,但事实就是这样

Microsoft提供了一个
at
方法,该方法抛出而不是创建元素,但这似乎不在标准或中

创建一个使用
find
执行相同操作的函数很容易:

template<typename Key, typename Value>
Value& at(std::map<Key,Value> & the_map, const Key & the_key)
{
    std::map<Key,Value>::iterator it = the_map.find(the_key);
    if (it == the_map.end())
        throw std::out_of_range("map index invalid");
    return it->second;
}

template<typename Key, typename Value>
const Value& at(const std::map<Key,Value> & the_map, const Key & the_key)
{
    std::map<Key,Value>::const_iterator it = the_map.find(thekey);
    if (it == the_map.end())
        throw std::out_of_range("map index invalid");
    return it->second;
}
模板
Value和at(std::map和_-map、const-Key和_-Key)
{
std::map::iterator it=the_map.find(the_键);
if(it==the_map.end())
抛出标准::超出范围(“映射索引无效”);
返回->秒;
}
样板
常量值和at(常量标准::映射和_映射,常量键和_键)
{
std::map::const_迭代器it=the_map.find(thekey);
if(it==the_map.end())
抛出标准::超出范围(“映射索引无效”);
返回->秒;
}

这不仅是记录在案的行为,而且稍加思考就很容易看出原因。索引操作只有两个合理的结果:要么返回映射中的有效项,要么抛出异常。如果不抛出异常,那么唯一的选择就是创建一个新条目。我不知道为什么标准选择了一个而不是另一个,但事实就是这样

Microsoft提供了一个
at
方法,该方法抛出而不是创建元素,但这似乎不在标准或中

创建一个使用
find
执行相同操作的函数很容易:

template<typename Key, typename Value>
Value& at(std::map<Key,Value> & the_map, const Key & the_key)
{
    std::map<Key,Value>::iterator it = the_map.find(the_key);
    if (it == the_map.end())
        throw std::out_of_range("map index invalid");
    return it->second;
}

template<typename Key, typename Value>
const Value& at(const std::map<Key,Value> & the_map, const Key & the_key)
{
    std::map<Key,Value>::const_iterator it = the_map.find(thekey);
    if (it == the_map.end())
        throw std::out_of_range("map index invalid");
    return it->second;
}
模板
Value和at(std::map和_-map、const-Key和_-Key)
{
std::map::iterator it=the_map.find(the_键);
if(it==the_map.end())
抛出标准::超出范围(“映射索引无效”);
返回->秒;
}
样板
常量值和at(常量标准::映射和_映射,常量键和_键)
{
std::map::const_迭代器it=the_map.find(thekey);
if(it==the_map.end())
抛出标准::超出范围(“映射索引无效”);
返回->秒;
}

运算符[]
如果键不在地图中,将始终插入默认值

在C++11中,
mymap.at('d')
将抛出
超出范围,而不插入默认值

在C++03中,可以使用
find
对其进行仿真:

template <typename Key, typename Value, typename C, typename A>
Value at(std::map<Key,Value,C,A> const & map, Key const & key)
{
    typename std::map<Key,Value,C,A>::const_iterator found = map.find(key);
    if (found == map.end()) {
        throw std::out_of_range("at(map,key)");
    }
    return found->second;
}
模板
值(标准::映射常量和映射,键常量和键)
{
typename std::map::const_iterator found=map.find(key);
if(found==map.end()){
抛出std::超出范围(“at(map,key)”;
}
返回找到->秒;
}

运算符[]
如果键不在地图中,将始终插入默认值

在C++11中,
mymap.at('d')
将抛出
超出范围,而不插入默认值

在C++03中,可以使用
find
对其进行仿真:

template <typename Key, typename Value, typename C, typename A>
Value at(std::map<Key,Value,C,A> const & map, Key const & key)
{
    typename std::map<Key,Value,C,A>::const_iterator found = map.find(key);
    if (found == map.end()) {
        throw std::out_of_range("at(map,key)");
    }
    return found->second;
}
模板
值(标准::映射常量和映射,键常量和键)
{
typename std::map::const_iterator found=map.find(key);
if(found==map.end()){
抛出std::超出范围(“at(map,key)”;
}
返回找到->秒;
}

当你编写
mymap['d']
时,你期望会发生什么?如果你逐字逐句地引用代码,提到它会很礼貌。特别是因为那一页提出并回答了你的问题。“请注意,上次访问(对元素'd')时,如何使用该键在映射中插入新元素,并将其初始化为默认值(空字符串),即使访问该元素只是为了检索其值。成员函数map::find不会产生这种效果。”编写
mymap['d']时,您预计会发生什么情况
?如果你逐字逐句地引用了这段代码,那么提到它会很礼貌。特别是因为那一页提出并回答了你的问题。请注意,最后一次访问(对元素“d”)是如何使用该键和ini在映射中插入新元素的