Skip to main content
Code Review

Return to Answer

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

Putting using namespace std at the top of every program is a bad habit a bad habit that you'd do well to avoid.

All versions of both standards since then (C99 and C++98) have maintained the same idea. We rely on automatically generated member functions in C++, and few people write explicit return; statements at the end of a void function. Reasons against omitting seem to boil down to "it looks weird" "it looks weird". If, like me, you're curious about the rationale for the change to the C standard read this question read this question. Also note that in the early 1990s this was considered "sloppy practice" because it was undefined behavior (although widely supported) at the time.

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid.

All versions of both standards since then (C99 and C++98) have maintained the same idea. We rely on automatically generated member functions in C++, and few people write explicit return; statements at the end of a void function. Reasons against omitting seem to boil down to "it looks weird". If, like me, you're curious about the rationale for the change to the C standard read this question. Also note that in the early 1990s this was considered "sloppy practice" because it was undefined behavior (although widely supported) at the time.

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid.

All versions of both standards since then (C99 and C++98) have maintained the same idea. We rely on automatically generated member functions in C++, and few people write explicit return; statements at the end of a void function. Reasons against omitting seem to boil down to "it looks weird". If, like me, you're curious about the rationale for the change to the C standard read this question. Also note that in the early 1990s this was considered "sloppy practice" because it was undefined behavior (although widely supported) at the time.

fixed some typos
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
#include <iostream>
#include <string>
#include <unordered_map>
int main() {
 std::unordered_map<std::string, unsigned> dict;
 for (std::string word; std::cin >> word; ) {
 ++dict[word];
 }
 std::cout<<cout << "number of distinct words are: " << dict.size(); << "\n";
}

There is a significant peformanceperformance benefit. I used both the original and this version to count all the words in the Project Gutenberg eBook of Dracula, by Bram Stoker both. Both correctly reported 19027 distinct words, but the original took 1.011 s and the version above took 0.057 s (17 times faster).

#include <iostream>
#include <string>
#include <unordered_map>
int main() {
 std::unordered_map<std::string, unsigned> dict;
 for (std::string word; std::cin >> word; ) {
 ++dict[word];
 }
 std::cout<< "number of distinct words are: " << dict.size();
}

There is a significant peformance benefit. I used both the original and this version to count all the words in the Project Gutenberg eBook of Dracula, by Bram Stoker both correctly reported 19027 distinct words, but the original took 1.011 s and the version above took 0.057 s (17 times faster).

#include <iostream>
#include <string>
#include <unordered_map>
int main() {
 std::unordered_map<std::string, unsigned> dict;
 for (std::string word; std::cin >> word; ) {
 ++dict[word];
 }
 std::cout << "number of distinct words are: " << dict.size() << "\n";
}

There is a significant performance benefit. I used both the original and this version to count all the words in the Project Gutenberg eBook of Dracula, by Bram Stoker. Both correctly reported 19027 distinct words, but the original took 1.011 s and the version above took 0.057 s (17 times faster).

mentioned using a newer book
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284

Use a newer book

The book you have was fine in its day, but it is now woefully out of date. I'd recommend Stroustrup's book "A Tour of C++" instead, since the current edition covers C++11 which is a very much improved and much different language than previous versions of C++.

Omit return 0

Omit return 0

Use a newer book

The book you have was fine in its day, but it is now woefully out of date. I'd recommend Stroustrup's book "A Tour of C++" instead, since the current edition covers C++11 which is a very much improved and much different language than previous versions of C++.

Omit return 0

Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
Loading
lang-cpp

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