0
点赞
收藏
分享

微信扫一扫

C++ 重要特性探究

何以至千里 2024-08-08 阅读 42

shared_from_this

#include<iostream>
#include<memory>

using namespace std;

class MyClass :public std::enable_shared_from_this<MyClass>
{
public:
	void show()
	{
		cout << "MyClass : show 运行" << endl;
	}
	
	//返回一个shared_ptr指向自身
	std::shared_ptr<MyClass> getSharedPtr()
	{
		return shared_from_this();
	}
	//获取shared_Ptr然后调用它
	void process()
	{
		std::shared_ptr<MyClass> ptr = shared_from_this();
		Funcaion(ptr);

	}
	void Funcaion(std::shared_ptr<MyClass>ptr)
	{
		cout << "函数内部传入的shared_ptr指针使用次数:" << ptr.use_count() << endl;
		ptr->show();
	}
};

int main()
{
	//创建一个指向Myclass的share_ptr指针
	std::shared_ptr<MyClass> obj = std::make_shared<MyClass>();

	//调用类内成员函数,获取shared_ptr自身
	std::shared_ptr<MyClass> selfptr = obj->getSharedPtr();

	//验证shared_ptr的使用次数
	cout << "shared_ptr use_count:" << selfptr.use_count() << endl;
	//cout << obj.use_count() << endl;

	//两个指针都可以调用类内成员函数验证
	obj->process();
	selfptr->process();
}

 左值和右值

 

 

 

 

#include <iostream>
#include <vector>

std::vector<int> createVector() {
    std::vector<int> temp = {1, 2, 3};
    return temp; // 返回的是一个右值
}

int main() {
    std::vector<int> vec = createVector(); // 这里使用移动语义
    for(int i : vec) {
        std::cout << i << " ";
    }
    return 0;
}

 

#include <iostream>

int main() {
    int a = 10; // a 是一个左值
    int b = a;  // 这里发生了左值到右值的转换,a 的值(10)被赋给 b

    std::cout << "a: " << a << std::endl; // a 是左值
    std::cout << "b: " << b << std::endl; // b 是左值,打印出来的值是 10

    int* p = &a; // 获取 a 的地址
    std::cout << "Address of a: " << p << std::endl;

    return 0;
}

 引用折叠

#include <iostream>
#include <utility> // for std::forward

// 接受左值引用
void process(int& x) {
    std::cout << "左值引用: " << x << std::endl;
}

// 接受右值引用
void process(int&& x) {
    std::cout << "右值引用: " << x << std::endl;
}

// 用于完美转发
template <typename T>
void forwarder(T&& arg) {
    process(std::forward<T>(arg));
}

int main() {
    int a = 10;
    forwarder(a);          // 调用 process(int&)  - 传递左值
    forwarder(20);         // 调用 process(int&&) - 传递右值
    forwarder(std::move(a)); // 调用 process(int&&) - 传递右值

    return 0;
}

 std::move

 

 C++指针

 

 

 

 

#include <iostream>
int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int multiply(int a, int b) {
    return a * b;
}

int main() {
    // 创建一个函数指针数组
    int (*operations[3])(int, int) = { add, subtract, multiply };

    int a = 10, b = 5;
    for (int i = 0; i < 3; ++i) {
        std::cout << "Result: " << operations[i](a, b) << std::endl;
    }
    return 0;
}
typedef int (*Operation)(int, int);

int add(int a, int b) {
    return a + b;
}

int multiply(int a, int b) {
    return a * b;
}

int main() {
    Operation op;
    op = add;
    std::cout << "Add: " << op(5, 3) << std::endl;

    op = multiply;
    std::cout << "Multiply: " << op(5, 3) << std::endl;

    return 0;
}

函数的三种传递方式(值传递、引用传递、指针传递)

void modifyValue(int x) {
    x = 20;  // 仅修改了局部变量 x 的值
}

int main() {
    int a = 10;
    modifyValue(a);
    std::cout << "a: " << a << std::endl;  // 输出 10,a 未被修改
    return 0;
}
void modifyValue(int& x) {
    x = 20;  // 修改了原始变量的值
}

