Society of Robots - Robot Forum
Software => Software => Topic started by: sseb4 on August 29, 2007, 05:15:34 AM
-
hey i need a way of comparing say 1 array eg n[10] to predetermined arrays that matches n[10]. n[10] will be scanned/input into the program. How could i do this in C++. would strcmp or memcmp work? i dont really know how to use memcmp as i dont hae a copy of a compiler for C++ at home. Please give me an example of how to do this
-
I'm pretty sure std::vector defines an operator ==.... So if you use that container, you can just compare all elements at once with that. Otherwise, just use a for loop and compare each individual element. If one does not match, then the arrays don't have the same content.
-
My solution would be as following
bool CompareArrays() {
for(int x = 0; x < 10; x++) {
if(array1[x] != array2[x]) {
return false;
}
}
return true;
}
void main() {
if(CompareArrays == true) {
print("wooohoooo");
}
//not sure about the 'print' command, I'm a C++ coder, so I use cout<< "blabla";
}
-
would strcmp or memcmp work?
definitely :)
Make sure though that your both arrays use the same data type. No use in doing a compare if one of your arrays contains 2-byte shorts and the other contains 1-byte chars.
MEMCMP(3) NEWLIB MEMCMP(3)
NAME
6.7 `memcmp'--compare two memory areas
SYNOPSIS
#include <string.h>
int memcmp(const void *S1, const void *S2, size_t N);
DESCRIPTION
This function compares not more than N characters of the object pointed
to by S1 with the object pointed to by S2.
RETURNS
The function returns an integer greater than, equal to or less than
zero according to whether the object pointed to by S1 is greater
than, equal to or less than the object pointed to by S2.
PORTABILITY
`memcmp' is ANSI C.
`memcmp' requires no supporting OS subroutines.
SEE ALSO
memcmp is part of the libc library. The full documentation for libc is
maintained as a Texinfo manual. If info and libc are properly
installed at your site, the command
info libc
will give you access to the complete manual.
NEWLIB 2006 Aug 09 MEMCMP(3)
-
if(array1[x] != array2[x]) {
return false;}
hmmm i would have just done a for loop cause im a noob like that . . .
does that also work in C, or just C++?
-
Noob or not... that is the only way you can compare two arrays. In loop of somekind (for, while, do-while).
Simple stuff like loops, conditionals, arrays are the same in C and C++.
From wikipedia:
Compared to the C language, C++ introduced extra features, including declarations as statements, function-like casts, new/delete, bool, reference types, inline functions, default arguments, function and operator overloading, namespaces and the scope resolution (::) operator, classes (including all class-related features such as inheritance, member functions, virtual functions, abstract classes, and constructors), templates, exception handling, runtime type identification, and the overloaded input (>>) and output (<<) operators for input and output respectively.
So as long you dont use this stuff in C++ you basicly have C :)
-
Further analysis of an array (based on the specifications) shows that these are allocated as a contiguous memory area.
A loop is OK, but as I've previously mentioned, a memcmp will work as efficiently.
Less code to write in fact, and as always less is always more.
Noob or not... that is the only way you can compare two arrays. In loop of somekind (for, while, do-while).