Skip to main content
Code Review

Return to Answer

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

See Why is using namespace std in C++ is considered bad practice on why you should avoid using directives at the global scope.

See Why is using namespace std in C++ is considered bad practice on why you should avoid using directives at the global scope.

added 10 characters in body
Source Link
Snowhawk
  • 6.8k
  • 1
  • 19
  • 37
std::vector<int> split(const std::string& s){
 // ......
 while (std::getline(ss, item, ' ')) {
 // ......
}
std::vector<int> split(const string& s){
 // ......
 while (getline(ss, item, ' ')) {
 // ......
}
std::vector<int> split(const std::string& s){
 // ......
 while (std::getline(ss, item, ' ')) {
 // ......
}
Fixed Grammar/Spelling and organized talking points better.
Source Link
Snowhawk
  • 6.8k
  • 1
  • 19
  • 37

Horizontal spacing helps readability by distinguishing separate language constructs. Unless you are code golfing, you should write code for the correctness and readability.

Every time you call split(), you are passing the arguments in by value. Pass Prefer to pass by value when parameters are cheap to copy. Pass Otherwise, pass by reference (to const if immutable) when parameters are expensive to copy or have an unknown cost. Determining the cost is architecture dependent. For most systems today, anything more the two or three words (size of a double or reference) should be considered expensive.

When creating the sequence, elems.push_back() will do many reallocations. The amount of reallocations is implementation defined bydependant upon the std::vectors growth factor. The sequence length is one of the input parameters of the problem. Use that length to reserve the space before reading.

A better approach, avoid reading as a std::string and tokenizing. Read the arguments as their intended type. Ensure you reserve space in your std::vector before reading values.

99% of problems on these code challenge sites will produce TLE's because of your algorithm and not the I/O method being employed. In the case of scanf/printf vs std::cin/std::cout, I prefer the latter for these types of exercises. If you really need thatthe performance of C formatted I/O from C++ IOStreams, consider the following:

  • Disable synchronization (std::ios::sync_with_stdio(false);),

    Disable synchronization

     std::ios::sync_with_stdio(false);
    
  • Untie the streams (std::cin.tie(nullptr);), and

    Untie the streams.

     std::cin.tie(nullptr);
    
  • Avoid unnecessary flushes (prefer '\n' to std::endl).

    Avoid unnecessary flushes (prefer '\n' to std::endl).

A better approach, avoid reading as a std::string and tokenizing. Read the arguments as their intended type. Ensure you reserve space in your std::vector before reading values.

Horizontal spacing helps readability by distinguishing separate language constructs. Unless you are code golfing, you should write code for the correctness and readability.

Every time you call split(), you are passing the arguments in by value. Pass by value when parameters are cheap to copy. Pass by reference (to const if immutable) when parameters are expensive to copy or have an unknown cost. Determining the cost is architecture dependent. For most systems today, anything more the two or three words (size of a double or reference) should be considered expensive.

When creating the sequence, elems.push_back() will do many reallocations. The amount of reallocations is implementation defined by the std::vectors growth factor. The sequence length is one of the input parameters of the problem. Use that length to reserve the space before reading.

A better approach, avoid reading as a std::string and tokenizing. Read the arguments as their intended type. Ensure you reserve space in your std::vector before reading values.

99% of problems on these code challenge sites will produce TLE's because of your algorithm and not the I/O method being employed. In the case of scanf/printf vs std::cin/std::cout, I prefer the latter for these types of exercises. If you really need that performance from C++ IOStreams

  • Disable synchronization (std::ios::sync_with_stdio(false);),
  • Untie the streams (std::cin.tie(nullptr);), and
  • Avoid unnecessary flushes (prefer '\n' to std::endl).

Horizontal spacing helps readability by distinguishing language constructs. Unless you are code golfing, you should write code for correctness and readability.

Every time you call split(), you are passing the arguments in by value. Prefer to pass by value when parameters are cheap to copy. Otherwise, pass by reference (to const if immutable) when parameters are expensive to copy or have an unknown cost. Determining the cost is architecture dependent. For most systems today, anything more the two or three words (size of a double or reference) should be considered expensive.

When creating the sequence, elems.push_back() will do many reallocations. The amount of reallocations is implementation dependant upon the std::vectors growth factor. The sequence length is one of the input parameters of the problem. Use that length to reserve the space before reading.

99% of problems on these code challenge sites will produce TLE's because of your algorithm and not the I/O method being employed. In the case of scanf/printf vs std::cin/std::cout, I prefer the latter for these types of exercises. If you really need the performance of C formatted I/O from C++ IOStreams, consider the following:

  • Disable synchronization

     std::ios::sync_with_stdio(false);
    
  • Untie the streams.

     std::cin.tie(nullptr);
    
  • Avoid unnecessary flushes (prefer '\n' to std::endl).

A better approach, avoid reading as a std::string and tokenizing. Read the arguments as their intended type. Ensure you reserve space in your std::vector before reading values.

added 2 characters in body
Source Link
Snowhawk
  • 6.8k
  • 1
  • 19
  • 37
Loading
Source Link
Snowhawk
  • 6.8k
  • 1
  • 19
  • 37
Loading
lang-cpp

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