count

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MOHIT0RMAYANK
    New Member
    • Sep 2007
    • 1

    count

    how to count the no. in an array or list?
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by MOHIT0RMAYANK
    how to count the no. in an array or list?
    I cant get you.
    Can you explain a bit more clearly

    Raghuram

    Comment

    • oler1s
      Recognized Expert Contributor
      • Aug 2007
      • 671

      #3
      Are you asking how you would get the length of either of those data structures? Iterate through each node in whatever structure, adding to a counter. At the end, you have the length. In an array, it is just iteration through elements, and in a list, it is iteration through the nodes.

      Comment

      • mschenkelberg
        New Member
        • Jun 2007
        • 44

        #4
        Any easier way to get the length of an array would be to do


        int arr[100];
        int size = sizeof(arr)/sizeof(int);

        you can do this for any data type

        hell, if you're using stl c++ data types like deque, list, or any others, it is simply
        list foo;
        foo.size();

        or stl string type use length

        cppreference.co m

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by mschenkelberg
          or stl string type use length
          std::string::le ngth() is deprecated. You are supposed to use std::string::si ze() like the other STL containers.

          Comment

          Working...