- initial size of a std::vector of std::strings - 8 Updates
Lynn McGuire <lynnmcguire5@gmail.com>: Dec 07 04:28PM -0600 On 12/7/2022 2:23 AM, Öö Tiib wrote: > 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. Thanks, Lynn |
Bo Persson <bo@bo-persson.se>: Dec 07 11:45PM +0100 On 2022-12-07 at 21:54, Lynn McGuire wrote: > }; > Visual Studio 2015 does not like > std::vector <std::string> component_names (max_ncp); You are not allowed to use round parenthesis in default member initializers, because of possible confusion with function declarations ("Most vexing parse"). Like std::vector <std::string> component_names (); is a member function returning a vector, not a default initialized variable. > But it does like > std::vector <std::string> component_names [max_ncp]; > But it does not work. Define "work". :-) This creates an array of vectors of strings. Probably not what you want, anyway. You CAN use an {} initializer for the vector std::vector <std::string> component_names {max_ncp, ""}; but have to be REALLY careful not to trigger an initializer_list constructor when things inside the {} is convertible to the vector value type. Because then it will do something completely different! Like vector<int> v {max_ncp, 0}; will create a vector with two elements, max_ncp and 0. Sigh! |
Keith Thompson <Keith.S.Thompson+u@gmail.com>: Dec 07 02:47PM -0800 >> 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<std::string> component_names(max_ncp); or this: std::vector<std::string> 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 */ |
Bo Persson <bo@bo-persson.se>: Dec 07 11:48PM +0100 On 2022-12-07 at 22:34, Michael S wrote: >> Lynn > Works fine on godbolt's vs2015 > https://godbolt.org/z/8WPo33Yn1 That's for a local variable inside a function, where std::vector <std::string> bar(x); works. However, as a struct member parenthesis are not allowed. Because reasons. |
Keith Thompson <Keith.S.Thompson+u@gmail.com>: Dec 07 02:53PM -0800 >> 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<std::string, max_ncp> 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 */ |
Lynn McGuire <lynnmcguire5@gmail.com>: Dec 07 04:56PM -0600 On 12/7/2022 4:53 PM, Keith Thompson wrote: > or a plain array of std::string: > std::vector formulas[max_ncp]; > Both require max_ncp to be a constant expression. Yup, either would work well for me. max_ncp is a const int that only changes at a far future recompile time. const int max_ncp = 1000; Thanks, Lynn |
Lynn McGuire <lynnmcguire5@gmail.com>: Dec 07 05:08PM -0600 On 12/7/2022 4:45 PM, Bo Persson wrote: > will create a vector with two elements, max_ncp and 0. Sigh! >> Thanks, >> Lynn Thanks, Lynn |
Lynn McGuire <lynnmcguire5@gmail.com>: Dec 07 05:09PM -0600 On 12/7/2022 4:47 PM, Keith Thompson wrote: > 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? The complaint was strange. Like it was getting confused. Thanks, Lynn |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment