std::string
constructors can be tricky. It helps to use a good reference, such as the cppreference wiki.
It's not clear exactly what "a string of length 10" should be without providing other details. You might want,
- ten whitespace characters, like
std::string variable1( 10, ' ' );
- a string variable which is empty, but can get up to ten characters without allocating additional memory, like
std::string variable1; variable1.reserve( 10 );
If you use the generic container methods of std::string
such as resize
, or pass ''
NUL characters instead of whitespace, then those NULs will be treated as part of the string. That could break usual text-oriented functionality and cause conflicting results from C and C++ based logic.
If you really want exactly ten characters starting as NUL, and not a class with an append
operation and such, then try an array like std::array<char,10> variable1
or char variable1[10]
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…