Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #87714 > unrolled thread
| Started by | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| First post | 2022-12-06 23:42 -0600 |
| Last post | 2022-12-08 01:44 -0600 |
| Articles | 10 on this page of 30 — 11 participants |
Back to article view | Back to comp.lang.c++
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
Page 2 of 2 — ← Prev page 1 [2]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2022-12-07 22:37 +0100 |
| Message-ID | <tmr12k$l979$1@dont-email.me> |
| In reply to | #87733 |
Sorry, this Lady is writing code with several hundered thousand lines of code and has issues with such things ... ???
[toc] | [prev] | [next] | [standalone]
| From | Michael S <already5chosen@yahoo.com> |
|---|---|
| Date | 2022-12-07 13:34 -0800 |
| Message-ID | <c8b03013-7bd5-4227-b668-dc6e839d9ad1n@googlegroups.com> |
| In reply to | #87731 |
On Wednesday, December 7, 2022 at 10:54:23 PM UTC+2, Lynn McGuire wrote:
> On 12/7/2022 5:28 AM, Alf P. Steinbach wrote:
> > 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
> I am trying to declare a vector with max_ncp strings in a struct.
> struct component_data {
> std::vector <std::string> 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 <std::string> component_names (max_ncp);
> or
> std::vector <std::string> component_names (max_ncp, "");
>
> But it does like
> std::vector <std::string> component_names [max_ncp];
> But it does not work.
>
> Thanks,
> Lynn
Works fine on godbolt's vs2015
https://godbolt.org/z/8WPo33Yn1
[toc] | [prev] | [next] | [standalone]
| From | Bo Persson <bo@bo-persson.se> |
|---|---|
| Date | 2022-12-07 23:48 +0100 |
| Message-ID | <jvcji3F7nudU2@mid.individual.net> |
| In reply to | #87734 |
On 2022-12-07 at 22:34, Michael S wrote:
> On Wednesday, December 7, 2022 at 10:54:23 PM UTC+2, Lynn McGuire wrote:
>> On 12/7/2022 5:28 AM, Alf P. Steinbach wrote:
>>> 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
>> I am trying to declare a vector with max_ncp strings in a struct.
>> struct component_data {
>> std::vector <std::string> 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 <std::string> component_names (max_ncp);
>> or
>> std::vector <std::string> component_names (max_ncp, "");
>>
>> But it does like
>> std::vector <std::string> component_names [max_ncp];
>> But it does not work.
>>
>> Thanks,
>> 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.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2022-12-07 13:49 -0800 |
| Message-ID | <87edta6g0a.fsf@nosuchdomain.example.com> |
| In reply to | #87731 |
Lynn McGuire <lynnmcguire5@gmail.com> writes:
[...]
> I am trying to declare a vector with max_ncp strings in a struct.
> struct component_data {
> std::vector <std::string> 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 <std::string> component_names (max_ncp);
> or
> std::vector <std::string> component_names (max_ncp, "");
How does it express its dislike? Both work for me:
#include <vector>
#include <string>
#include <iostream>
int main() {
int max_ncp = 42;
{
std::vector<std::string> component_names(max_ncp);
std::cout << component_names.size() << ' ';
}
{
std::vector<std::string> component_names(max_ncp, "");
std::cout << component_names.size() << '\n';
}
}
The output is "42 42".
> But it does like
> std::vector <std::string> 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?
--
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 */
[toc] | [prev] | [next] | [standalone]
| From | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| Date | 2022-12-07 16:01 -0600 |
| Message-ID | <tmr2ft$laqd$1@dont-email.me> |
| In reply to | #87736 |
On 12/7/2022 3:49 PM, Keith Thompson wrote:
> Lynn McGuire <lynnmcguire5@gmail.com> writes:
> [...]
>> I am trying to declare a vector with max_ncp strings in a struct.
>> struct component_data {
>> std::vector <std::string> 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 <std::string> component_names (max_ncp);
>> or
>> std::vector <std::string> component_names (max_ncp, "");
>
> How does it express its dislike? Both work for me:
>
> #include <vector>
> #include <string>
> #include <iostream>
>
> int main() {
> int max_ncp = 42;
> {
> std::vector<std::string> component_names(max_ncp);
> std::cout << component_names.size() << ' ';
> }
> {
> std::vector<std::string> component_names(max_ncp, "");
> std::cout << component_names.size() << '\n';
> }
> }
>
> The output is "42 42".
>
>> But it does like
>> std::vector <std::string> 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.
Thanks,
Lynn
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2022-12-07 14:47 -0800 |
| Message-ID | <87a63y6dcg.fsf@nosuchdomain.example.com> |
| In reply to | #87737 |
Lynn McGuire <lynnmcguire5@gmail.com> writes:
> On 12/7/2022 3:49 PM, Keith Thompson wrote:
>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>> [...]
>>> I am trying to declare a vector with max_ncp strings in a struct.
>>> struct component_data {
>>> std::vector <std::string> 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 <std::string> component_names (max_ncp);
>>> or
>>> std::vector <std::string> component_names (max_ncp, "");
>> How does it express its dislike? Both work for me:
>> #include <vector>
>> #include <string>
>> #include <iostream>
>> int main() {
>> int max_ncp = 42;
>> {
>> std::vector<std::string> component_names(max_ncp);
>> std::cout << component_names.size() << ' ';
>> }
>> {
>> std::vector<std::string> component_names(max_ncp, "");
>> std::cout << component_names.size() << '\n';
>> }
>> }
>> The output is "42 42".
>>
>>> But it does like
>>> std::vector <std::string> 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<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 */
[toc] | [prev] | [next] | [standalone]
| From | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| Date | 2022-12-07 17:09 -0600 |
| Message-ID | <tmr6ef$lk9a$4@dont-email.me> |
| In reply to | #87740 |
On 12/7/2022 4:47 PM, Keith Thompson wrote:
> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>> On 12/7/2022 3:49 PM, Keith Thompson wrote:
>>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>>> [...]
>>>> I am trying to declare a vector with max_ncp strings in a struct.
>>>> struct component_data {
>>>> std::vector <std::string> 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 <std::string> component_names (max_ncp);
>>>> or
>>>> std::vector <std::string> component_names (max_ncp, "");
>>> How does it express its dislike? Both work for me:
>>> #include <vector>
>>> #include <string>
>>> #include <iostream>
>>> int main() {
>>> int max_ncp = 42;
>>> {
>>> std::vector<std::string> component_names(max_ncp);
>>> std::cout << component_names.size() << ' ';
>>> }
>>> {
>>> std::vector<std::string> component_names(max_ncp, "");
>>> std::cout << component_names.size() << '\n';
>>> }
>>> }
>>> The output is "42 42".
>>>
>>>> But it does like
>>>> std::vector <std::string> 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<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?
The complaint was strange. Like it was getting confused.
Thanks,
Lynn
[toc] | [prev] | [next] | [standalone]
| From | Bo Persson <bo@bo-persson.se> |
|---|---|
| Date | 2022-12-07 23:45 +0100 |
| Message-ID | <jvcjd3F7nudU1@mid.individual.net> |
| In reply to | #87731 |
On 2022-12-07 at 21:54, Lynn McGuire wrote:
> On 12/7/2022 5:28 AM, Alf P. Steinbach wrote:
>> 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
>
> I am trying to declare a vector with max_ncp strings in a struct.
> struct component_data {
> std::vector <std::string> 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 <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.
> or
> std::vector <std::string> component_names (max_ncp, "");
>
> 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!
>
> Thanks,
> Lynn
[toc] | [prev] | [next] | [standalone]
| From | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| Date | 2022-12-07 17:08 -0600 |
| Message-ID | <tmr6da$lk9a$3@dont-email.me> |
| In reply to | #87739 |
On 12/7/2022 4:45 PM, Bo Persson wrote:
> On 2022-12-07 at 21:54, Lynn McGuire wrote:
>> On 12/7/2022 5:28 AM, Alf P. Steinbach wrote:
>>> 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
>>
>> I am trying to declare a vector with max_ncp strings in a struct.
>> struct component_data {
>> std::vector <std::string> 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 <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.
>
>> or
>> std::vector <std::string> component_names (max_ncp, "");
>>
>> 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!
>
>>
>> Thanks,
>> Lynn
Thanks,
Lynn
[toc] | [prev] | [next] | [standalone]
| From | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| Date | 2022-12-08 01:44 -0600 |
| Message-ID | <tms4kl$1l5q$1@gioia.aioe.org> |
| In reply to | #87714 |
On 12/6/2022 11:42 PM, 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];" > > Thanks, > Lynn Thanks all ! Lynn
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c++
csiph-web