本文整理汇总了C++中std::u16string::resize方法的典型用法代码示例。如果您正苦于以下问题:C++ u16string::resize方法的具体用法?C++ u16string::resize怎么用?C++ u16string::resize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std::u16string
的用法示例。
在下文中一共展示了u16string::resize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GetNtFunctions
void IPv4AddressToString(IPv4Address address, std::u16string& buffer)
{
const char* const InvalidIPv4AddressStr = "Invalid IPv4 address!";
const auto& addressBytes = address.Bytes;
const in_addr addr = { static_cast<uchar>(addressBytes[0]), static_cast<uchar>(addressBytes[1]),
static_cast<uchar>(addressBytes[2]), static_cast<uchar>(addressBytes[3]) };
const auto& ntFunctions = GetNtFunctions();
//If the length of the buffer pointed to by the AddressString parameter is not large enough
//to receive the string representation of the IPv4 address and port, RtlIpv4AddressToStringEx
//returns ERROR_INVALID_PARAMETER and sets the AddressStringLength parameter to the buffer length required.
ulong bufferLength = {};
auto errCode = ntFunctions.RtlIpv4AddressToStringEx(&addr, 0, nullptr, &bufferLength);
//如果 bufferLength 在调用后依旧为 0 说明 address 有问题。
Try<std::invalid_argument>(bufferLength == 0, InvalidIPv4AddressStr);
buffer.resize(static_cast<std::size_t>(bufferLength));
errCode = ntFunctions.RtlIpv4AddressToStringEx(&addr, 0, reinterpret_cast<wchar_t*>(&buffer[0]), &bufferLength);
//如果有错误,前面必然已经抛出异常,故这里必然是 Success。
assert(errCode == NtFunctions::NtSuccess);
assert(bufferLength > 0);
//在写入的 buffer 中包含 '\0',会导致判等出现奇怪的结果,故舍弃掉结尾的空字符。
buffer.pop_back();
}
示例2:
void LineEdit::IntValidator::assign(std::u16string &string,
const std::u16string &arg) const {
if(arg.size()==0){
if(string.size()==0){
string.resize(1);
string[0] = '0';
}
return;
}
bool good=true;
if(arg[0]=='-'){
if(arg.size()!=1){
if(arg[1]=='0' && arg.size()!=2)
good=false;
for(size_t i=1; good && i<arg.size(); ++i ){
const char16_t c = arg[i];
good &= ('0'<=c && c<='9');
}
} else {
good = false;
}
} else {
if(arg[0]=='0' && arg.size()!=1)
good=false;
for(size_t i=0; good && i<arg.size(); ++i ){
const char16_t c = arg[i];
good &= ('0'<=c && c<='9');
}
}
if(good){
string = arg;
} else {
if(string.size()==0){
string.resize(1);
string[0] = '0';
}
}
}