46

If a string's length is determined at compile-time, how can I properly initialize it?

#include <string>
int length = 3;
string word[length]; //invalid syntax, but doing `string word = "   "` will work
word[0] = 'a'; 
word[1] = 'b';
word[2] = 'c';

...so that i can do something like this?

Example: http://ideone.com/FlniGm

My purpose for doing this is because I have a loop to copy characters from certain areas of another string into a new string.

2
  • You mean at compile time? If it's at runtime just stuff some letters into it and std::string will figure out the rest. If you do mean compile time then no, std::string doesn't support this. Commented Dec 9, 2014 at 2:25
  • Yes, sorry I meant compile time Commented Dec 9, 2014 at 2:25

6 Answers 6

68

A string is mutable and it's length can changed at run-time. But you can use the "fill constructor" if you must have a specified length: http://www.cplusplus.com/reference/string/string/string/

std::string s6 (10, 'x');

s6 now equals "xxxxxxxxxx".

Sign up to request clarification or add additional context in comments.

1 Comment

If you are resizing only to overwrite the contents afterwards, you can avoid writing the buffer twice using reserve() or resize_and_overwrite() (the latter C++23 only).
6

You can initialize your string like this:

string word = "abc"

or

string word(length,' ');
word[0] = 'a';
word[1] = 'b';
word[2] = 'c';

5 Comments

This does not work if the string to copy is not known until compile time.
@penu why would you do a[0] = 'h'; a[1] = 'i'; instead of the more natural a = "hi";
@greatwolf I have a for loop to copy characters from certain areas of another string
@penu ok then just do a += other_str;. The way you're accessing a[i] right now is wrong because there's no guarantee std::string has allocated up to the index you're trying to access. IOW assert(i < a.length());.
@penu Sorry for the misunderstanding. If you want to initialize it character by character like that, you have to allocate the right number of indices.
6

How about the following?

string word;
word.resize(3);
word[0] = 'a';
word[1] = 'b';
word[2] = 'c';

More on resizing a string: http://www.cplusplus.com/reference/string/string/resize/

1 Comment

Do you mean word.reserve() instead of word.resize()? word.resize() without specifying another character puts null characters into the string, and these may have bad side effects later, if the application does not fill the string all the way up to the reserved size. word.reserve() reserves at least the provided minimum capacity, but doesn't change the string length and thus gives the application flexibility in filling it.
5

You are probably looking for:

string word(3, ' ');

Comments

5

std::string does not support lengths known at compile time. There's even a proposal for adding compile time strings to the C++ standard.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4121.pdf

For now you're out of luck. What you can do is use static const char[] which does support compile time constant strings but obviously lacks some of the niceties of std::string. Whichever is appropriate depends on what you're doing. It may be that std::string features are unneeded and static char[] is the way to go or it may be that std::string is needed and the runtime cost is neglibible (very likely).

The syntax you are trying will work with static const char[]:

static const char myString[] = "hello";

Any constructor for std::string shown in the other answers is executed at runtime.

Comments

0

you can also simply initialize the the new string ans by string ans= sourceString; in this way you can copy rearranged string in ans.:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.