'What is String T(a,b) in C++ [closed]

While I learned C++ I challenged sample problems in which each 1 in stdin will move right in stdout. In sample answer,T(4,0) is defined. As I am beginner, I couldn't understand what is T(4,0). is this different from normal arrays? Thanks

sample answer

#include<bits/stdc++.h>
using namespace std;
int main(){
  string S;
  cin >> S;
  string T(4,0);
  T[0]='0';
  T[1]=S[0];
  T[2]=S[1];
  T[3]=S[2];
  cout << T;
}
c++


Solution 1:[1]

std::string has several constructors. In this case:

basic_string( size_type count, CharT ch, const Allocator& alloc = Allocator() );

(You don't provide an argument for alloc so the default is used. Don't worry about it.)

This constructor has the following effects:

  1. Constructs the string with count copies of character ch. [This constructor is not used for class template argument deduction if the Allocator type that would be deduced does not qualify as an allocator. (since C++17)]

So this creates a std::string with 4 characters in it, and each character is 0, aka '\0', aka null.

Solution 2:[2]

Since T is a std::string, it's the constructor of std::string https://en.cppreference.com/w/cpp/string/basic_string/basic_string

In this case, it's the constructor under 2):

basic_string( size_type count, CharT ch,
              const Allocator& alloc = Allocator() );

(alloc is a default argument and not needed to be supplied by you.)
It creates a string with count characters and every character is initialized to ch.

In your case, it's a string with 4 characters, all initialized to whatever ascii value 0 has. The \0 character.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nathan Pierson
Solution 2 Raildex