Language: C++


// #include <bits/stdc++.h>
using namespace std;

#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define ALL( a )        (a).begin(), (a).end()             

bool binarySearch( vector<int>& a, int key );

int main( void ) {
	int n; cin >> n;
	vector<int> s( n );
	REP( i, n ) cin >> s[i];

	int q, key, sum = 0; cin >> q;
	REP( i, q ) {
		cin >> key;
		if( binarySearch( s, key ) ) sum++;
	}

	cout << sum << endl;

}

bool binarySearch( vector<int>& a, int key ) {
	int left = 0, right = a.size();
	
	while( left < right ) {
		int mid = ( left + right ) / 2;
		if( a[mid] == key ) return true;
		else if( key < a[mid] ) {
			right = mid;
		} else if( key > a[mid] ) {
			left = mid + 1;
		}
	}
	return false;
}


Compiler output:
a.cc:9:20: error: 'vector' was not declared in this scope
    9 | bool binarySearch( vector<int>& a, int key );
      |                    ^~~~~~
a.cc:1:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
  +++ |+#include <vector>
    1 | 
a.cc:9:27: error: expected primary-expression before 'int'
    9 | bool binarySearch( vector<int>& a, int key );
      |                           ^~~
a.cc:9:36: error: expected primary-expression before 'int'
    9 | bool binarySearch( vector<int>& a, int key );
      |                                    ^~~
a.cc:9:44: error: expression list treated as compound expression in initializer [-fpermissive]
    9 | bool binarySearch( vector<int>& a, int key );
      |                                            ^
a.cc: In function 'int main()':
a.cc:12:16: error: 'cin' was not declared in this scope
   12 |         int n; cin >> n;
      |                ^~~
a.cc:1:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
  +++ |+#include <iostream>
    1 | 
a.cc:13:9: error: 'vector' was not declared in this scope
   13 |         vector<int> s( n );
      |         ^~~~~~
a.cc:13:9: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
a.cc:13:16: error: expected primary-expression before 'int'
   13 |         vector<int> s( n );
      |                ^~~
a.cc:14:28: error: 's' was not declared in this scope
   14 |         REP( i, n ) cin >> s[i];
      |                            ^
a.cc:19:35: error: 's' was not declared in this scope
   19 |                 if( binarySearch( s, key ) ) sum++;
      |                                   ^
a.cc:19:33: error: 'binarySearch' cannot be used as a function
   19 |                 if( binarySearch( s, key ) ) sum++;
      |                     ~~~~~~~~~~~~^~~~~~~~~~
a.cc:22:9: error: 'cout' was not declared in this scope
   22 |         cout << sum << endl;
      |         ^~~~
a.cc:22:9: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:22:24: error: 'endl' was not declared in this scope
   22 |         cout << sum << endl;
      |                        ^~~~
a.cc:1:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
  +++ |+#include <ostream>
    1 | 
a.cc: At global scope:
a.cc:26:6: error: redefinition of 'bool binarySearch'
   26 | bool binarySearch( vector<int>& a, int key ) {
      |      ^~~~~~~~~~~~
a.cc:9:6: note: 'bool binarySearch' previously defined here
    9 | bool binarySearch( vector<int>& a, int key );
      |      ^~~~~~~~~~~~
a.cc:26:20: error: 'vector' was not declared in this scope
   26 | bool binarySearch( vector<int>& a, int key ) {
      |                    ^~~~~~
a.cc:26:20: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
a.cc:26:27: error: expected primary-expression before 'int'
   26 | bool binarySearch( vector<int>& a, int key ) {
      |                           ^~~
a.cc:26:36: error: expected primary-expression before 'int'
   26 | bool binarySearch( vector<int>& a, int key ) {
      |                                    ^~~