English 中文(简体)
关于 C++ [重复] 中的排序()函数
原标题:Confused about the sort() function in C++ [duplicate]
  • 时间:2024-07-23 04:06:45
  •  标签:
  • c++
  • sorting
This question already has answers here: Why does flowing off the end of a non-void function without returning a value not produce a compiler error? (11 answers) Closed 21 hours ago. I ve just done a problem on Leetcode. But I was confused about the sort() function of C++. This is more detail about it: Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order. -Input: An array -Output: Sorted array following the problem s rule Here is my code. First code snippet: class Solution { public: vector frequencySort(vector& nums) { std::map hm; for (int numb : nums) hm[numb] ++ ; sort(nums.begin(), nums.end(), [&](int a,int b){ if(hm[a] == hm[b]) return a > b; return hm[a] < hm[b]; }); } } The second code snippet: class Solution { public: vector frequencySort(vector& nums) { std::map hm; for (int numb : nums) hm[numb] ++ ; vector arr; for(auto &[numb,freq] : hm){ while(freq > 0){ arr.push_back(numb); freq--; } } std::sort(arr.begin(), arr.end(), [&](int a, int b){ if( hm[a] == hm[b]) return a > b; else return hm[a] < hm[b]; }); return arr; } }; Here s an example -Input: [1,1,2,2,2,3] -Output for first code snippet: [3,1,1,2,2,2] -Output for second code snippet: [3,2,2,2,1,1] The thing that I don t understand is why the second one s sort() function seems to not be working? Can anybody help me to make it clear, please? I tried two code snippets with the same function but it seems to return two different results.
问题回答
Both functions are wrong. The 1st function does not return anything, which is undefined behavior. The 2nd function return s prematurely, skipping half of its code. The compiler should have warned you about both mistakes. If it didn t, turn up the warning level.




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签