Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #87720

Re: initial size of a std::vector of std::strings

From "Alf P. Steinbach" <alf.p.steinbach@gmail.com>
Newsgroups comp.lang.c++
Subject Re: initial size of a std::vector of std::strings
Date 2022-12-07 12:28 +0100
Organization A noiseless patient Spider
Message-ID <tmptcb$hm5s$1@dont-email.me> (permalink)
References <tmp947$g0jv$1@dont-email.me>

Show all headers | View raw


On 7 Dec 2022 06:42, Lynn McGuire wrote:
> Is there a way to set the size of std::vector <std::string> formulas to 
> max_ncp size ?
> 
> Something like "std::vector <std::string> formulas [max_ncp];"

The most reasonable interpretation to me is that you're asking how to 
set the initial /capacity/ of the vector in the declaration, to avoid an 
explicit .reserve() invocation as a separate following statement.

Presumably because you declare such vectors in many places.

To do that you can to wrap the vector, either via a factory function or 
via a simple class, like

     struct Capacity{ int value; };

     struct Fast_stringvec:
         public vector<string>
     {
         template< class... Args >
         Fast_stringvec( const Capacity capacity, Args&&... args ):
             vector<string>( forward<Args>( args )... )
         {
             reserve( capacity.value );
         }
     };

Then do e.g.

     auto formulas = Fast_stringvec( Capacity( max_ncp ) );

... where I believe the use of `auto` helps avoid the Most Vexing Parse. ;-)

If `max_ncp` is a constant you can add a constructor that uses that by 
default.

---

A not so reasonable interpretation is that you're asking how to make the 
vector contain `max_ncp` strings initially, for which you can just do

     vector<string> formulas( max_ncp, ""s );

---

Crossing the line to perhaps unreasonable interpretation is that you're 
asking how you can make it so that when you add a new to string to the 
vector, the new string item will guaranteed be of size `max_ncp`.

For that you need to make the item type one that holds a `string` of 
exactly that size.

It would probably be necessary to take control of copying to such items, 
to avoid inadvertent size changes.


- Alf

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

initial size of a std::vector of std::strings Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-06 23:42 -0600
  Re: initial size of a std::vector of std::strings James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-12-07 00:52 -0500
  Re: initial size of a std::vector of std::strings Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-07 08:17 +0100
  Re: initial size of a std::vector of std::strings Öö Tiib <ootiib@hot.ee> - 2022-12-07 00:23 -0800
    Re: initial size of a std::vector of std::strings Juha Nieminen <nospam@thanks.invalid> - 2022-12-07 09:13 +0000
      Re: initial size of a std::vector of std::strings Öö Tiib <ootiib@hot.ee> - 2022-12-07 06:03 -0800
        Re: initial size of a std::vector of std::strings Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-07 15:37 +0100
          Re: initial size of a std::vector of std::strings Öö Tiib <ootiib@hot.ee> - 2022-12-07 09:33 -0800
            Re: initial size of a std::vector of std::strings Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-07 18:45 +0100
      Re: initial size of a std::vector of std::strings Paavo Helde <eesnimi@osa.pri.ee> - 2022-12-07 16:34 +0200
        Re: initial size of a std::vector of std::strings Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-07 15:38 +0100
        Re: initial size of a std::vector of std::strings Juha Nieminen <nospam@thanks.invalid> - 2022-12-08 07:58 +0000
    Re: initial size of a std::vector of std::strings Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-07 16:28 -0600
      Re: initial size of a std::vector of std::strings Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-12-07 14:53 -0800
        Re: initial size of a std::vector of std::strings Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-07 16:56 -0600
        Re: initial size of a std::vector of std::strings Juha Nieminen <nospam@thanks.invalid> - 2022-12-08 08:03 +0000
  Re: initial size of a std::vector of std::strings "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-12-07 12:28 +0100
    Re: initial size of a std::vector of std::strings Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-07 14:54 -0600
      Re: initial size of a std::vector of std::strings "daniel...@gmail.com" <danielaparker@gmail.com> - 2022-12-07 13:16 -0800
      Re: initial size of a std::vector of std::strings "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-12-07 22:33 +0100
        Re: initial size of a std::vector of std::strings Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-07 22:37 +0100
      Re: initial size of a std::vector of std::strings Michael S <already5chosen@yahoo.com> - 2022-12-07 13:34 -0800
        Re: initial size of a std::vector of std::strings Bo Persson <bo@bo-persson.se> - 2022-12-07 23:48 +0100
      Re: initial size of a std::vector of std::strings Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-12-07 13:49 -0800
        Re: initial size of a std::vector of std::strings Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-07 16:01 -0600
          Re: initial size of a std::vector of std::strings Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-12-07 14:47 -0800
            Re: initial size of a std::vector of std::strings Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-07 17:09 -0600
      Re: initial size of a std::vector of std::strings Bo Persson <bo@bo-persson.se> - 2022-12-07 23:45 +0100
        Re: initial size of a std::vector of std::strings Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-07 17:08 -0600
  Re: initial size of a std::vector of std::strings Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-08 01:44 -0600

csiph-web