9.1 新类型
long long int
64位
在本文中,我们将讨论 C++ 中的 long long int 数据类型。 C++ 中的 long long int 数据类型用于存储 64 位整数。它是存储整数值的最大数据类型之一,不像 unsigned long long int 正数和负数。
long long int 数据类型的一些属性是:
作为有符号数据类型,它可以存储正值和负值。 采用 64 位大小,其中 1 位用于存储整数的符号。 可以存储在 long long int 数据类型中的最大整数值通常是 9、223、372、036、854、775、807,大约为 263 – 1(但取决于编译器)。 long long int 中可以存储的最大值作为常量存储在 <climits> 头文件中。其值可用作 LLONG_MAX。 可以存储在 long long int 数据类型中的最小整数值通常为 –9、223、372、036、854、775、808,大约为 –263(但取决于编译器)。 在数据类型上溢或下溢的情况下,该值将被环绕。例如,如果 –9、223、372、036、854、775、808 存储在 long long int 数据类型中并从中减去 1,则该变量中的值将等于 9、223、372、036 , 854, 775, 807. 同样,在溢出的情况下,该值将四舍五入为 –9, 223, 372, 036, 854, 775, 808。 下面是在 C++ 中获取可以存储在 long long int 中的最大值的程序:
// C++ program to illustrate the maximum
// value that can be stored in long long int
#include <climits>
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// From the constant of climits
// header file
long long int valueFromLimits = LLONG_MAX;
cout << "Value from climits "
<< "constant (maximum): ";
cout << valueFromLimits
<< "\n";
valueFromLimits = LLONG_MIN;
cout << "Value from climits "
<< "constant (minimum): ";
cout << valueFromLimits
<< "\n";
return 0;
}
9.2 noexcept 的修饰和操作
#include <iostream>
void may_throw() {
throw true;
}
auto non_block_throw = []{
may_throw();
};
void no_throw() noexcept {
return;
}
auto block_throw = []() noexcept {
no_throw();
};
int main()
{
std::cout << std::boolalpha
<< "may_throw() noexcept? " << noexcept(may_throw()) << std::endl
<< "no_throw() noexcept? " << noexcept(no_throw()) << std::endl
<< "lmay_throw() noexcept? " << noexcept(non_block_throw()) << std::endl
<< "lno_throw() noexcept? " << noexcept(block_throw()) << std::endl;
try {
may_throw();
} catch (...) {
std::cout << "exception captured from my_throw()" << std::endl;
}
try {
non_block_throw();
} catch (...) {
std::cout << "exception captured from non_block_throw()" << std::endl;
}
try {
block_throw();
} catch (...) {
std::cout << "exception captured from block_throw()" << std::endl;
}
}
?
?
// noexceptOperator.cpp
#include <iostream>
#include <array>
#include <vector>
class NoexceptCopy{
public:
std::array<int, 5> arr{1, 2, 3, 4, 5}; // (2)
};
class NonNoexceptCopy{
public:
std::vector<int> v{1, 2, 3, 4 , 5}; // (3)
};
template <typename T>
T copy(T const& src) noexcept(noexcept(T(src))){ // (1)
return src;
}
int main(){
NoexceptCopy noexceptCopy;
NonNoexceptCopy nonNoexceptCopy;
std::cout << std::boolalpha << std::endl;
std::cout << "noexcept(copy(noexceptCopy)): " << // (4)
noexcept(copy(noexceptCopy)) << std::endl;
std::cout << "noexcept(copy(nonNoexceptCopy)): " << // (5)
noexcept(copy(nonNoexceptCopy)) << std::endl;
std::cout << std::endl;
}
9.3 字面量
#include <iostream>
#include <string>
std::string operator"" _wow1(const char *wow1, size_t len) {
return std::string(wow1)+"woooooooooow, amazing";
}
std::string operator""_wow2 (unsigned long long i) {
return std::to_string(i)+"woooooooooow, amazing";
}
int main() {
std::string str = R"(C:\\File\\To\\Path)";
std::cout << str << std::endl;
int value = 0b1001010101010;
std::cout << value << std::endl;
auto str2 = "abc"_wow1;
auto num = 1_wow2;
std::cout << str2 << std::endl;
std::cout << num << std::endl;
return 0;
}
?
?
9.4 内存对齐
#include <iostream>
struct Storage {
char a;
int b;
double c;
long long d;
};
struct alignas(std::max_align_t) AlignasStorage {
char a;
int b;
double c;
long long d;
};
int main() {
std::cout << alignof(Storage) << std::endl;
std::cout << alignof(AlignasStorage) << std::endl;
return 0;
}
?
?
?
?
?
|