Create string with specified number of characters

71,406

Solution 1

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:

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

#include <string>
#include <algorithm>

// ...

std::string pattern("Xeo ");
auto pattern_it = pattern.begin();
std::string s(256, '\0');
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.

Solution 2

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

#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;
}

Solution 3

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); 

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

Solution 4

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

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

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():

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.

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_string second one.

Share:
71,406

Related videos on Youtube

sircrisp
Author by

sircrisp

Dumb csci student that doesn't know anything at all.

Updated on July 09, 2022

Comments

  • sircrisp
    sircrisp almost 2 years

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

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

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

    • jrok
      jrok over 11 years
      If you mean std::string, then no, there is no constructor that would do that. There is a constructor that makes a string of n repetitions of some character, but that's it. You'll have to use a loop.
    • sircrisp
      sircrisp over 11 years
      What is the constructor?
    • sircrisp
      sircrisp over 11 years
      I know what a constructor is I am just wanting to know the one you speak of.
    • jrok
      jrok over 11 years
      Heh, sorry :) Number 2 in this reference page
  • jrok
    jrok over 11 years
    do { names += myName; } while (names.size() < 256); names.resize(256);
  • Mooing Duck
    Mooing Duck over 11 years
    Clever use of generate, I was thinking of making a seperate fill_pattern function.
  • bspikol
    bspikol over 11 years
    @jrok: Very nice. I like the way you use resize() to chop off the string at 256 characters. Slick.
  • Hindol
    Hindol over 11 years
    Very nice example. But too hard for someone who is learning programming.