The string class provides an assign method to assign a new value to a given string. Captions
1. string& assign ( const string& str ); 2. string& assign ( const string& str, size_t pos, size_t n ); 3. string& assign ( const char* s, size_t n ); 4. string& assign ( const char* s ); 5. string& assign ( size_t n, char c ); 6. template <class InputIterator> string& assign ( InputIterator first, InputIterator last );
Citation source: cplusplus.com (I recommend this site because it gives you a very detailed link to the standard C ++ libraries).
I think that you are looking for something like the fifth of these functions: n indicates the desired length of your string and c character filled in this string. For example, if you write
sTemp.assign(10, 'b');
your line will be filled only 10 b.
Initially, I suggested using the std::fill STL algorithm, but at the same time, the length of your string remains unchanged. The string::resize method provides a way to resize a string and fills the added characters with the specified value - but only the added ones are set. Finally string::assign remains the best approach!
phlipsy
source share