🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #9494

Re: How to determine the Number of Elements in an Array?

#9494
From: PC Hua
Date: Mon, 05 Jul 2004 23:41
53 lines
2517 bytes
Thank you for :
   sizeof( SomeStr ) / sizeof( SomeStr[0] )

Since I'm using the array combined with 'map' there's another problem. I'm 
trying to process a XML file. Although the array is static I want to keep 
it easy to be modified.
std::string	A_Attrib[]={"a0","a1","a2"};
std::string	B_Attrib[]={"b0"};

typedef	map< std::string, std::string*>	NodeType;
NodeType	Node;

// Initialize
Node["A"]= A_Attrib;
Node["B"]= B_Attrib;

i.e. when "A" comes in it will go through A_Attrib, B_Attrib for "B".
Unfortunately, sizeof doesn't work on Node["A"] because it's not an array 
of the elements which I want to count.

Now I have solved it by:
typedef	map< std::string, int>	NodeSizeType;

NodeSizeType	NodeSz["A"]= sizeof( A_Attrib)/ sizeof( A_Attrib[0]);
NodeSizeType	NodeSz["B"]= sizeof( B_Attrib)/ sizeof( B_Attrib[0]);

It sounds very complicate :-) Maybe someone knows a better way.

Phuoc Can HUA

On Mon, 5 Jul 2004 08:36:58 -0700, Dennis Jones <nospam@bogus.com> wrote:

> As Andrue has already said, you can do this:
>
> number of elements = sizeof( SomeStr ) / sizeof( SomeStr[0] )
>
> HOWEVER, this will only work for a static array -- that is, an array 
> whose
> size is defined at compile-time.  The only way to determine the number of
> elements in an array whose size is not static is to use a sentry value as
> the last element.  Then you iterate through the array, counting elements 
> as
> you go, and stop when you get to the sentry value.
>
> - Dennis
>
>



-- 
_/\__/\__/\__/\__/\__/\_
www.folksfun.com
Technical Details
Message-ID: <opsaoursodpmxkmr@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news-hog.berkeley.edu!ucberkeley!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <opsam505lcpmxkmr@newsgroups.borland.com> <40e9759b$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,befc51f674d02011
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: How to determine the Number of Elements in an Array?
References: <opsam505lcpmxkmr@newsgroups.borland.com> <40e9759b$1@newsgroups.borland.com>
Message-ID: <opsaoursodpmxkmr@newsgroups.borland.com>
From: PC Hua <home-AT-huaonline.DOT.com>
Content-Type: text/plain; format=flowed; charset=iso-8859-15
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Date: Tue, 06 Jul 2004 09:41:42 +1000
User-Agent: Opera7.21/Win32 M2 build 3218
NNTP-Posting-Host: 203.194.21.162
X-Trace: newsgroups.borland.com 1089070917 203.194.21.162 (5 Jul 2004 16:41:57 -0700)
Lines: 53
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news-hog.berkeley.edu!ucberkeley!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail