跳到主要内容

std::string 构造函数

std::basic_string 类可以通过多种不同的方式构造。使用右上角的按钮,方便地使用箭头导航。

注意

std::basic_string 是一个类模板,具有以下类型参数,用于构造函数中

公共CharT字符类型
公共特性指定字符类型操作的特性类
公共分配器用于分配内部存储的分配器类型

默认构造函数

// 1)
basic_string()
: basic_string( Allocator() )
{
}

// 2)
explicit basic_string( Allocator const& alloc );
  • 如果 Allocator 的构造函数也是 noexcept,则构造函数 1) 是 noexcept
  • 构造函数 2) 是 noexcept,因为 Allocator 之前已构造
  • 两个构造函数都是 constexpr

默认构造函数。构造空字符串(零大小和未指定容量)。如果未提供分配器,则从默认构造的实例获取分配器。

示例

#include <iostream>
#include <string>
#include <cassert>

int main() {
// Default constructed string:
std::string s;

// This assertion has to pass:
assert(s.empty() && (s.length() == 0) && (s.size() == 0));

// Capacity is unspecified:
std::cout << "s.capacity(): " << s.capacity() << '\n';
}
结果
s.capacity(): 15

重复字符构造函数

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

使用 count 个字符 ch 的副本构造字符串。

推导指南 (C++17 起)

如果推导出的 Allocator 类型不符合分配器条件,则此构造函数不用于类模板参数推导。

示例

#include <iostream>
#include <string>
#include <iomanip> // for std::quoted

int main() {
std::string s(4, '=');

std::cout << std::quoted(s) << '\n';
}
结果(控制台)
"===="

复制子串构造函数(无界)

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

使用 other 的子串 [pos, other.size()) 构造字符串。
换句话说,从 pos 索引处的元素开始复制 other 字符串。

示例

#include <iostream>
#include <string>

int main() {

// |01234567....|
// |-------+++++|
// "Hello, World"
std::string hw = "Hello, World";
std::string s(hw, 7);

std::cout << s << '\n';
}
结果(控制台)
World

复制子串构造函数(有界)

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

使用 other 的子串 [pos, pos+count) 构造字符串。如果 count == npos,或者请求的子串超出字符串末尾,则结果子串为 [pos, other.size())

示例

#include <iostream>
#include <string>

int main() {

// |012345......|
// |--++++------|
// "Hello, World"
std::string hw = "Hello, World";
std::string s(hw, 2, 4);

std::cout << s << '\n';
}
结果(控制台)
llo,

复制字符范围构造函数(无界)

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

使用指向 s空终止字符字符串的副本初始化字符串内容。字符串的长度由第一个空字符确定。如果 [s, s + Traits::length(s)) 不是有效范围(例如,如果 s 是空指针),则行为未定义。

推导指南 C++17 起

如果推导出的 Allocator 类型不符合分配器条件,则此构造函数不用于类模板参数推导。(C++17 起)

示例

#include <iostream>
#include <string>

int main() {
std::string s("Hello, World!");

std::cout << s << '\n';
}
结果(控制台)
Hello, World!

复制字符范围构造函数(有界)

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

使用指向 s 的字符字符串的前 count 个字符构造字符串。s 可以包含空字符。字符串的长度为 count。如果 [s, s + count) 不是有效范围,则行为未定义。

示例

#include <iostream>
#include <string>

int main() {

// |01234.......|
// |+++++-------|
// "Hello, World"
std::string s("Hello, World", 5);

std::cout << s << '\n';
}
结果(控制台)
Hello

从迭代器范围复制构造函数

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

使用范围 [first, last) 的内容构造字符串。

详情
仅当 InputIt 是整型时,等效于重载 (2),如同
basic_string(static_cast<size_type>(first), static_cast<value_type>(last), alloc)

示例

#include <iostream>
#include <string>
#include <array>

int main() {
std::array<char, 6> chars{ "Hello" };

std::string s(chars.begin(), chars.end());

std::cout << s << '\n';
}
结果(控制台)
Hello

复制构造函数

// 1)
constexpr basic_string( std::basic_string const& other );

// 2)
constexpr basic_string(
std::basic_string const& other,
Allocator const& alloc
);

使用 other 内容的副本构造字符串。

您可以选择指定自定义分配器 (2)。

示例

#include <iostream>
#include <string>

int main() {
std::string hw = "Hello, World";
// Uses copy constructor, not assignment operator
std::string s = hw;

std::cout << s << '\n';
}
结果(控制台)
Hello World

移动构造函数

// 1)
constexpr basic_string( std::basic_string && other ) noexcept;

