<tfoot id='C6utI'></tfoot>

  1. <i id='C6utI'><tr id='C6utI'><dt id='C6utI'><q id='C6utI'><span id='C6utI'><b id='C6utI'><form id='C6utI'><ins id='C6utI'></ins><ul id='C6utI'></ul><sub id='C6utI'></sub></form><legend id='C6utI'></legend><bdo id='C6utI'><pre id='C6utI'><center id='C6utI'></center></pre></bdo></b><th id='C6utI'></th></span></q></dt></tr></i><div id='C6utI'><tfoot id='C6utI'></tfoot><dl id='C6utI'><fieldset id='C6utI'></fieldset></dl></div>

      <legend id='C6utI'><style id='C6utI'><dir id='C6utI'><q id='C6utI'></q></dir></style></legend>
        <bdo id='C6utI'></bdo><ul id='C6utI'></ul>

      <small id='C6utI'></small><noframes id='C6utI'>

    1. 确定映射是否包含键的值?

      时间:2024-05-11

        • <bdo id='A2wNR'></bdo><ul id='A2wNR'></ul>
        • <i id='A2wNR'><tr id='A2wNR'><dt id='A2wNR'><q id='A2wNR'><span id='A2wNR'><b id='A2wNR'><form id='A2wNR'><ins id='A2wNR'></ins><ul id='A2wNR'></ul><sub id='A2wNR'></sub></form><legend id='A2wNR'></legend><bdo id='A2wNR'><pre id='A2wNR'><center id='A2wNR'></center></pre></bdo></b><th id='A2wNR'></th></span></q></dt></tr></i><div id='A2wNR'><tfoot id='A2wNR'></tfoot><dl id='A2wNR'><fieldset id='A2wNR'></fieldset></dl></div>

            <legend id='A2wNR'><style id='A2wNR'><dir id='A2wNR'><q id='A2wNR'></q></dir></style></legend>
              <tbody id='A2wNR'></tbody>
              1. <small id='A2wNR'></small><noframes id='A2wNR'>

                <tfoot id='A2wNR'></tfoot>

              2. 本文介绍了确定映射是否包含键的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                确定 STL 映射是否包含给定键的值的最佳方法是什么?

                What is the best way to determine if a STL map contains a value for a given key?

                #include <map>
                
                using namespace std;
                
                struct Bar
                {
                    int i;
                };
                
                int main()
                {
                    map<int, Bar> m;
                    Bar b = {0};
                    Bar b1 = {1};
                
                    m[0] = b;
                    m[1] = b1;
                
                    //Bar b2 = m[2];
                    map<int, Bar>::iterator iter = m.find(2);
                    Bar b3 = iter->second;
                
                }
                

                在调试器中检查这个,看起来 iter 只是垃圾数据.

                Examining this in a debugger, it looks like iter is just garbage data.

                如果我取消注释这一行:

                If I uncomment out this line:

                Bar b2 = m[2]
                

                调试器显示 b2{i = 0}.(我猜这意味着使用未定义的索引将返回一个包含所有空/未初始化值的结构?)

                The debugger shows that b2 is {i = 0}. (I'm guessing it means that using an undefined index will return a struct with all empty/uninitialized values?)

                这些方法都不是很好.我真正想要的是这样的界面:

                Neither of these methods is so great. What I'd really like is an interface like this:

                bool getValue(int key, Bar& out)
                {
                    if (map contains value for key)
                    {
                        out = map[key];
                        return true;
                    }
                    return false;
                }
                

                是否存在类似这样的东西?

                Does something along these lines exist?

                推荐答案

                是否存在类似这样的东西?

                Does something along these lines exist?

                没有.使用 stl 映射类,您可以使用 ::find() 搜索地图,并将返回的迭代器与 std::map::end()

                No. With the stl map class, you use ::find() to search the map, and compare the returned iterator to std::map::end()

                所以

                map<int,Bar>::iterator it = m.find('2');
                Bar b3;
                if(it != m.end())
                {
                   //element found;
                   b3 = it->second;
                }
                

                显然,如果您愿意,您可以编写自己的 getValue() 例程(同样在 C++ 中,没有理由使用 out),但我怀疑一旦你掌握了使用 std::map::find() 的窍门,你不会想浪费时间.

                Obviously you can write your own getValue() routine if you want (also in C++, there is no reason to use out), but I would suspect that once you get the hang of using std::map::find() you won't want to waste your time.

                还有你的代码有点错误:

                m.find('2'); 将在地图中搜索 '2' 的键值.IIRC C++ 编译器会将 '2' 隐式转换为 int,这会导致 '2' 的 ASCII 代码的数值不是您想要的.

                m.find('2'); will search the map for a keyvalue that is '2'. IIRC the C++ compiler will implicitly convert '2' to an int, which results in the numeric value for the ASCII code for '2' which is not what you want.

                因为你在这个例子中的keytype是int,你想这样搜索:m.find(2);

                Since your keytype in this example is int you want to search like this: m.find(2);

                这篇关于确定映射是否包含键的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何检查元素是否在 std::set 中? 下一篇:如何对对象使用优先队列 STL?

                相关文章

                1. <i id='gM7Od'><tr id='gM7Od'><dt id='gM7Od'><q id='gM7Od'><span id='gM7Od'><b id='gM7Od'><form id='gM7Od'><ins id='gM7Od'></ins><ul id='gM7Od'></ul><sub id='gM7Od'></sub></form><legend id='gM7Od'></legend><bdo id='gM7Od'><pre id='gM7Od'><center id='gM7Od'></center></pre></bdo></b><th id='gM7Od'></th></span></q></dt></tr></i><div id='gM7Od'><tfoot id='gM7Od'></tfoot><dl id='gM7Od'><fieldset id='gM7Od'></fieldset></dl></div>
                2. <small id='gM7Od'></small><noframes id='gM7Od'>

                  1. <tfoot id='gM7Od'></tfoot>
                      <bdo id='gM7Od'></bdo><ul id='gM7Od'></ul>

                    <legend id='gM7Od'><style id='gM7Od'><dir id='gM7Od'><q id='gM7Od'></q></dir></style></legend>