int main() {
    int a = 10;
    modifyValue(a);
    std::cout << "a: " << a << std::endl;  // 输出 20,a 被修改
    return 0;
}
void modifyValue(int* x) {
    if (x != nullptr) {
        *x = 20;  // 通过指针修改原始变量的值
    }
}

int main() {
    int a = 10;
    modifyValue(&a);
    std::cout << "a: " << a << std::endl;  // 输出 20,a 被修改
    return 0;
}

 迭代器

野指针和悬空指针

 

 

四种强制类型转换

 

 

 

 

类型萃取

 

结构体相等判断方法分析

struct MyStruct {
    int a;
    float b;
    std::string c;
};

bool areEqual(const MyStruct& lhs, const MyStruct& rhs) {
    return lhs.a == rhs.a && lhs.b == rhs.b && lhs.c == rhs.c;
}
struct MyStruct {
    int a;
    float b;
    std::string c;

    bool operator==(const MyStruct& other) const {
        return a == other.a && b == other.b && c == other.c;
    }
};
#include <cstring>

struct MyStruct {
    int a;
    float b;
};

bool areEqual(const MyStruct& lhs, const MyStruct& rhs) {
    return std::memcmp(&lhs, &rhs, sizeof(MyStruct)) == 0;
}
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/equal_to.hpp>

struct MyStruct {
    int a;
    float b;
    std::string c;
};

BOOST_FUSION_ADAPT_STRUCT(MyStruct, a, b, c)

bool areEqual(const MyStruct& lhs, const MyStruct& rhs) {
    return boost::fusion::equal_to(lhs, rhs);
}

C++常用的四种模板简述

 

 

 

 switch语句的case分支是否可以直接定义变量分析

#include <iostream>

void example(int n) {
    switch (n) {
        case 1:
            int a = 10;  // 定义变量 a
            std::cout << "Case 1: " << a << std::endl;
            break;
        case 2:
            int b = 20;  // 这里也定义了变量 b
            std::cout << "Case 2: " << b << std::endl;
            break;
        default:
            std::cout << "Default case" << std::endl;
    }
}

int main() {
    example(1);
    example(2);
    return 0;
}

#include <iostream>

void example(int n) {
    switch (n) {
        case 1: {
            int a = 10;  // 在新的作用域中定义变量 a
            std::cout << "Case 1: " << a << std::endl;
            break;
        }
        case 2: {
            int b = 20;  // 在新的作用域中定义变量 b
            std::cout << "Case 2: " << b << std::endl;
            break;
        }
        default:
            std::cout << "Default case" << std::endl;
    }
}

int main() {
    example(1);
    example(2);
    return 0;
}

可变参数模版 

#include <iostream>

// 基础情况:不接受任何参数时终止递归
void print() {
    std::cout << std::endl;
}

// 可变参数模板函数
template <typename T, typename... Args>
void print(T first, Args... args) {
    std::cout << first << " ";
    print(args...); // 递归调用
}

int main() {
    print(1, 2, 3.5, "hello", 'A');
    return 0;

#include <iostream>

// 基础情况:没有参数时返回0
int sum() {
    return 0;
}

// 可变参数模板函数
template <typename T, typename... Args>
int sum(T first, Args... args) {
    return first + sum(args...); // 递归调用
}

int main() {
    std::cout << sum(1, 2, 3, 4, 5) << std::endl; // 输出: 15
    return 0;
}

 

#include <iostream>
#include <string>

// 基础情况:空的元组
template <typename... Values>
class Tuple;

// 可变参数模板类
template <typename Head, typename... Tail>
class Tuple<Head, Tail...> : private Tuple<Tail...> {
public:
    Tuple(Head head, Tail... tail) : Tuple<Tail...>(tail...), head_(head) {}

    Head head() const { return head_; }
    const Tuple<Tail...>& tail() const { return *this; }

private:
    Head head_;
};

// 终止递归的空元组特化
template <>
class Tuple<> {};

int main() {
    Tuple<int, double, std::string> t(42, 3.14, "hello");
    std::cout << t.head() << std::endl;         // 输出: 42
    std::cout << t.tail().head() << std::endl;  // 输出: 3.14
    std::cout << t.tail().tail().head() << std::endl;  // 输出: hello

    return 0;
}
举报

相关推荐

0 条评论