Solved

# how to compare integers in an array

Posted on 2002-05-27
318 Views
Lets say I have 2 arrays:
----------------------------------------
int* a=new int[9]
int* b=new int[9]

a=[1,2,3,4,5,6,7,8,9]
b=[1,2,3,4,6,4,3,2,1]
----------------------------------------
compare(a,b,first_5) => false

Now.. how can I compare first 5 (or first n) integers, without having to use a while loop (or for)?
Is there a faster way at all (then to use a loop)?
Thank you!

Jurij
0
Question by:jurij

LVL 86

Accepted Solution

jkr earned 200 total points
You can use 'memcmp()':

memcmp ( a, b, 5 * sizeof ( int));

This will do a binary compare for the first 5 elements in the arrays, which is equal to compare each of the elements.

Here's a smple from the VC++ docs

Example

/* MEMCMP.C: This program uses memcmp to compare
* the strings named first and second. If the first
* 19 bytes of the strings are equal, the program
* considers the strings to be equal.
*/

#include <string.h>
#include <stdio.h>

void main( void )
{
char first[]  = "12345678901234567890";
char second[] = "12345678901234567891";
int result;

printf( "Compare '%.19s' to '%.19s':\n", first, second );
result = memcmp( first, second, 19 );
if( result < 0 )
printf( "First is less than second.\n" );
else if( result == 0 )
printf( "First is equal to second.\n" );
else if( result > 0 )
printf( "First is greater than second.\n" );
printf( "Compare '%.20s' to '%.20s':\n", first, second );
result = memcmp( first, second, 20 );
if( result < 0 )
printf( "First is less than second.\n" );
else if( result == 0 )
printf( "First is equal to second.\n" );
else if( result > 0 )
printf( "First is greater than second.\n" );
}

Output

Compare '1234567890123456789' to '1234567890123456789':
First is equal to second.
Compare '12345678901234567890' to '12345678901234567891':
First is less than second.

0

Expert Comment

I think the suggested call to memcmp is the fastest route, simply because it has been optimized by previous programmers. Conceptually speaking, keep in mind that any such comparison, including memcmp itself, needs to use a loop of some description.
0

## Join & Write a Comment Already a member? Login.

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base classâ€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationâ€¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

#### 772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!