auto i = 42; // i被推导为int类型
auto d = 3.14; // d被推导为double类型
std::vector v = {1, 2, 3, 4, 5};
for (auto& i : v) {i *= 2;
}
auto f = [](int x, int y) -> int { return x + y; };
int result = f(3, 4); // result = 7
std::vector v1 = {1, 2, 3, 4, 5};
std::vector v2 = std::move(v1); // v2接管了v1的资源,v1变为无效状态
std::unique_ptr p(new int(42));
std::shared_ptr q = std::make_shared(42);
std::weak_ptr r = q;
void f(int* p) {}
f(nullptr); // 可以显式地传递空指针
class MyVector {
public:MyVector(MyVector&& other) noexcept {// 移动构造函数}MyVector& operator=(MyVector&& other) noexcept {// 移动赋值运算符return *this;}
};
std::vector v = {1, 2, 3, 4, 5};
std::map m = {{"one", 1}, {"two", 2}, {"three", 3}};
using IntVec = std::vector;
IntVec v = {1, 2, 3, 4, 5};
template
using Vec = std::vector;
Vec v = {1, 2, 3, 4, 5};
constexpr int fib(int n) {return (n <= 1) ? 1 : fib(n-1) + fib(n-2);
}
constexpr int x = fib(10); // 编译期计算出x的值为89
template
void print(Args... args) {std::cout << sizeof...(args) << std::endl; // 打印参数个数
}
print(1, 2, 3); // 打印3
print("hello", 3.14); // 打印2
auto sum = [](auto x, auto y) { return x + y; };
std::cout << sum(1, 2) << std::endl; // 输出3
std::cout << sum(1.5, 2.5) << std::endl; // 输出4.0
auto add(int x, int y) {return x + y; // 返回类型会自动推断为int
}
template
constexpr T pi = T(3.1415926535897932385);
std::cout << pi << std::endl; // 输出3.14159...
static_assert(sizeof(int) == 4, "int必须是4字节"); // 如果sizeof(int)不等于4,会输出提示信息
constexpr char operator""_c(char c) { return c; } // 将字符转化为字符
std::cout << 'a'_c << std::endl; // 输出字符'a'
int x = 1, y = 2;
auto f = [x, y = x + 1] { return x + y; };
std::cout << f() << std::endl; // 输出4
template
constexpr T pi = T(3.1415926535897932385);
std::cout << pi << std::endl; // 输出3.14159...
std::atomic x = 0; // 原子变量
#pragma omp parallel for
for (int i = 0; i < 1000; ++i) {x.fetch_add(1); // 线程安全的对x进行加一操作
}
std::cout << x << std::endl; // 输出1000
std::pair p = {1, 2};
auto [x, y] = p; // 结构化绑定
std::cout << x << " " << y << std::endl; // 输出1 2
if (int x = get_value(); x > 0) { // 在if语句中初始化变量xstd::cout << "x is positive" << std::endl;
}
std::pair p{1, 2}; // 编译器可以自动推断出std::pair
template
void foo(T t) {if constexpr (std::is_pointer_v) { // 如果T是指针类型std::cout << "t is a pointer" << std::endl;} else { // 如果T不是指针类型std::cout << "t is not a pointer" << std::endl;}
}
template
auto sum(Args... args) {return (args + ...); // 对args进行折叠求和
}
std::cout << sum(1, 2, 3, 4) << std::endl; // 输出10
inline int x = 1; // 定义一个内联变量x,初始值为1
namespace A {namespace B {void foo() {std::cout << "hello, world!" << std::endl;}}
}
A::B::foo(); // 调用函数foo
template
concept Integral = std::is_integral_v;
template
void foo(T t) requires Integral { // 使用概念描述模板参数要求std::cout << t << std::endl;
}
foo(1); // 调用foo函数
struct Point {int x, y;auto operator<=>(const Point& other) const {return std::tie(x, y) <=> std::tie(other.x, other.y);}
};
bool operator==(const Point& lhs, const Point& rhs) {return lhs.x == rhs.x && lhs.y == rhs.y;
}
std::set s{{1, 2}, {2, 1}, {1, 1}, {2, 2}};
for (const auto& p : s) {std::cout << p.x << ", " << p.y << std::endl;
}
输出结果为:
1, 1
1, 2
2, 1
2, 2
int x = 1;
auto lambda = [value = x * 2]() { // 在捕获列表中初始化变量valuestd::cout << value << std::endl;
};
lambda(); // 调用lambda表达式
consteval int get_value() { return 42; } // 定义一个在编译期计算的函数
std::array arr; // 在编译期创建一个大小为42的数组
// 定义一个模块
module my_module;
export void foo() {std::cout << "hello, world!" << std::endl;
}// 使用模块
import my_module;
int main() {foo(); // 调用函数fooreturn 0;
}
上一篇:生辰八字五行计算