C++ 如何用长度初始化 std::string?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27370480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:47:25  来源:igfitidea点击:

How to initialize an std::string with a length?

c++string

提问by penu

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

示例: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.

我这样做的目的是因为我有一个循环将字符从另一个字符串的某些区域复制到一个新字符串中。

回答by EToreo

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/

字符串是可变的,它的长度可以在运行时改变。但是如果你必须有一个指定的长度,你可以使用“填充构造函数”:http: //www.cplusplus.com/reference/string/string/string/

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

s6now equals "xxxxxxxxxx".

s6现在等于"xxxxxxxxxx"

回答by zachyee

You can initialize your string like this:

您可以像这样初始化字符串:

string word = "abc"

or

或者

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

回答by bialpio

You are probably looking for:

您可能正在寻找:

string word(3, ' ');

回答by Praxeolitic

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

std::string不支持编译时已知的长度。甚至有人提议将编译时字符串添加到 C++ 标准中。

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

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::stringfeatures are unneeded and static char[]is the way to go or it may be that std::stringis needed and the runtime cost is neglibible (very likely).

现在你倒霉了。您可以做的是使用static const char[]which 确实支持编译时常量字符串,但显然缺乏std::string. 哪个合适取决于你在做什么。可能std::string是不需要功能并且static char[]是要走的路,或者可能std::string是需要的并且运行时成本可以忽略不计(很可能)。

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

您正在尝试的语法将使用static const char[]

static const char myString[] = "hello";

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

std::string其他答案中显示的任何构造函数都在运行时执行。

回答by spartan

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/

有关调整字符串大小的更多信息:http: //www.cplusplus.com/reference/string/string/resize/