// 2)
constexpr basic_string(
std::basic_string && other,
Allocator const& alloc
);

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

您可以选择指定自定义分配器 (2)。

示例

#include <iostream>
#include <string>

int main() {
std::string hw = "Hello, World";
// Uses move constructor, not assignment operator
std::string s = std::move(hw);
// hw has unspecified state right now

std::cout << s << '\n';
}
结果(控制台)
Hello, World

初始化列表构造函数

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

使用初始化列表 ilist 的内容构造字符串。

示例

#include <iostream>
#include <string>

int main() {
std::string s = { "Hello" };
// Same as:
// std::string s = { 'H', 'e', 'l', 'l', 'o', '\0' };

std::cout << s << '\n';
std::cout << s.size() << '\n';
}
结果(控制台)
Hello
5

string_view 类似类型构造函数复制(无界)

template <typename T>
explicit constexpr basic_string(
T const& t,
Allocator const& alloc = Allocator()
);

string_view 类似类型 T 的对象 t 复制内容构造字符串。

它是如何工作的

隐式地将 t 转换为字符串视图 sv,如同通过

std::basic_string_view<CharT, Traits> sv = t;

然后用 sv 的内容初始化字符串,如同通过

basic_string(sv.data(), sv.size(), alloc)

重载决议

此重载仅在以下情况下参与重载解析

std::is_convertible_v< const T&, std::basic_string_view<CharT, Traits> >

true

std::is_convertible_v<const T&, const CharT*>

false

string_view 类似类型构造函数复制(有界)

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

string_view 类似类型 T 的对象 t 复制内容的子范围 [pos, pos + n) 构造字符串。

它是如何工作的

隐式地将 t 转换为字符串视图 sv,如同通过

std::basic_string_view<CharT, Traits> sv = t;

然后用 sv 的子范围 [pos, pos + n) 初始化字符串,如同通过

basic_string(sv.data(), sv.size(), alloc)

重载决议

此重载仅在以下情况下参与重载解析

std::is_convertible_v< const T&, std::basic_string_view<CharT, Traits> >

true

已删除的 std::nullptr_t 构造函数

constexpr basic_string( std::nullptr_t ) = delete;

basic_string 无法从 nullptr 构造。

本文源自此 CppReference 页面。它可能已为改进或编辑偏好而进行更改。单击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。

std::string 构造函数

std::basic_string 类可以通过多种不同的方式构造。使用右上角的按钮,方便地使用箭头导航。

注意

std::basic_string 是一个类模板,具有以下类型参数,用于构造函数中

公共CharT字符类型
公共特性指定字符类型操作的特性类
公共分配器用于分配内部存储的分配器类型

默认构造函数

// 1)
basic_string()
: basic_string( Allocator() )
{
}

// 2)
explicit basic_string( Allocator const& alloc );
  • 如果 Allocator 的构造函数也是 noexcept,则构造函数 1) 是 noexcept
  • 构造函数 2) 是 noexcept,因为 Allocator 之前已构造
  • 两个构造函数都是 constexpr

默认构造函数。构造空字符串(零大小和未指定容量)。如果未提供分配器,则从默认构造的实例获取分配器。

示例

#include <iostream>
#include <string>
#include <cassert>

int main() {
// Default constructed string:
std::string s;

// This assertion has to pass:
assert(s.empty() && (s.length() == 0) && (s.size() == 0));

// Capacity is unspecified:
std::cout << "s.capacity(): " << s.capacity() << '\n';
}
结果
s.capacity(): 15

重复字符构造函数

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

使用 count 个字符 ch 的副本构造字符串。

推导指南 (C++17 起)

如果推导出的 Allocator 类型不符合分配器条件,则此构造函数不用于类模板参数推导。

示例

#include <iostream>
#include <string>
#include <iomanip> // for std::quoted

int main() {
std::string s(4, '=');

std::cout << std::quoted(s) << '\n';
}
结果(控制台)
"===="

复制子串构造函数(无界)

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

使用 other 的子串 [pos, other.size()) 构造字符串。
换句话说,从 pos 索引处的元素开始复制 other 字符串。

示例

#include <iostream>
#include <string>

int main() {

// |01234567....|
// |-------+++++|
// "Hello, World"
std::string hw = "Hello, World";
std::string s(hw, 7);

std::cout << s << '\n';
}
结果(控制台)
World

复制子串构造函数(有界)

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

使用 other 的子串 [pos, pos+count) 构造字符串。如果 count == npos,或者请求的子串超出字符串末尾,则结果子串为 [pos, other.size())

示例

#include <iostream>
#include <string>

