Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

###1. Performance

1. Performance

###2. General tips

2. General tips

###3. Final code

3. Final code

###1. Performance

###2. General tips

###3. Final code

1. Performance

2. General tips

3. Final code

spelling and formatting
Source Link
Toby Speight
  • 87.9k
  • 14
  • 104
  • 325

1. Performance ###1. Performance

Just as Loki said, you should use std::lower_bound()std::lower_bound() and std::distance()std::distance() to solve this task, because std::find()std::find() has higher complexity.

2. General tips ###2. General tips

  1. Do not include unused libraries (in this case, <cmath> and <cstdio>).

  2. Only declare variables when you need them. Declaring all the variables at the beginning of the program is old C style practice, which is discouraged in C++.

  3. Prefer prefix to postfix operator++. The postfix version creates a copy, so if you do not need that copy, you should use the prefix version.

  4. If you have access to C++11, you should consider using auto more. It'll make your code easier to read and save you some typing.

  5. You can safely ommitomit return 0 from main();

[C++11: 3.6.1/5]: A return statement in mainmain has the effect of leaving the mainmain function (destroying any objects with automatic storage duration) and calling std::exitstd::exit with the return value as the argument. If control reaches the end of mainmain without encountering a returnreturn statement, the effect is that of executing return 0;return 0;.

3. Final code ###3. Final code

1. Performance

Just as Loki said, you should use std::lower_bound() and std::distance() to solve this task, because std::find() has higher complexity.

2. General tips

  1. Do not include unused libraries (in this case, <cmath> and <cstdio>).

  2. Only declare variables when you need them. Declaring all the variables at the beginning of the program is old C style practice, which is discouraged in C++.

  3. Prefer prefix to postfix operator++. The postfix version creates a copy, so if you do not need that copy, you should use the prefix version.

  4. If you have access to C++11, you should consider using auto more. It'll make your code easier to read and save you some typing.

  5. You can safely ommit return 0 from main();

[C++11: 3.6.1/5]: A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

3. Final code

###1. Performance

Just as Loki said, you should use std::lower_bound() and std::distance() to solve this task, because std::find() has higher complexity.

###2. General tips

  1. Do not include unused libraries (in this case, <cmath> and <cstdio>).

  2. Only declare variables when you need them. Declaring all the variables at the beginning of the program is old C style practice, which is discouraged in C++.

  3. Prefer prefix to postfix operator++. The postfix version creates a copy, so if you do not need that copy, you should use the prefix version.

  4. If you have access to C++11, you should consider using auto more. It'll make your code easier to read and save you some typing.

  5. You can safely omit return 0 from main();

[C++11: 3.6.1/5]: A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;.

###3. Final code

Source Link
Kodnot
  • 454
  • 3
  • 13

Although you've probably already solved this problem, I'll still post my review for completion's sake.

1. Performance

Just as Loki said, you should use std::lower_bound() and std::distance() to solve this task, because std::find() has higher complexity.

2. General tips

  1. Do not include unused libraries (in this case, <cmath> and <cstdio>).

  2. Only declare variables when you need them. Declaring all the variables at the beginning of the program is old C style practice, which is discouraged in C++.

  3. Prefer prefix to postfix operator++. The postfix version creates a copy, so if you do not need that copy, you should use the prefix version.

  4. If you have access to C++11, you should consider using auto more. It'll make your code easier to read and save you some typing.

  5. You can safely ommit return 0 from main();

[C++11: 3.6.1/5]: A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

  1. Give your variables better names. Although other variable names in your code are good, a vector named v is not very descriptive.

  2. Use const_iterators for functions that do not alter the original range.

3. Final code

#include <vector>
#include <iostream>
#include <algorithm>
int main() {
 int N;
 std::cin >> N;
 
 std::vector<int> numbers(N);
 //populate the vector
 for(int i = 0; i < N; ++i) {
 std::cin >> numbers[i];
 }
 
 int queryLength;
 std::cin >> queryLength; 
 for(int j = 0; j < queryLength; ++j) {
 int query;
 std::cin >> query;
 auto lowerBoundIt = std::lower_bound(numbers.cbegin(), numbers.cend(), query);
 if(*lowerBoundIt == query) {
 std::cout << "Yes ";
 }
 else {
 std::cout << "No ";
 }
 std::cout << std::distance(numbers.cbegin(), lowerBoundIt) + 1 << '\n';
 }
}
lang-cpp

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