std::string 构造函数
std::basic_string
类可以通过多种不同的方式构造。使用右上角的按钮,方便地使用箭头导航。
std::basic_string
是一个类模板,具有以下类型参数,用于构造函数中
公共 | CharT | 字符类型 |
公共 | 特性 | 指定字符类型操作的特性类 |
公共 | 分配器 | 用于分配内部存储的分配器类型 |
默认构造函数
- C++20
- C++17
- C++17 之前
- 简化
- 详细
// 1)
basic_string()
: basic_string( Allocator() )
{
}
// 2)
explicit basic_string( Allocator const& alloc );
- 如果
Allocator
的构造函数也是noexcept
,则构造函数 1) 是noexcept
- 构造函数 2) 是
noexcept
,因为Allocator
之前已构造 - 两个构造函数都是
constexpr
// 1)
constexpr basic_string() noexcept(noexcept( Allocator() ))
: basic_string( Allocator() )
{
}
// 2)
explicit constexpr basic_string( Allocator const& alloc ) noexcept;
- 简化
- 详细
// 1)
basic_string()
: basic_string( Allocator() )
{
}
// 2)
explicit basic_string( Allocator const& alloc );
- 如果
Allocator
的构造函数也是noexcept
,则构造函数 1) 是noexcept
- 构造函数 2) 是
noexcept
,因为Allocator
之前已构造
// 1)
basic_string() noexcept(noexcept( Allocator() ))
: basic_string( Allocator() )
{
}
// 2)
explicit basic_string( Allocator const& alloc ) noexcept;
basic_string();
explicit basic_string( Allocator const& alloc );
默认构造函数。构造空字符串(零大小和未指定容量)。如果未提供分配器,则从默认构造的实例获取分配器。
示例
#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
重复字符构造函数
- C++20
- 直到 C++20
constexpr basic_string(
size_type count,
CharT ch,
Allocator const& alloc = Allocator()
);
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';
}
"===="
复制子串构造函数(无界)
- C++20
- 直到 C++20
constexpr basic_string(
basic_string const& other,
size_type pos,
Allocator const& alloc = Allocator()
);
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
复制子串构造函数(有界)
- C++20
- 直到 C++20
constexpr basic_string(
basic_string const& other,
size_type pos,
size_type count,
Allocator const& alloc = Allocator()
);
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,
复制字符范围构造函数(无界)
- C++20
- 直到 C++20
constexpr basic_string(
CharT const* s,
Allocator const& alloc = Allocator()
);
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!
复制字符范围构造函数(有界)
- C++20
- 直到 C++20
constexpr basic_string(
CharT const* s,
size_type count,
Allocator const& alloc = Allocator()
);
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
从迭代器范围复制构造函数
- C++20
- 直到 C++20
template <typename InputIt>
constexpr basic_string(
InputIt first,
InputIt last,
Allocator const& alloc = Allocator()
);
template <typename InputIt>
basic_string(
InputIt first,
InputIt last,
Allocator const& alloc = Allocator()
);
使用范围 [first, last)
的内容构造字符串。
详情
- C++11
- 直到 C++11
InputIt
是整型时,等效于重载 (2),如同basic_string(static_cast<size_type>(first), static_cast<value_type>(last), alloc)
InputIt
满足 LegacyInputIterator 时参与重载解析。示例
#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
复制构造函数
- C++20
- 自 C++11 起
// 1)
constexpr basic_string( std::basic_string const& other );
// 2)
constexpr basic_string(
std::basic_string const& other,
Allocator const& alloc
);
// 1)
basic_string( std::basic_string const& other );
// 2)
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
移动构造函数
- C++20
- 自 C++11 起
// 1)
constexpr basic_string( std::basic_string && other ) noexcept;
// 2)
constexpr basic_string(
std::basic_string && other,
Allocator const& alloc
);
// 1)
basic_string( std::basic_string && other ) noexcept;
// 2)
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
初始化列表构造函数
- C++20
- 自 C++11 起
constexpr basic_string(
std::initializer_list<CharT> ilist,
Allocator const& alloc = Allocator()
);
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
类似类型构造函数复制(无界)
- C++20
- 自 C++17 起
template <typename T>
explicit constexpr basic_string(
T const& t,
Allocator const& alloc = Allocator()
);
template <typename T>
explicit 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
类似类型构造函数复制(有界)
- C++20
- 自 C++17 起
template <typename T>
explicit constexpr basic_string(
T const& t,
size_type pos,
size_type n,
Allocator const& alloc = Allocator()
);
template <typename T>
explicit 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
构造函数
- 自 C++23 起
constexpr basic_string( std::nullptr_t ) = delete;
basic_string
无法从 nullptr
构造。