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. Know when to use it and when not to (as when writing include headers). In this particular case, I happen to think it's perfectly appropriate because it's a single short program that and not a header. Some people seem to think it should never be used under any circumstance, but my view is that it can be used as long as it is done responsibly and with full knowledge of the consequences.

This is almost always wrong almost always wrong. Instead, you could make this both simpler and more correct by writing it like this:

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid. Know when to use it and when not to (as when writing include headers). In this particular case, I happen to think it's perfectly appropriate because it's a single short program that and not a header. Some people seem to think it should never be used under any circumstance, but my view is that it can be used as long as it is done responsibly and with full knowledge of the consequences.

This is almost always wrong. Instead, you could make this both simpler and more correct by writing it like this:

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid. Know when to use it and when not to (as when writing include headers). In this particular case, I happen to think it's perfectly appropriate because it's a single short program that and not a header. Some people seem to think it should never be used under any circumstance, but my view is that it can be used as long as it is done responsibly and with full knowledge of the consequences.

This is almost always wrong. Instead, you could make this both simpler and more correct by writing it like this:

added const references
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
void pushFile(const std::vector<std::string>string>& totalList, const std::stringstring& fileName);
void pushFile(std::vector<std::string> totalList, std::string fileName);
void pushFile(const std::vector<std::string>& totalList, const std::string& fileName);
fixed code formatting
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284

Now the entire function is much cleaner, shorter and easier to read: void pushFile(const std::vectorstd::string& totalList, const std::string& fileName) { std::fstream ioFile(fileName); if (ioFile) { for (const auto &item : totalList) { ioFile << item << '\n'; } } }

void pushFile(const std::vector<std::string>& totalList, 
 const std::string& fileName)
{
 std::fstream ioFile(fileName);
 if (ioFile) {
 for (const auto &item : totalList) {
 ioFile << item << '\n';
 }
 }
}

Now the entire function is much cleaner, shorter and easier to read: void pushFile(const std::vectorstd::string& totalList, const std::string& fileName) { std::fstream ioFile(fileName); if (ioFile) { for (const auto &item : totalList) { ioFile << item << '\n'; } } }

Now the entire function is much cleaner, shorter and easier to read:

void pushFile(const std::vector<std::string>& totalList, 
 const std::string& fileName)
{
 std::fstream ioFile(fileName);
 if (ioFile) {
 for (const auto &item : totalList) {
 ioFile << item << '\n';
 }
 }
}
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
Loading
lang-cpp

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