Assigning Default string Values

M

Mike Copeland

During execution of my programs, I repeatedly initialize a number of
basic::strings to default lengths and values - usually blanks (but not
always). Currently, I create a single string variable of the largest
needed size and use it with .substr() to assign the default values.
Is there a better way? TIA
 
V

Victor Bazarov

During execution of my programs, I repeatedly initialize a number of
basic::strings to default lengths and values - usually blanks (but not
always). Currently, I create a single string variable of the largest
needed size and use it with .substr() to assign the default values.
Is there a better way? TIA

There is a c-tor that takes a char and a number. You don't need to use
substr on a longer string to get a short one with all spaces, or zeros,
for instance.

std::string tenspaces(10, ' ');

V
 
M

Mike Copeland

There is a c-tor that takes a char and a number. You don't need to use
substr on a longer string to get a short one with all spaces, or zeros,
for instance.

std::string tenspaces(10, ' ');
Not sure how this helps me: I need to repeatedly assign default
values for various data fields. I'm converting file data records (csv
input) which, as they're read, may or may not have content for the data
fields. Each time I start converting an input record to a field-field
output record, I have to "initialize" all data fields to their default
(empty or "null" values...which may be blanks, zeroes, whatever).
In such a process, I'm not engaging a c-tor each time - so how would
this solution help me? 8<}}
 
S

Stefan Ram

fields. Each time I start converting an input record to a field-field
output record, I have to "initialize" all data fields to their default

Are you looking for »str.assign( 10, ' ' )«?
 
V

Victor Bazarov

Not sure how this helps me: I need to repeatedly assign default
values for various data fields. I'm converting file data records (csv
input) which, as they're read, may or may not have content for the data
fields. Each time I start converting an input record to a field-field
output record, I have to "initialize" all data fields to their default
(empty or "null" values...which may be blanks, zeroes, whatever).
In such a process, I'm not engaging a c-tor each time - so how would
this solution help me? 8<}}

std::string somestring;
...
somestring = std::string(10, ' ');

That uses the move assignment operator that essentially constructs a
temporary in place. Or you could use the 'assign' trick Stefan mentioned.

Of course assigning "default" values to string that have something in
them still involves releasing the memory they hold and replacing it with
newly allocated memory portion... That's inefficient. It would be
better to review your process. Perhaps adding a flag that means "this
string has a default value of such and such specification" is actually
better (allows for lazy [re]allocation). IOW, redesign your classes to
give your fields a bit more sophistication.

V
 
S

Stefan Ram

Victor Bazarov said:
them still involves releasing the memory they hold and replacing it with
newly allocated memory portion... That's inefficient. It would be

Or, possibly, though it might be frowned upon, using a
char[] buffer and a strfil function can avoid reallocation.

template<typename C>
inline void strfil( C * p, C const c, int n ){ while( n-- )*p++ = c; }

OTOH, maybe an ::std::string class can observe when an
assigned string will fit nicely in the current buffer
and avoid a reallocation in this case.
 
J

James Kanze

std::string somestring;
...
somestring = std::string(10, ' ');
That uses the move assignment operator that essentially constructs a
temporary in place. Or you could use the 'assign' trick Stefan mentioned.
Of course assigning "default" values to string that have something in
them still involves releasing the memory they hold and replacing it with
newly allocated memory portion... That's inefficient. It would be
better to review your process. Perhaps adding a flag that means "this
string has a default value of such and such specification" is actually
better (allows for lazy [re]allocation). IOW, redesign your classes to
give your fields a bit more sophistication.

Using "somestring.assign( 10, ' ' )" shouldn't reallocate unless
somestring.capacity() < 10.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top