38

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.

penu
  • 968
  • 1
  • 9
  • 22
  • 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. – Praxeolitic Dec 09 '14 at 02:25
  • Yes, sorry I meant compile time – penu Dec 09 '14 at 02:25

6 Answers6

59

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".

Azeem
  • 11,148
  • 4
  • 27
  • 40
EToreo
  • 2,936
  • 4
  • 30
  • 36
7

You can initialize your string like this:

string word = "abc"

or

string word(length,' ');
word[0] = 'a';
word[1] = 'b';
word[2] = 'c';
zachyee
  • 348
  • 1
  • 8
  • This does not work if the string to copy is not known until compile time. – penu Dec 09 '14 at 02:26
  • @penu why would you do `a[0] = 'h'; a[1] = 'i';` instead of the more natural `a = "hi";` – greatwolf Dec 09 '14 at 02:30
  • @greatwolf I have a for loop to copy characters from certain areas of another string – penu Dec 09 '14 at 02:31
  • @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());`. – greatwolf Dec 09 '14 at 02:33
  • @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. – zachyee Dec 09 '14 at 02:39
5

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/

spartan
  • 141
  • 2
  • 1
4

You are probably looking for:

string word(3, ' ');
bialpio
  • 1,004
  • 7
  • 17
4

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.

Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
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.:)

mkbhru
  • 29
  • 4