Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

In your hash function, you're doing x.size() in a loop, which will slow the function down quite a lot for a long string. Instead, store the size in a variable and compare to that

int hash(const string &x)
{
 int y=0;
 int xLength = x.size();
 for (int i = 0; i < xLength; ++i){
 y+=x[i]*137;
 }
 return y%1000;
}

Also, you're using namespace::std. Don't do that, for good reasons Don't do that, for good reasons. As well as this, if you're having to explain your variable names that last for a while and actually require explanation (I'm looking at V, u, v, c), maybe name them something else? Or at least include comments if they're the convention that people would expect that would give someone who doesn't know the convention a chance.

In your hash function, you're doing x.size() in a loop, which will slow the function down quite a lot for a long string. Instead, store the size in a variable and compare to that

int hash(const string &x)
{
 int y=0;
 int xLength = x.size();
 for (int i = 0; i < xLength; ++i){
 y+=x[i]*137;
 }
 return y%1000;
}

Also, you're using namespace::std. Don't do that, for good reasons. As well as this, if you're having to explain your variable names that last for a while and actually require explanation (I'm looking at V, u, v, c), maybe name them something else? Or at least include comments if they're the convention that people would expect that would give someone who doesn't know the convention a chance.

In your hash function, you're doing x.size() in a loop, which will slow the function down quite a lot for a long string. Instead, store the size in a variable and compare to that

int hash(const string &x)
{
 int y=0;
 int xLength = x.size();
 for (int i = 0; i < xLength; ++i){
 y+=x[i]*137;
 }
 return y%1000;
}

Also, you're using namespace::std. Don't do that, for good reasons. As well as this, if you're having to explain your variable names that last for a while and actually require explanation (I'm looking at V, u, v, c), maybe name them something else? Or at least include comments if they're the convention that people would expect that would give someone who doesn't know the convention a chance.

Source Link
Yann
  • 2.2k
  • 1
  • 17
  • 36

In your hash function, you're doing x.size() in a loop, which will slow the function down quite a lot for a long string. Instead, store the size in a variable and compare to that

int hash(const string &x)
{
 int y=0;
 int xLength = x.size();
 for (int i = 0; i < xLength; ++i){
 y+=x[i]*137;
 }
 return y%1000;
}

Also, you're using namespace::std. Don't do that, for good reasons. As well as this, if you're having to explain your variable names that last for a while and actually require explanation (I'm looking at V, u, v, c), maybe name them something else? Or at least include comments if they're the convention that people would expect that would give someone who doesn't know the convention a chance.

lang-cpp

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