On Mar 7, 10:32*pm, James Kanze <james.ka...@gmail.com> wrote:
> On Mar 7, 6:05 pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
>
>
>
>
>
>
>
>
>
> > On 3/7/2011 12:17 PM, Angus wrote:
> > > using lower_bound I want to pass a 2-dimensional array eg like this:
> > > * * static const int tbl[5][3] = {
> > > * * * *1000, 800, 801,
> > > * * * *1001, 802, 803,
> > > * * * *1002, 804, 805,
> > > * * * *1003, 806, 807,
> > > * * * *1004, 808, 809
> > > * * };
> > > I want to search in only the first column - eg the numbers 1000-1004.
> > > I am trying to find the position in this 2-dimensional array, and then
> > > once located I can access elements[position][1] and elements[position]
> > > [2].
> > > I tried this:
> > > * * int arrsize = sizeof(tbl)/sizeof(tbl[0]);
> > > * * low=lower_bound(tbl[0], tbl[0]+arrsize, 7);
> > > But get compile error C2440 cannot convert from 'const int *' to 'int
> > > *'
>
> That's because you're passing tbl[0], instead of just tbl.
>
> Of course, if you pass tbl, tbl+arrsize, you'll still get an
> error when lower_bound trys to do int[3] < 7. *Or something like
> that: C style arrays are pretty broken, and you can get some
> strange effects from them. *I recently had to do something
> similar, but with several different arrays with different
> numbers of rows. *The template function to handle this was
> something like:
>
> * * template <size_t N, size_t M>
> * * std::string const (&
> * * * * * * findIt(std::string const(&target)[N][M],
> * * * * * * * * * *int id_column,
> * * * * * * * * * *std::string const& key)
> * * * * )[M]
> * * {
> * * * * // *...
> * * }
>
> (From memory, so it might not be exact.)
>
> Anyway, what you need to do is pass tbl and tbl + arrsize (type
> int (*)[3]), and provide a less than operator along the lines of
>
> * * struct LessThanFirstOfThree
> * * {
> * * * * bool operator()(int (&arr)[3], int other) const
> * * * * {
> * * * * * * return arr[0] < other;
> * * * * }
> * * * * bool operator()(int other, int (&arr)[3]) const
> * * * * {
> * * * * * * return other < arr[0];
> * * * * }
> * * };
>
> (That's without trying it, so there are likely some typos or
> other errors, but that's the general idea.)
>
> > > How can I do this?
> > You should probably provide your own iterator, which when incremented
> > will skip the rest of the "row".
>
> I'm not sure that that's the solution. *He has an array of [] of
> [3] int; an iterator into it will designate and array of [3]
> int. *Doing this with C style arrays and pointers can lead to
> some very unnatural syntax, but is doable. *Typedef's can help a
> lot. *(In my example above, I couldn't use a typedef because it
> was a template, but my earlier versions, before it became a
> template, used typedef's, and weren't that bad.) *Something
> like:
>
> * * typedef int Row[3];
> * * Row const array[] = { ... };
>
> * * Row const& element = std::lower_bound( begin( array ),
> * * * * * * * * * * * * * * * * * * * * * *end( array ),
> * * * * * * * * * * * * * * * * * * * * * *int key,
> * * * * * * * * * * * * * * * * * * * * * *CompareFirst() );
>
> (with CompareFirst something like the above.)
>
> --
> James Kanze
Hello
I am trying to progress with code as simple as possible as a learning
exercise.
Here is my code which finds if code in column 1, but not if number
searched is less than any item in first column.
What am I doing wrong?
Here is code:
#pragma warning(disable: 4786)
#include <iostream>
#include <algorithm>
using namespace std;
struct errorinfo
{
errorinfo() : category(0), error(0) {}
int category;
int error;
};
bool lessthanrow1(const int (&arr)[3], const int other)
{
return arr[0] < other;
}
bool mapBW2CSTAError(const int bw_error, errorinfo& csta_error)
{
const int cols = 3;
//in real life much bigger array.
static const int tbl[6][cols] = {
1000, 10, 0,
1001, 11, 1,
1200, 12, 2,
4002, 13, 3,
8090, 14, 4,
121105, 15, 5
};
int tblsize = sizeof(tbl) /( sizeof(int) * cols);
int* result = (int*)lower_bound(tbl, tbl+tblsize, bw_error,
lessthanrow1);
int idx = ((result - (int*)tbl) / cols);
if(idx != tblsize){
csta_error.category = tbl[idx][1];
csta_error.error = tbl[idx][2];
} else {
cout << "item not found\n";
return false;
}
cout << "Your error: " << bw_error << " category=" << tbl[idx][1]
<< " errcode=" << tbl[idx][2] << endl;
return true;
}
int main(){
//4002 found ok. 90 returns first element in array. large number
is ok.
errorinfo err;
if(mapBW2CSTAError(4002, err))
cout << "error code used for lookup: " << 4002 << ", type: " <<
err.category << ", specific error code: " << err.error << endl;
else
cout << "error 4002 not recognised\n";
if(mapBW2CSTAError(90, err))
cout << "error code used for lookup: " << 90 << ", type: " <<
err.category << ", specific error code: " << err.error << endl;
else
cout << "error 90 not recognised\n";
if(mapBW2CSTAError(999999, err))
cout << "error code used for lookup: " << 999999 << ", type: "
<< err.category << ", specific error code: " << err.error << endl;
else
cout << "error 999999 not recognised\n";
return 0;
}