In Array C++ Header Adaptation (in_array)
Description
As a frequent user of arrays to organize information in a program, I often find myself missing PHP and Java's APIs when it comes to finding an element in an array.
This basic header file allows a similar functionality to PHP's in_array, with some added uses (such as an index callback).
As of the moment it is limited to arrays of integers, characters, and strings - but is easily adapted to more.
Usage
#include <iostream>
#include "in_array.h"
using namespace std;
int main(){
int intArr[5] = { 1, 2, 9, 5, 7 };
int foundIndex;
if( in_array( 7, intArr ) )
cout << "We found it!" << endl;
else
cout << "Not found." << endl;
if( in_array( 99, intArr, &foundIndex ) )
cout << "Found at " << foundIndex << endl;
else
cout << "Not found." << endl;
string strArr[] = { "Apples", "Oranges", "Yum!" };
if( in_array( "Yum!", strArr, &foundIndex ) )
cout << "Found at " << foundIndex << endl;
else
cout << "Not found." << endl;
char charArr[] = { 'A', 'B', 'C', 'D', 'E' };
if( in_array( 'D', charArr, &foundIndex ) )
cout << "Found at " << foundIndex << endl;
else
cout << "Not found." << endl;
return 0;
}
Outputs:
We found it!
Not found.
Found at 2
Found at 3
API
bool in_array( needle, haystack[], int *indexCallback );
Checks to see if a value needle is in array haystack. Works with int, char, and string. If found, the variable passed into the *indexCallback parameter will now equal the index where the value was first found at.Returns true if found (
), false otherwise (
).bool in_array( needle, haystack[]);
Operates the same as above, but no indexCallback variable needs to be passed in. This is used for simply checking if a value is in an array.Returns true if found (
), false otherwise (
).
Code
in_array.h - v1.0.2
/*************************************************
* In Array C++ Header Adaptation (in_array)
* Version: 1.1.0
* Coded by: Shane Chism <http://shanechism.com>
* Updates: http://shanechism.com/code/static/13
* Distributed under the GNU Public License
*************************************************/
#ifndef IN_ARRAY_H
#define IN_ARRAY_H
#include <string>
using namespace std;
bool in_array( int needle, int haystack[], int *indexCallback = NULL );
bool in_array( char needle, char haystack[], int *indexCallback = NULL );
bool in_array( string needle, string haystack[], int *indexCallback = NULL );
bool in_array( int needle, int haystack[], int *indexCallback ){
for( int i = 0; haystack[i] != '\0'; i++ ){
if( haystack[i] == needle ){
if( indexCallback != NULL )
*indexCallback = i;
return true;
}
}
return false;
}
bool in_array( char needle, char haystack[], int *indexCallback ){
for( int i = 0; haystack[i] != '\0'; i++ ){
if( haystack[i] == needle ){
if( indexCallback != NULL )
*indexCallback = i;
return true;
}
}
return false;
}
bool in_array( string needle, string haystack[], int *indexCallback ){
for( int i = 0; (int)haystack[i].c_str() > 0; i++ ){
if( haystack[i].compare( needle ) == 0 ){
if( indexCallback != NULL )
*indexCallback = i;
return true;
}
}
return false;
}
#endif // IN_ARRAY_H

