首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >蓝桥ROS机器人之现代C++学习笔记第 9 章 其他杂项

蓝桥ROS机器人之现代C++学习笔记第 9 章 其他杂项

作者头像
zhangrelay
发布2022-04-29 19:55:41
发布2022-04-29 19:55:41
5280
举报

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 中的最大值的程序:

代码语言:javascript
复制
// 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 的修饰和操作

代码语言:javascript
复制
#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;
    }
}
代码语言:javascript
复制
// 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 字面量

代码语言:javascript
复制
#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 内存对齐

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-04-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 9.1 新类型
    • long long int
  • 9.2 noexcept 的修饰和操作
  • 9.3 字面量
  • 9.4 内存对齐
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档