Skip to main content
Tweeted twitter.com/StackCodeReview/status/1271502544242708485
deleted 4 characters in body; edited title
Source Link
Mast
  • 13.9k
  • 12
  • 57
  • 128

Am I causing Undefined Behavior? Return whether the cards can be rearranged

I solved this question on LeetCode.com:

Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can. For e.g., for hand=[1,2,3,6,2,3,4,7,8] and W=3, the answer should be true.

as:

class Solution { public: bool isNStraightHand(vector<int>& nums, int k) { if(nums.size()%k!=0) return false; map<int, int> _m; for(int num: nums) _m[num]++; while(_m.size()) { auto it=_m.begin(); int count=1; int prev=it->first; while(count<=k) { it->second--; if(count>1 && it->first-prev!=1) return false; else prev=it->first; count++; if(it->second==0) { auto backupIt=it; _m.erase(backupIt); //am I causing UB here? } it++; } } return true; } }; 

This works, but it doesn't look like a sturdy solution. I am curious to know if I am causing Undefined Behavior (UB) when erasing the element above. Earlier, I just had _m.erase(it);, but that wasn't good either. I think so, since the official website says:

References and iterators to the erased elements are invalidated.

so, when I do a it++ in the following line, isn't that invalid? If it is indeed true that I am causing UB, could someone please point out how I could rectify it?

Thanks! That part in particular can probably be improved.

Am I causing Undefined Behavior?

I solved this question on LeetCode.com:

Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can. For e.g., for hand=[1,2,3,6,2,3,4,7,8] and W=3, the answer should be true.

as:

class Solution { public: bool isNStraightHand(vector<int>& nums, int k) { if(nums.size()%k!=0) return false; map<int, int> _m; for(int num: nums) _m[num]++; while(_m.size()) { auto it=_m.begin(); int count=1; int prev=it->first; while(count<=k) { it->second--; if(count>1 && it->first-prev!=1) return false; else prev=it->first; count++; if(it->second==0) { auto backupIt=it; _m.erase(backupIt); //am I causing UB here? } it++; } } return true; } }; 

I am curious to know if I am causing Undefined Behavior (UB) when erasing the element above. Earlier, I just had _m.erase(it);, but that wasn't good either. I think so, since the official website says:

References and iterators to the erased elements are invalidated.

so, when I do a it++ in the following line, isn't that invalid? If it is indeed true that I am causing UB, could someone please point out how I could rectify it?

Thanks!

Return whether the cards can be rearranged

I solved this question on LeetCode.com:

Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can. For e.g., for hand=[1,2,3,6,2,3,4,7,8] and W=3, the answer should be true.

as:

class Solution { public: bool isNStraightHand(vector<int>& nums, int k) { if(nums.size()%k!=0) return false; map<int, int> _m; for(int num: nums) _m[num]++; while(_m.size()) { auto it=_m.begin(); int count=1; int prev=it->first; while(count<=k) { it->second--; if(count>1 && it->first-prev!=1) return false; else prev=it->first; count++; if(it->second==0) { auto backupIt=it; _m.erase(backupIt); //am I causing UB here? } it++; } } return true; } }; 

This works, but it doesn't look like a sturdy solution. I am curious to know if I am causing Undefined Behavior (UB) when erasing the element above. Earlier, I just had _m.erase(it);, but that wasn't good either. I think so, since the official website says:

References and iterators to the erased elements are invalidated.

so, when I do a it++ in the following line, isn't that invalid? That part in particular can probably be improved.

Source Link
J. Doe
  • 143
  • 3

Am I causing Undefined Behavior?

I solved this question on LeetCode.com:

Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can. For e.g., for hand=[1,2,3,6,2,3,4,7,8] and W=3, the answer should be true.

as:

class Solution { public: bool isNStraightHand(vector<int>& nums, int k) { if(nums.size()%k!=0) return false; map<int, int> _m; for(int num: nums) _m[num]++; while(_m.size()) { auto it=_m.begin(); int count=1; int prev=it->first; while(count<=k) { it->second--; if(count>1 && it->first-prev!=1) return false; else prev=it->first; count++; if(it->second==0) { auto backupIt=it; _m.erase(backupIt); //am I causing UB here? } it++; } } return true; } }; 

I am curious to know if I am causing Undefined Behavior (UB) when erasing the element above. Earlier, I just had _m.erase(it);, but that wasn't good either. I think so, since the official website says:

References and iterators to the erased elements are invalidated.

so, when I do a it++ in the following line, isn't that invalid? If it is indeed true that I am causing UB, could someone please point out how I could rectify it?

Thanks!