Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

#Basic Algorithm

Basic Algorithm

#Basic Algorithm

Basic Algorithm

added 302 characters in body
Source Link
Jerry Coffin
  • 34.1k
  • 4
  • 77
  • 144

I'd also consider using an array instead of a map, as outlined in an answer to an earlier question: https://codereview.stackexchange.com/a/208502/489 --but this can depend on the range of values you're dealing with. With a 16-bit int, it's no problem at all on most machines. With a 32-bit int (and no other constraints on values) it's still possible on many machines, but probably impractical. For arbitrary 64-bit int, an array won't be practical.

I'd also consider using an array instead of a map, as outlined in an answer to an earlier question: https://codereview.stackexchange.com/a/208502/489

I'd also consider using an array instead of a map, as outlined in an answer to an earlier question: https://codereview.stackexchange.com/a/208502/489 --but this can depend on the range of values you're dealing with. With a 16-bit int, it's no problem at all on most machines. With a 32-bit int (and no other constraints on values) it's still possible on many machines, but probably impractical. For arbitrary 64-bit int, an array won't be practical.

added 1 character in body
Source Link
Jerry Coffin
  • 34.1k
  • 4
  • 77
  • 144
int count_dupes(std::vector<int> const &inputs) { 
 std::map<int, int> counts;
 for (charauto ci : inputinputs)
 ++counts[c];++counts[i];
 return std::count_if(counts.begin(), counts.end(),
 [](auto const &p) { return p.second >= 2; });
}

Right now, you're passing the input by value. This means when you call the function with some vector, a copy of the original vector will normally be made and passed to the function. As a general rule, something like a vector that's potentially large and slow to copy should be passed by reference to const, as shown in the code above.

...where each closing brace is vertically aligned with the beginning of the block it closes. As a side-note, there are almost endless debates about the efficacy of various bracing styles. I'm not going to advocate for or against any of the well known styles, but I think there's a fair amount to be gained from using a style that's well known, and then using it consistently. I don't see much to get gainedgain from inventing yet another style, slightly that's different from what most others usealmost anybody else uses.

int count_dupes(std::vector<int> const &inputs) { 
 std::map<int, int> counts;
 for (char c : input)
 ++counts[c];
 return std::count_if(counts.begin(), counts.end(),
 [](auto const &p) { return p.second >= 2; });
}

Right now, you're passing the input by value. This means when you call the function with some vector, a copy of the original vector will normally be made and passed to the function. As a general rule, something like a vector should be passed by reference to const, as shown in the code above.

...where each closing brace is vertically aligned with the beginning of the block it closes. As a side-note, there are almost endless debates about the efficacy of various bracing styles. I'm not going to advocate for or against any of the well known styles, but I think there's a fair amount to be gained from using a style that's well known, and then using it consistently. I don't see much to get gained from inventing yet another style, slightly different from what most others use.

int count_dupes(std::vector<int> const &inputs) { 
 std::map<int, int> counts;
 for (auto i : inputs)
 ++counts[i];
 return std::count_if(counts.begin(), counts.end(),
 [](auto const &p) { return p.second >= 2; });
}

Right now, you're passing the input by value. This means when you call the function with some vector, a copy of the original vector will normally be made and passed to the function. As a general rule, something like a vector that's potentially large and slow to copy should be passed by reference to const, as shown in the code above.

...where each closing brace is vertically aligned with the beginning of the block it closes. As a side-note, there are almost endless debates about the efficacy of various bracing styles. I'm not going to advocate for or against any of the well known styles, but I think there's a fair amount to be gained from using a style that's well known, and then using it consistently. I don't see much to gain from style that's different from what almost anybody else uses.

added 394 characters in body
Source Link
Jerry Coffin
  • 34.1k
  • 4
  • 77
  • 144
Loading
Source Link
Jerry Coffin
  • 34.1k
  • 4
  • 77
  • 144
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /