C++ vector size types

前端 未结 5 1917
醉话见心
醉话见心 2021-02-12 13:49

I just started learning C++ and have a question about vectors. The book I\'m reading states that if I want to extract the size of a vector of type double (for example), I should

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-12 14:10

    C++ is a language for library writing*, and allowing the author to be as general as possible is one of its key strengths. Rather than prescribing the standard containers to use any particular data type, the more general approach is to decree that each container expose a size_type member type. This allows for greater flexibility and genericity. For example, consider this generic code:

    template