Re: any function to handle this kind of counting?



Gerry Quinn wrote:
> In article <dcdi1t$g7m$1@xxxxxxxxxxxxxxxxxxxxxxxx>, a@xxxxxxxxxxx
> says...
> > Dear all,
> >
> > i have generated a 2000 X 1 vector, e.g.
>
> > and then i want to count, say, for "1", there are two A's, one E, ...
> >
> > it seems that a for loop can do the job but as we don't know how many
> > instances there are, i.e. don't know A, C, E and then what will occur in
> > this corresponding vector, is there any function which is devoted to handle
> > this kind of counting problem? thx!!
>
> Well, I guess there are libraries for linked lists, vectors etc. There
> are also sorting functions that can often make such problems easier.
>
> The following is untested C++ to count them directly.
>
> I will treat both vectors as containing ints though I'll call them
> 'nums' and 'lets'. It's trivial to convert the second to and from
> chars if required (e.g. just use val = letter - 'A'.) Besides, they
> could be enums for all I know. The algorithm should be modified if
> speed is at a premium and there are a large number of different letter
> values.
>
> 'targ' is the required number, 1 in the example. One could modify it
> easily to return a complete solution for all numbers as a vector of
> vectors, using a similar methodology.
>
> If the data are in fact C-style arrays rather than vectors, it's just
> as easy to implement, but using an STL vector for the result makes the
> solution very easy in C++:
>
> #include <vector>
> using namespace std;
>
> class Datum
> {
> public:
> int letter;
> int count;
> };
>
> vector< Datum > result;
>
> ASSERT( nums.size() == lets.size() );
>
> for ( int iLet = 0; iLet < lets.size(); iLet++ )
> {
> if ( nums[ iLet ] != targ )
> {
> continue;
> }
> int iRes = 0;
> while ( iRes < result.size() )
> {
> if ( result[ iRes ].letter == lets[ iLet ] )
> {
> break;
> }
> iRes++;
> }
> if ( iRes == result.size() )
> {
> Datum datum;
> datum.letter = lets[ iLet ];
> datum.count = 0;
> result.push_back( datum );
> }
> result[ iRes ].count++;
> }

The above is fine if you use a Microsoft C++ compiler.

In standard C++ there is no function or macro called "ASSERT", there is
a macro called "assert".

The two do opposite things:

In debug builds "ASSERT" will bomb if the condition in parens is true.
In debug builds "assert" will bomb if the condition in parens is not
true.
Neither are compiled in release builds.

I know this isn't a standard C++ only newsgroup, but I thought I would
mention the above since it's rather confusing.

.



Relevant Pages

  • Re: friend CStringT operator+(const CStringT& str1, const CStringT& st
    ... but I am using class derived from CString that ends up calling ... > CStringT strResult(str1.GetManager()); ... > str1_GetLength 9 int ... > Ignore the assert and the result is: ...
    (microsoft.public.vc.atl)
  • Re: Segfault City
    ... Richard Heathfield wrote: ... assert(isdigit(ch)); ... int ctoi ... The C language has Macros, ...
    (comp.lang.c)
  • Re: memcmp() checker: memory access errors
    ... First, you malloc 'data' and never free it; ... files opened in binary mode, ... Never put anything inside 'assert' that you want to be evaluated. ... int probe_img ...
    (comp.lang.c)
  • Re: Converting Bitmap into 2D-Array
    ... // (Does bounds-checking in debug builds, using ASSERT macro.) ... explicit Matrix2D(int rows, int cols) ... ASSERT(cols> 0); ... int Columnsconst ...
    (microsoft.public.vc.language)
  • Re: [Full-disclosure] bash vulnerability?
    ... > It's not nice to brag about finding 0-day bullshit in the bash fork ... > bomb that has been Zalewski's signature for years:P ... int main { ... such financial profit, political mileage, or good joke as it lends itself to. ...
    (Full-Disclosure)