C++ 创建具有指定字符数的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12483406/
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-27 16:18:44  来源:igfitidea点击:

Create string with specified number of characters

c++cstringvariables

提问by sircrisp

Is there a way to create a string of say 256 characters at initialization with c++?

有没有办法在初始化时用 C++ 创建一个 256 个字符的字符串?

Part of my assignment requires me to "1. Create a string of 256 characters. Use repetitions of your first name."

我的部分作业要求我“1. 创建一个 256 个字符的字符串。重复使用你的名字。”

I'm not quite sure of how to do this other than using a loop but I feel like there is an easier way.

除了使用循环之外,我不太确定如何做到这一点,但我觉得有一种更简单的方法。

回答by Xeo

Taking a look at the constructor reference of basic_string, one can see that there is no easy way of repeating a complete string. For a single character, you could use (2)like this:

查看的构造函数引用basic_string,可以看到没有简单的方法来重复完整的字符串。对于单个字符,您可以这样使用(2)

std::string s(5, 'a'); // s == "aaaaa"

For generating a string repetition, you'll need some workaround. It's easier to do this post-construction by simply filling the string with, for example, std::generate(having fun with algorithms).

要生成字符串重复,您需要一些解决方法。通过简单地填充字符串,例如std::generate(玩弄算法),可以更轻松地完成此后构造。

#include <string>
#include <algorithm>

// ...

std::string pattern("Xeo ");
auto pattern_it = pattern.begin();
std::string s(256, '
#include <string>
using std::string;

string repeatToLength ( unsigned len, string s ) {
  string r, si = s;

  // add all the whole multiples of s.
  for ( unsigned q = len / s.size(); q > 0; q >>= 1 ) {
    if ( q & 1 ) r += si; // add si to r if the low bit of q is 1
    si +=  si; // double si
  }
  r += s.substr ( 0, len - r.size() ); // add any remainder
  return r;
}
'); std::generate(s.begin(), s.end(), [&]() -> char { if(pattern_it == pattern.end()) pattern_it = pattern.begin(); return *pattern_it++; // return current value and increment });

Live example.

活生生的例子。

回答by Daniel Patru

If you want a very long string, you can cut down the number of loop repetitions by doubling.

如果你想要一个很长的字符串,你可以通过加倍来减少循环重复的次数。

const string myName = "Blivit";
int numLeftOver = 256 % myName.length();

string Names;
for ( int Index = 0; Index < (256 / myName.length() ); ++Index ) {
   Names += myName;
}
Names += myName.substr(0, numLeftOver); 

回答by bspikol

std::string name = 
    "Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan "
    "Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan "
    "Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan "
    "Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan Zan ";

This is if you want to generate the string. I'm fairly sure there is a shorter way to do this...

这是如果您想生成字符串。我相当确定有一种更短的方法可以做到这一点......

回答by CyberGuy

http://www.cplusplus.com/reference/string/string/

http://www.cplusplus.com/reference/string/string/

As far as i understand you you want to reserve only 256 elements.

据我了解,您只想保留 256 个元素。

There is no constructor for it, and you cant set capacity() at the initialization. Probably you want to use .reserve after initialization. Be aware reserve():

它没有构造函数,并且您不能在初始化时设置容量()。可能你想在初始化后使用 .reserve 。请注意储备():

This can expand or shrink the size of the storage space in the string, although notice that the resulting capacity after a call to this function is not necessarily equal to res_arg but can be either equal or greater than res_arg, therefore shrinking requests may or may not produce an actual reduction of the allocated space in a particular library implementation.

这可以扩大或缩小字符串中存储空间的大小,但请注意,调用此函数后产生的容量不一定等于 res_arg,但可以等于或大于 res_arg,因此缩小请求可能会也可能不会在特定库实现中实际减少分配的空间。

Maybe you need a different type?

也许你需要不同的类型?

if you want just fill your string than use:

如果你只想填充你的字符串而不是使用:

http://en.cppreference.com/w/cpp/string/basic_string/basic_stringsecond one.

http://en.cppreference.com/w/cpp/string/basic_string/basic_string第二个。

回答by Zan Lynx

Not enough information to say really. From what you said, it sounds like its valid to just use your text editor to make a string 256 long with your name repeating.

没有足够的信息说真的。从你所说的来看,使用你的文本编辑器制作一个长度为 256 的字符串并重复你的名字听起来是有效的。

##代码##

Done.

完毕。