int main() {

// |012345......|
// |--++++------|
// "Hello, World"
std::string hw = "Hello, World";
std::string s(hw, 2, 4);

std::cout << s << '\n';
}
结果(控制台)
llo,

复制字符范围构造函数(无界)

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

使用指向 s空终止字符字符串的副本初始化字符串内容。字符串的长度由第一个空字符确定。如果 [s, s + Traits::length(s)) 不是有效范围(例如,如果 s 是空指针),则行为未定义。

推导指南 C++17 起

如果推导出的 Allocator 类型不符合分配器条件,则此构造函数不用于类模板参数推导。(C++17 起)

示例

#include <iostream>
#include <string>

int main() {
std::string s("Hello, World!");

std::cout << s << '\n';
}
结果(控制台)
Hello, World!

复制字符范围构造函数(有界)

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

使用指向 s 的字符字符串的前 count 个字符构造字符串。s 可以包含空字符。字符串的长度为 count。如果 [s, s + count) 不是有效范围,则行为未定义。

示例

#include <iostream>
#include <string>

int main() {

// |01234.......|
// |+++++-------|
// "Hello, World"
std::string s("Hello, World", 5);

std::cout << s << '\n';
}
结果(控制台)
Hello

从迭代器范围复制构造函数

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

使用范围 [first, last) 的内容构造字符串。

详情
仅当 InputIt 是整型时,等效于重载 (2),如同
basic_string(static_cast<size_type>(first), static_cast<value_type>(last), alloc)

示例

#include <iostream>
#include <string>
#include <array>

int main() {
std::array<char, 6> chars{ "Hello" };

std::string s(chars.begin(), chars.end());

std::cout << s << '\n';
}
结果(控制台)
Hello

复制构造函数

// 1)
constexpr basic_string( std::basic_string const& other );

// 2)
constexpr basic_string(
std::basic_string const& other,
Allocator const& alloc
);

使用 other 内容的副本构造字符串。

您可以选择指定自定义分配器 (2)。

示例

#include <iostream>
#include <string>

int main() {
std::string hw = "Hello, World";
// Uses copy constructor, not assignment operator
std::string s = hw;

std::cout << s << '\n';
}
结果(控制台)
Hello World

移动构造函数

// 1)
constexpr basic_string( std::basic_string && other ) noexcept;

// 2)
constexpr basic_string(
std::basic_string && other,
Allocator const& alloc
);

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

您可以选择指定自定义分配器 (2)。

示例

#include <iostream>
#include <string>

int main() {
std::string hw = "Hello, World";
// Uses move constructor, not assignment operator
std::string s = std::move(hw);
// hw has unspecified state right now

std::cout << s << '\n';
}
结果(控制台)
Hello, World

初始化列表构造函数

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

使用初始化列表 ilist 的内容构造字符串。

示例

#include <iostream>
#include <string>

int main() {
std::string s = { "Hello" };
// Same as:
// std::string s = { 'H', 'e', 'l', 'l', 'o', '\0' };

std::cout << s << '\n';
std::cout << s.size() << '\n';
}
结果(控制台)
Hello
5

string_view 类似类型构造函数复制(无界)

template <typename T>
explicit constexpr basic_string(
T const& t,
Allocator const& alloc = Allocator()
);

string_view 类似类型 T 的对象 t 复制内容构造字符串。

它是如何工作的

隐式地将 t 转换为字符串视图 sv,如同通过

std::basic_string_view<CharT, Traits> sv = t;

然后用 sv 的内容初始化字符串,如同通过

basic_string(sv.data(), sv.size(), alloc)

重载决议

此重载仅在以下情况下参与重载解析

std::is_convertible_v< const T&, std::basic_string_view<CharT, Traits> >

true

std::is_convertible_v<const T&, const CharT*>

false

string_view 类似类型构造函数复制(有界)

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

string_view 类似类型 T 的对象 t 复制内容的子范围 [pos, pos + n) 构造字符串。

它是如何工作的

隐式地将 t 转换为字符串视图 sv,如同通过

std::basic_string_view<CharT, Traits> sv = t;

然后用 sv 的子范围 [pos, pos + n) 初始化字符串,如同通过

basic_string(sv.data(), sv.size(), alloc)

重载决议

此重载仅在以下情况下参与重载解析

std::is_convertible_v< const T&, std::basic_string_view<CharT, Traits> >

true

已删除的 std::nullptr_t 构造函数

constexpr basic_string( std::nullptr_t ) = delete;

basic_string 无法从 nullptr 构造。

本文源自此 CppReference 页面。它可能已为改进或编辑偏好而进行更改。单击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。