C++ 字符串 - 使用初始化列表构造函数时的奇怪行为
C++ string - strange behaviour when using initialization list constructor
我知道我可以使用字符数组和初始化列表来填充字符串。
看起来编译器做了一些从 int 到 initializer_list 或分配器的隐式提升。但我不知道为什么它不给我任何警告,为什么它使它隐含。
你能给我解释一下字符串 s4 和 s5 会发生什么吗?
#include <iostream>
#include <string>
using namespace std;
class A{
};
int main() {
// string::string(charT const* s)
string s1("12345");
// 5 - because constructor takes into account null-terminated character
cout << s1.size() << endl;
// string(std::initializer_list<charT> ilist)
string s2({'1','2','3','4','5'});
// 5 - because string is built from the contents of the initializer list init.
cout << s2.size()<<endl;
// string::string(charT const* s, size_type count)
string s3("12345",3);
// 3 - Constructs the string with the first count characters of character string pointed to by s
cout << s3.size() << endl;
// basic_string( std::initializer_list<CharT> init,const Allocator& alloc = Allocator() ); - ?
string s4({'1','2','3','4','5'},3);
// 2 - why this compiles (with no warning) and what this result means?
cout << s4.size() << endl;
string s5({'1','2','3','4','5'},5);
// 0 - why this compiles (with no warning) and what this result means?
cout << s5.size() << endl;
// basic_string( std::initializer_list<CharT> init,const Allocator& alloc = Allocator() );
// doesn't compile, no known conversion for argument 2 from 'A' to 'const std::allocator<char>&'
//string s6({'1','2','3','4','5'},A());
//cout << s6.size() << endl;
return 0;
}
string s6({'1','2','3','4','5'},3);
string s7({'1','2','3','4','5'},5);
实际上,这些初始化不只是调用 std::initializer_list
构造函数。第二个参数不能隐式转换为std::allocator
,所以要考虑其他构造函数。调用的构造函数是具有此签名的构造函数:
basic_string( const basic_string& other,
size_type pos,
size_type count = std::basic_string::npos,
const Allocator& alloc = Allocator() );
std::initializer_list
构造函数用于从 braced-init-list 创建临时 std::string
以作为 other
参数传递给上述构造函数。临时文件可以绑定到它,因为它是对常量的引用。因此,第二个参数是 pos
参数,用作子字符串复制构造的起点。
所以s6
是区间[3, 5)
中的字符(即"45"
),s7
是区间[5,5)
中的字符(即""
).
我知道我可以使用字符数组和初始化列表来填充字符串。
看起来编译器做了一些从 int 到 initializer_list 或分配器的隐式提升。但我不知道为什么它不给我任何警告,为什么它使它隐含。
你能给我解释一下字符串 s4 和 s5 会发生什么吗?
#include <iostream>
#include <string>
using namespace std;
class A{
};
int main() {
// string::string(charT const* s)
string s1("12345");
// 5 - because constructor takes into account null-terminated character
cout << s1.size() << endl;
// string(std::initializer_list<charT> ilist)
string s2({'1','2','3','4','5'});
// 5 - because string is built from the contents of the initializer list init.
cout << s2.size()<<endl;
// string::string(charT const* s, size_type count)
string s3("12345",3);
// 3 - Constructs the string with the first count characters of character string pointed to by s
cout << s3.size() << endl;
// basic_string( std::initializer_list<CharT> init,const Allocator& alloc = Allocator() ); - ?
string s4({'1','2','3','4','5'},3);
// 2 - why this compiles (with no warning) and what this result means?
cout << s4.size() << endl;
string s5({'1','2','3','4','5'},5);
// 0 - why this compiles (with no warning) and what this result means?
cout << s5.size() << endl;
// basic_string( std::initializer_list<CharT> init,const Allocator& alloc = Allocator() );
// doesn't compile, no known conversion for argument 2 from 'A' to 'const std::allocator<char>&'
//string s6({'1','2','3','4','5'},A());
//cout << s6.size() << endl;
return 0;
}
string s6({'1','2','3','4','5'},3);
string s7({'1','2','3','4','5'},5);
实际上,这些初始化不只是调用 std::initializer_list
构造函数。第二个参数不能隐式转换为std::allocator
,所以要考虑其他构造函数。调用的构造函数是具有此签名的构造函数:
basic_string( const basic_string& other,
size_type pos,
size_type count = std::basic_string::npos,
const Allocator& alloc = Allocator() );
std::initializer_list
构造函数用于从 braced-init-list 创建临时 std::string
以作为 other
参数传递给上述构造函数。临时文件可以绑定到它,因为它是对常量的引用。因此,第二个参数是 pos
参数,用作子字符串复制构造的起点。
所以s6
是区间[3, 5)
中的字符(即"45"
),s7
是区间[5,5)
中的字符(即""
).