首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::basic_string::basic_string

(1)

explicit basic_string( const Allocator& alloc = Allocator() );

(until C++14)

basic_string() : basic_string( Allocator() ) {} explicit basic_string( const Allocator& alloc );

(since C++14) (until C++17)

basic_string() noexcept(noexcept( Allocator() )): basic_string( Allocator() ) {} explicit basic_string( const Allocator& alloc ) noexcept;

(since C++17)

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

(2)

(3)

basic_string( const basic_string& other, size_type pos, size_type count = std::basic_string::npos, const Allocator& alloc = Allocator() );

(until C++17)

basic_string( const basic_string& other, size_type pos, const Allocator& a = Allocator());

(since C++17)

basic_string( const basic_string& other, size_type pos, size_type count, const Allocator& alloc = Allocator() );

(since C++17)

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

(4)

basic_string( const CharT* s, const Allocator& alloc = Allocator() );

(5)

template< class InputIt > basic_string( InputIt first, InputIt last, const Allocator& alloc = Allocator() );

(6)

basic_string( const basic_string& other );

(7)

basic_string( const basic_string& other, const Allocator& alloc );

(7)

(since C++11)

basic_string( basic_string&& other ) noexcept;

(8)

(since C++11)

basic_string( basic_string&& other, const Allocator& alloc );

(8)

(since C++11)

basic_string( std::initializer_list<CharT> init, const Allocator& alloc = Allocator() );

(9)

(since C++11)

explicit basic_string( std::basic_string_view<CharT, Traits> sv, const Allocator& alloc = Allocator() );

(10)

(since C++17)

template < class T > basic_string( const T& t, size_type pos, size_type n, const Allocator& alloc = Allocator() );

(11)

(since C++17)

从各种数据源构造新字符串,并可选择使用用户提供的分配器。alloc...

1%29默认构造函数。构造空字符串%28零大小和未指定的容量%29。

2%29用count字符副本ch如果count >= npos...

3%29用子字符串构造字符串。[pos, pos+count)other.如果count == npos,如果count未指定,或者如果请求的子字符串持续时间超过字符串的结尾,则生成的子字符串为[pos, size())...

4%29用第一个字符串构造字符串。count所指向的字符串字符s...s可以包含空字符。字符串的长度为count如果s不指向至少count元素CharT,包括当时的情况s为空指针。

5%29构造内容初始化为空结束字符串副本的字符串。s字符串的长度由第一个空字符决定。如果s不指向至少Traits::length(s)+1元素CharT,包括当时的情况s为空指针。

6%29使用范围的内容构造字符串。[first, last).如果InputIt是一个积分类型,等价于basic_string(static_cast<size_type>(first), static_cast<value_type>(last), a)...

7%29复制构造函数。的内容的副本构造字符串。other...

8%29移动构造函数。的内容构造字符串。other使用移动语义。other处于有效但未指定的状态。

9%29使用初始化程序列表的内容构造字符串。init...

10%29使用String视图的内容构造字符串。sv,好像basic_string(sv.data(), sv.size(), alloc)

11%29隐式转换t到字符串视图sv好像std::basic_string_view<CharT, Traits> sv = t;,然后使用子范围初始化字符串。[pos, pos + n)sv好像basic_string(sv.substr(pos, n), a)。此重载只参与以下情况下的过载解决方案:std::is_convertible_v<const T&,std::basic_string_view<CharT, Traits>>true...

参数

alloc

-

allocator to use for all memory allocations of this string

count

-

size of the resulting string

ch

-

value to initialize the string with

pos

-

position of the first character to include

first, last

-

range to copy the characters from

s

-

pointer to an array of characters to use as source to initialize the string with

other

-

another string to use as source to initialize the string with

init

-

std::initializer_list to initialize the string with

sv

-

std::basic_string_view to initialize the string with

t

-

object (convertible to std::basic_string_view) to initialize the string with

复杂性

1%29常数

2-4%29线性count

5%29线性s

6%29直线之间的距离firstlast

7%29线性other

8%29常数。如果alloc被赋予和alloc != other.get_allocator(),然后线性

9%29线性init

例外

3%29std::out_of_range如果pos > other.size()

8%29如果alloc == str.get_allocator()

注记

初始化字符串文字包含嵌入式'\0'字符使用重载%285%29,它在第一个空字符处停止。可以通过指定不同的构造函数或使用operator""s*

二次

代码语言:javascript
复制
std::string s1 = "ab\0\0cd";   // s1 contains "ab"
std::string s2{"ab\0\0cd", 6}; // s2 contains "ab\0\0cd"
std::string s3 = "ab\0\0cd"s;  // s3 contains "ab\0\0cd"

二次

二次

代码语言:javascript
复制
#include <iostream>
#include <cassert>
#include <iterator>
#include <string>
#include <cctype>
 
int main()
{
  {
    // string::string()
    std::string s;
    assert(s.empty() && (s.length() == 0) && (s.size() == 0));
  }
 
  {
    // string::string(size_type count, charT ch)
    std::string s(4, '=');
    std::cout << s << '\n'; // "===="
  }
 
  {
    std::string const other("Exemplary");
    // string::string(string const& other, size_type pos, size_type count)
    std::string s(other, 0, other.length()-1);
    std::cout << s << '\n'; // "Exemplar"
  }
 
  {
    // string::string(charT const* s, size_type count)
    std::string s("C-style string", 7);
    std::cout << s << '\n'; // "C-style"
  }
 
  {
    // string::string(charT const* s)
    std::string s("C-style\0string");
    std::cout << s << '\n'; // "C-style"
  }
 
  {
    char mutable_c_str[] = "another C-style string";
    // string::string(InputIt first, InputIt last)
    std::string s(std::begin(mutable_c_str)+8, std::end(mutable_c_str)-1);
    std::cout << s << '\n'; // "C-style string"
  }
 
  {
    std::string const other("Exemplar");
    std::string s(other);
    std::cout << s << '\n'; // "Exemplar"
  }
 
  {
    // string::string(string&& str)
    std::string s(std::string("C++ by ") + std::string("example"));
    std::cout << s << '\n'; // "C++ by example"
  }
 
  {
    // string(std::initializer_list<charT> ilist)
    std::string s({ 'C', '-', 's', 't', 'y', 'l', 'e' });
    std::cout << s << '\n'; // "C-style"
  }
 
  {
    // overload resolution selects string(InputIt first, InputIt last) [with InputIt = int]
    // which behaves as if string(size_type count, charT ch) is called
    std::string s(3, std::toupper('a'));
    std::cout << s << '\n'; // "AAA"
  }
}

二次

产出:

二次

代码语言:javascript
复制
====
Exemplar
C-style
C-style
C-style string
Exemplar
C++ by example
C-style
AAA

二次

另见

assign

assign characters to a string (public member function)

operator=

assigns values to the string (public member function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券