// 以模块的形式导入头文件
import <iostream>;
// 使用模块中的函数
int main() {
std::cout << 'Hello, C++20 Modules!' << std::endl;
return 0;
}
template <typename T>
concept Integral = std::is_integral<T>::value;
template <Integral T>
T add(T a, T b) {
return a + b;
}
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
// 使用结构化绑定
for (auto& [index, value] : numbers) {
std::cout << 'Index: ' << index << ', Value: ' << value << std::endl;
}
return 0;
}
#include <iostream>
#include <coroutine>
struct SimpleCoroutine {
struct promise_type {
SimpleCoroutine get_return_object() {
return {};
}
std::suspend_never initial_suspend() {
return {};
}
std::suspend_never final_suspend() noexcept {
return {};
}
void return_void() {}
};
// 协程的执行体
void await_suspend(std::coroutine_handle<> handle) {
std::cout << 'Coroutine is running...' << std::endl;
}
};
int main() {
SimpleCoroutine myCoroutine;
co_await myCoroutine;
return 0;
}
#include <iostream>
#include <vector>
#include <ranges>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
// 使用区间进行过滤和变换
auto result = numbers | std::views::filter([](int n) { return n % 2 == 0; })
| std::views::transform([](int n) { return n * 2; });
for (int n : result) {
std::cout << n << ' ';
}
return 0;
}
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
std::mutex myMutex;
void printNumbers(int id) {
std::lock_guard<std::mutex> lock(myMutex);
for (int i = 0; i < 5; ++i) {
std::cout << 'Thread ' << id << ': ' << i << std::endl;
}
}
int main() {
std::vector<std::thread> threads;
for (int i = 0; i < 3; ++i) {
threads.emplace_back(printNumbers, i);
}
for (auto& thread : threads) {
thread.join();
}
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {5, 2, 8, 1, 7};
// 使用新算法进行排序
std::ranges::sort(numbers);
for (int n : numbers) {
std::cout << n << ' ';
}
return 0;
}
#include <iostream>
#include <regex>
int main() {
std::string text = 'Hello, C++20 is amazing!';
std::regex pattern('C\\+\\+20');
if (std::regex_search(text, pattern)) {
std::cout << 'Found C++20 in the text!' << std::endl;
}
return 0;
}
#include <iostream>
#include <memory>
struct MyClass {
void sayHello() {
std::cout << 'Hello, C++20 Smart Pointers!' << std::endl;
}
};
int main() {
std::shared_ptr<MyClass> myObject = std::make_shared<MyClass>();
myObject->sayHello();
return 0;
}
#include <iostream>
#include <vector>
#include <typeinfo>
template <typename T>
void printTypeName(const T& value) {
std::cout << 'Type of value: ' << typeid(value).name() << std::endl;
}
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
printTypeName(numbers);
return 0;
}
联系客服