Skip to main content
added 206 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

@rolfl has mentioned some great points, so I won't repeat those. I'll address some C++-specific things I've found, keeping in mind that Boost and C++11 shouldn't be mentioned.

  • These using statements:

      using std::cout;
      using std::endl;
      using std::string;
    

    are okay to use, but I'd still use std:: where needed, especially if this list ends up getting longer. But this is still better than having using namespace std.

  • Regarding the use of char* []:

    For this:

      int elements = sizeof(test_strings) / sizeof(test_strings[0]);
    

    elements should be of type std::size_t, which is the return type of the sizeof operator. You should also give it a more accurate name, such as numberOfElements.

    Moreover, you shouldn't be needing to do this in C++ when you have access to the standard library. You can instead have an std::vector of std::strings instead of char* [].

    ThisFor instance, this is what test_strings would look like:

      std::vector<std::string> test_strings = { /* string 1 */, /* string n */ };
    

    To get the size, just use size():

      std::vector<std::string>::size_type elements = test_strings.size();
    

    This type should also be used when incrementing through the number of elements.

    Instead of using test_strings[0] to access the first element, you can use front():

      test_strings.front();
    

    I've also done a quick example test of all this herehere.

@rolfl has mentioned some great points, so I won't repeat those. I'll address some C++-specific things I've found, keeping in mind that Boost and C++11 shouldn't be mentioned.

  • These:

      using std::cout;
      using std::endl;
      using std::string;
    

    are okay to use, but I'd still use std:: where needed, especially if this list ends up getting longer. But this is still better than having using namespace std.

  • For this:

      int elements = sizeof(test_strings) / sizeof(test_strings[0]);
    

    elements should be of type std::size_t, which is the return type of the sizeof operator.

    Moreover, you shouldn't be needing to do this in C++ when you have access to the standard library. You can instead have an std::vector of std::strings instead of char* [].

    This is what test_strings would look like:

      std::vector<std::string> test_strings = { /* string 1 */, /* string n */ };
    

    To get the size, just use size():

      std::vector<std::string>::size_type elements = test_strings.size();
    

    This type should also be used when incrementing through the number of elements.

    I've also done a quick test here.

@rolfl has mentioned some great points, so I won't repeat those. I'll address some C++-specific things I've found, keeping in mind that Boost and C++11 shouldn't be mentioned.

  • These using statements:

      using std::cout;
      using std::endl;
      using std::string;
    

    are okay to use, but I'd still use std:: where needed, especially if this list ends up getting longer. But this is still better than having using namespace std.

  • Regarding the use of char* []:

    For this:

      int elements = sizeof(test_strings) / sizeof(test_strings[0]);
    

    elements should be of type std::size_t, which is the return type of the sizeof operator. You should also give it a more accurate name, such as numberOfElements.

    Moreover, you shouldn't be needing to do this in C++ when you have access to the standard library. You can instead have an std::vector of std::strings instead of char* [].

    For instance, this is what test_strings would look like:

      std::vector<std::string> test_strings = { /* string 1 */, /* string n */ };
    

    To get the size, just use size():

      std::vector<std::string>::size_type elements = test_strings.size();
    

    This type should also be used when incrementing through the number of elements.

    Instead of using test_strings[0] to access the first element, you can use front():

      test_strings.front();
    

    I've also done a quick example test of all this here.

Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

@rolfl has mentioned some great points, so I won't repeat those. I'll address some C++-specific things I've found, keeping in mind that Boost and C++11 shouldn't be mentioned.

  • These:

      using std::cout;
      using std::endl;
      using std::string;
    

    are okay to use, but I'd still use std:: where needed, especially if this list ends up getting longer. But this is still better than having using namespace std.

  • For this:

      int elements = sizeof(test_strings) / sizeof(test_strings[0]);
    

    elements should be of type std::size_t, which is the return type of the sizeof operator.

    Moreover, you shouldn't be needing to do this in C++ when you have access to the standard library. You can instead have an std::vector of std::strings instead of char* [].

    This is what test_strings would look like:

      std::vector<std::string> test_strings = { /* string 1 */, /* string n */ };
    

    To get the size, just use size():

      std::vector<std::string>::size_type elements = test_strings.size();
    

    This type should also be used when incrementing through the number of elements.

    I've also done a quick test here.