<bdo id='sjNsd'></bdo><ul id='sjNsd'></ul>

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

      在地图中寻找最大值

      时间:2023-09-15
    2. <legend id='f7JvI'><style id='f7JvI'><dir id='f7JvI'><q id='f7JvI'></q></dir></style></legend>
      • <i id='f7JvI'><tr id='f7JvI'><dt id='f7JvI'><q id='f7JvI'><span id='f7JvI'><b id='f7JvI'><form id='f7JvI'><ins id='f7JvI'></ins><ul id='f7JvI'></ul><sub id='f7JvI'></sub></form><legend id='f7JvI'></legend><bdo id='f7JvI'><pre id='f7JvI'><center id='f7JvI'></center></pre></bdo></b><th id='f7JvI'></th></span></q></dt></tr></i><div id='f7JvI'><tfoot id='f7JvI'></tfoot><dl id='f7JvI'><fieldset id='f7JvI'></fieldset></dl></div>
          <tbody id='f7JvI'></tbody>

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

            <bdo id='f7JvI'></bdo><ul id='f7JvI'></ul>
            <tfoot id='f7JvI'></tfoot>

              • 本文介绍了在地图中寻找最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我一直在做一个基本的程序来查找向量的最大值、最小值、中值、方差、众数等.一切顺利,直到我进入模式.

                I've been doing a basic program to find the max, min, median, variance, mode etc. of a vector. Everything went fine until I got to the mode.

                在我看来,我应该能够遍历向量,并且对于出现的每个数字,我都会在地图上增加一个键.找到具有最高值的键将是出现次数最多的键.与其他键相比,我会知道它是单多还是无模式应答.

                The way I see it, I should be able to loop through the vector, and for each number that occurs I increment a key on the map. Finding the key with the highest value would then be the one that occurred the most. Comparing to other keys would tell me if it's a single multiple or no mode answer.

                这是给我带来很多麻烦的代码块.

                Here's the chunk of code that's been causing me so much trouble.

                map<int,unsigned> frequencyCount;
                // This is my attempt to increment the values
                // of the map everytime one of the same numebers 
                for(size_t i = 0; i < v.size(); ++i)
                    frequencyCount[v[i]]++;
                
                unsigned currentMax = 0;
                unsigned checked = 0;
                unsigned maax = 0;
                for(auto it = frequencyCount.cbegin(); it != frequencyCount.cend(); ++it )
                    //checked = it->second;
                    if (it ->second > currentMax)
                    {
                        maax = it->first;
                    }
                    //if(it ->second > currentMax){
                    //v = it->first
                
                cout << " The highest value within the map is: " << maax << endl;
                

                可以在这里看到整个程序.http://pastebin.com/MzPENmHp

                The entire program can be seen here. http://pastebin.com/MzPENmHp

                推荐答案

                您从未更改代码中的 currentMax.

                You never changed currentMax in your code.

                map<int,unsigned> frequencyCount;
                for(size_t i = 0; i < v.size(); ++i)
                    frequencyCount[v[i]]++;
                
                unsigned currentMax = 0;
                unsigned arg_max = 0;
                for(auto it = frequencyCount.cbegin(); it != frequencyCount.cend(); ++it ) }
                    if (it ->second > currentMax) {
                        arg_max = it->first;
                        currentMax = it->second;
                    }
                }
                cout << "Value " << arg_max << " occurs " << currentMax << " times " << endl;
                

                另一种找到模式的方法是对向量​​进行排序并循环遍历一次,跟踪值发生变化的索引.

                Another way to find the mode is to sort the vector and loop through it once, keeping track of the indices where the values change.

                这篇关于在地图中寻找最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何擦除 &amp;删除指向存储在向量中的对象的 下一篇:C++ 将多种类型推送到向量上

                相关文章

                最新文章

                <tfoot id='Shex3'></tfoot>

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

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

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