Results 1 to 5 of 5

Thread: silly array element counter..

  1. #1
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default silly array element counter..

    Ok.. first of all I am a bit new to STL, however I am having this problem in C++.

    I have got a function defined as

    int MyClass::MyString(string str[])
    {
    //code

    }

    Is there any way to find out how many elements of array are being passed to the function.
    I have no way to know how many functions are being passed but only know that the constratints would be say 1 to 100 as I don't have the code that calls the function and am only supposed to write this single class.

    doing (sizeof(str)/sizeof(str[0])) returns "1" and is of no use..

    is there some way to find count the elements of this string array..
    Last edited by ct; 1st March 2006 at 20:11.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: silly array element counter..

    No, there is no way. But you can use a std::vector instead of an array. This way you'll be able to retrieve the number of object it contains.

  3. #3
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: silly array element counter..

    Stroustrup recommends not using arrays. Many coding standards insist on never using arrays.

    See "What's wrong with arrays?" at
    http://public.research.att.com/~bs/bs_faq2.html

  4. #4
    Join Date
    Mar 2006
    Posts
    22
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: silly array element counter..

    Unless your array is NULL terminated, you have no way of finding out. This is why you often see array length as argument to array functions.

  5. #5
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Thumbs up Re: silly array element counter..

    It may be nice to not impose a particular type of container (like std::vector) on the callers of the int MyClass::MyString method. Sometimes, I like to make these types of methods templatized on an iterator type like this:
    Qt Code:
    1. template< class IteratorType >
    2. int MyClass::MyString(const IteratorType &iterBegin, const IteratorType &iterEnd)
    3. {
    4. // Get a count of items passed in.
    5. const size_t nNumItems = iterEnd-iterBegin;
    6.  
    7. // Iterate over the input
    8. IteratorType iter;
    9. for(iter = iterBegin; iter != iterEnd; ++iter)
    10. {
    11. // Do something here.
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Ben.Hines; 3rd March 2006 at 16:02.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.