Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: initial size of a std::vector of std::strings Date: Wed, 07 Dec 2022 14:53:39 -0800 Organization: None to speak of Lines: 34 Message-ID: <875yem6d1o.fsf@nosuchdomain.example.com> References: <6762abc6-85ac-4173-b807-aa27c36a04bcn@googlegroups.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: reader01.eternal-september.org; posting-host="e9284c35170f8649df9b0e58c7fb123c"; logging-data="711267"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+5Y+c4iOaaU9fY24eu5q27" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:Yu42qJw6A3J7BqCNoecYC6vfen4= sha1:2pt4O92Ytks2bTUp6QgDqgdLFQw= Xref: csiph.com comp.lang.c++:87742 Lynn McGuire writes: > On 12/7/2022 2:23 AM, Öö Tiib wrote: >> On Wednesday, 7 December 2022 at 07:42:49 UTC+2, Lynn McGuire wrote: >>> Is there a way to set the size of std::vector formulas to >>> max_ncp size ? >>> >>> Something like "std::vector formulas [max_ncp];" >> When that max_ncp is compile-time known immutable value then >> std::array can be what you want. >> Maybe worth to note that I've seen reserve(n) of std::vector >> overused >> in practice as bad pessimization. Vector is typically designed so that >> we get log(max) allocations and max times element copies because >> of vector just growing freely during its life-time. That can be turned into >> way worse by someone micromanaging it. > > Now I am wondering if I should be using std::array instead of std::vector. Only if the size of the vector will never change after it's created (I don't think you mentioned that). And in that case, you could use either a std::array of std::string: std::array formulas; or a plain array of std::string: std::vector formulas[max_ncp]; Both require max_ncp to be a constant expression. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for XCOM Labs void Void(void) { Void(); } /* The recursive call of the void */