Edit: Solutions must compile against Microsoft Visual Studio 2012.
I want to use a known string length to declare another string of the same length.
The reasoning is the second string will act as a container for operation done to the first string which must be non volatile with regards to it.
e.g.
const string messy "a bunch of letters";
string dostuff(string sentence) {
string organised NNN????? // Idk, just needs the same size.
for ( x = 0; x < NNN?; x++) {
organised[x] = sentence[x]++; // Doesn't matter what this does.
}
}
In both cases above, the declaration and the exit condition, the NNN? stands for the length of 'messy'.
How do I discover the length at compile time?
std::string
isn't a compile time type (it can't be aconstexpr
), so you can't use it directly to determine the length at compile time.You could initialize a
constexpr
char[]
and then usesizeof
on that:and use that, but frankly, that's pretty ugly; the length would be compile time, but
organized
would need to use thecount
andchar
constructor that would still be performed on each call, allocating and initializing only to have the contents replaced in the loop.While it's not compile time, you'd avoid that initialization cost by just using
reserve
and+=
to build the newstring
, which with the#define
could be done in an ugly but likely efficient way as:This avoids setting
organised
's values more than once, allocating more than once (well, possibly twice if initial construction performs it) per call, and only performs a single read/write pass ofsentence
, no full read followed by read/write or the like. It also makes the loop constraint a compile time value, so the compiler has the opportunity to unroll the loop (though there is no guarantee of this, and even if it happens, it may not be helpful).Also note: In your example, you mutate
sentence
, but it's accepted by value, so you're mutating the local copy, not the caller copy. If mutation of the caller value is required, accept it by reference, and if mutation is not required, accept byconst
reference to avoid a copy on every call (I understand the example code was filler, just mentioning this).std::string
has two constructors which could fit your purposes.The first, a copy constructor:
The second, a constructor which takes a character and a count. You could initialize a string with a temporary character.
Alternatively, you can:
+=
) text to it as you go along, orstd::stringstream
for the same purpose.the stringstream will likely be more efficient.
Overall, I would prefer the copy constructor if the length is known.