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:47:11 -0800 Organization: None to speak of Lines: 62 Message-ID: <87a63y6dcg.fsf@nosuchdomain.example.com> References: <87edta6g0a.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="e9284c35170f8649df9b0e58c7fb123c"; logging-data="711267"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19PuR7eaMRpaAfEE10R7XKX" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:f0CmcrT1Gm9lN6SveBkJhGQyTN0= sha1:8xPtIrJajoGbvN7fO1bq9D2Js3g= Xref: csiph.com comp.lang.c++:87740 Lynn McGuire writes: > On 12/7/2022 3:49 PM, Keith Thompson wrote: >> Lynn McGuire writes: >> [...] >>> I am trying to declare a vector with max_ncp strings in a struct. >>> struct component_data { >>> std::vector component_names [max_ncp]; >>> doublereal heatoffusionatmeltingpoint [max_ncp]; >>> doublereal meltingpointtemperature [max_ncp]; >>> doublereal sublimationtemperature [max_ncp]; >>> doublereal triplepointtemperature [max_ncp]; >>> doublereal triplepointpressure [max_ncp]; >>> }; >>> >>> Visual Studio 2015 does not like >>> std::vector component_names (max_ncp); >>> or >>> std::vector component_names (max_ncp, ""); >> How does it express its dislike? Both work for me: >> #include >> #include >> #include >> int main() { >> int max_ncp = 42; >> { >> std::vector component_names(max_ncp); >> std::cout << component_names.size() << ' '; >> } >> { >> std::vector component_names(max_ncp, ""); >> std::cout << component_names.size() << '\n'; >> } >> } >> The output is "42 42". >> >>> But it does like >>> std::vector component_names [max_ncp]; >>> But it does not work. >> That defines an array (not a std::array, just a C-style array >> object) of >> max_ncp vectors. I think you want a single vector. >> Do you want to set the initial size or the initial capacity? > > One vector with max_ncp strings in it. So the initial state of the vector should be that it has a size() of max_ncp, and each of the max_ncp strings it contains is initialized to -- what, the empty string? Then either this: std::vector component_names(max_ncp); or this: std::vector component_names(max_ncp, ""); should give you want you want. You said Visual Studio 2015 doesn't like either of those, but you didn't say what it's complaint was. Is the size of the vector going to change after it's been created? -- 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 */