在使用 gadget
之前,我们需要在 Jupyter Notebook 中注册它的魔法命令,
这样就可以用 %%gadget
直接在 Notebook 单元格里写入、保存和编译 C/C++ 代码。
下面的代码完成了这个初始化步骤:
from gadget import gadget, undo_magic_name
from IPython.core.getipython import get_ipython
ipython = get_ipython()
ipython.register_magic_function(gadget, 'cell')
C 语言手动资源管理示例
在 C 语言中,动态分配的内存需要开发者手动管理。
这里我们使用 malloc
分配一块可以容纳 4 个 int
的内存,然后依次赋值、遍历求和,最后使用 free
释放内存。
这种方式容易出现 内存泄漏(忘记释放)或 野指针(释放后继续使用)等问题。
下面的代码演示了这个过程:
%%gadget -c ./oldc.c
#include<stdlib.h>
#include<stdio.h>
int main(){
size_t nv = 4;
int *v = (int *)malloc(nv * sizeof(int));
v[0] = 4;
v[1] = 3;
v[2] = 2;
v[3] = 1;
int sum = 0;
for(size_t i=0; i<nv; i++)
sum += v[i];
printf("%d\n", sum);
free(v);
return 0;
}
运行编译命令并执行程序,得到结果:
!gcc ./oldc.c -o ./oldc
!./oldc
输出:
10
讲解:
malloc
分配的内存在程序结束前必须手动调用free
释放,否则会造成内存泄漏。 当代码逻辑复杂、分支较多时,很容易漏掉free
,这也是后来 RAII 思想被引入 C++ 的原因之一。
C++98 使用 STL 容器自动管理内存
相比 C 语言需要手动 malloc
/ free
,C++98 引入的 标准模板库(STL) 提供了容器类,例如 std::vector
,可以自动管理内存。
使用 std::vector
时,容器在构造函数中分配内存,在析构函数中自动释放,不需要开发者手动调用 free
,大大降低了内存泄漏的风险。
下面的代码使用 std::vector<int>
存储 4 个整数,并通过传统的 for
循环遍历求和:
%%gadget -c cpp_98_stl.cpp
#include<vector>
#include<iostream>
int main(){
std::vector<int> v(4);
v[0] = 4;
v[1] = 3;
v[2] = 2;
v[3] = 1;
int sum = 0;
for(size_t i=0; i<v.size(); i++)
sum += v[i];
std::cout << sum << std::endl;
return 0;
}
为了构建该程序,我们使用 CMake 来生成 Makefile 并编译:
%%gadget -c CMakeLists.txt
# 设置 CMake 最低版本要求
cmake_minimum_required(VERSION 3.10)
# 设置项目名称
project(Cpp98StlProject)
# 指定使用 C++98 标准
set(CMAKE_CXX_STANDARD 98)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# 添加可执行文件
add_executable(cpp_98_stl cpp_98_stl.cpp)
编译并运行:
!cmake -B build
!cmake --build build --target cpp_98_stl
!build/cpp_98_stl
输出结果:
10
讲解:
std::vector
在作用域结束时会自动释放内存,避免了手动释放的负担。- C++98 虽然引入了自动内存管理的容器,但遍历依然需要传统的
for
循环,这在之后的 C++11 中得到了改进。
明白,我帮你写纯内容的 Markdown,紧贴代码块,直接补充讲解部分,没有“这篇博客”之类的描述:
````md
# 使用 C++11 标准和基础循环计算向量元素之和
下面的代码定义了一个整数向量 `v`,初始化为 `{4, 3, 2, 1}`。
通过传统的 `for` 循环遍历向量中的每个元素,将其累加到变量 `sum` 中。最终输出的结果是所有元素的和,即 `10`。
```cpp
#include <vector>
#include <iostream>
int main(){
std::vector<int> v = {4, 3, 2, 1};
int sum = 0;
for(size_t i=0; i<v.size(); i++)
sum += v[i];
std::cout << sum << std::endl;
return 0;
}
使用 CMake 进行项目构建时,指定了 C++11 标准以确保代码兼容。CMakeLists.txt 内容如下:
cmake_minimum_required(VERSION 3.10)
project(Cpp11LambdaProject)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_executable(cpp_11_lambda cpp_11_lambda.cpp)
运行编译和执行命令:
cmake -B build
cmake --build build --target cpp_11_lambda
./build/cpp_11_lambda
输出结果为:
10
使用 C++11 范围基循环计算向量元素之和
以下代码演示了 C++11 引入的范围基 for
循环(range-based for loop)用法。
它简化了对容器元素的遍历,不需要使用下标或迭代器,代码更加简洁易读。
#include <vector>
#include <iostream>
int main(){
std::vector<int> v = {4, 3, 2, 1};
int sum = 0;
for (int vi : v)
sum += vi;
std::cout << sum << std::endl;
return 0;
}
- 变量
vi
依次取出向量v
中的每个元素值。 - 循环体中将元素累加到
sum
。 - 输出最终的累加结果
10
。
使用 CMake 构建项目,设置 C++11 标准:
cmake_minimum_required(VERSION 3.10)
project(cpp11RangeBasedForLoopProject)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_executable(cpp_11_range_based_for_loop cpp_11_range_based_for_loop.cpp)
编译执行命令:
cmake -B build
cmake --build build --target cpp_11_range_based_for_loop
./build/cpp_11_range_based_for_loop
输出结果:
10
范围基循环让代码更简洁,适合遍历 STL 容器和数组,是 C++11 推广使用的现代循环方式。
# 使用 `std::for_each` 和函数进行向量元素求和
以下代码使用标准库算法 `std::for_each` 来遍历向量中的每个元素,并通过自定义函数 `func` 累加元素值。
```cpp
#include <vector>
#include <iostream>
#include <algorithm>
int sum = 0;
void func(int vi){
sum += vi;
}
int main(){
std::vector<int> v = {4, 3, 2, 1};
std::for_each(v.begin(), v.end(), func);
std::cout << sum << std::endl;
return 0;
}
- 定义了全局变量
sum
用于累加。 func
函数接收元素值,将其加到sum
上。- 使用
std::for_each
遍历向量,传入func
作为操作函数。 - 最终输出元素和
10
。
使用 CMake 配置项目,指定 C++11 标准:
cmake_minimum_required(VERSION 3.12)
project(cpp11ForEachProject)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_executable(cpp_11_for_each cpp_11_for_each.cpp)
编译和执行命令:
cmake -B build
cmake --build build --target cpp_11_for_each
./build/cpp_11_for_each
输出结果:
10
std::for_each
是 STL 提供的通用算法,允许对区间内每个元素执行自定义操作。结合函数或 Lambda,可实现灵活的数据处理。
# 使用 C++11 Lambda 表达式与 `std::for_each` 计算向量元素和
该示例使用 C++11 的 Lambda 表达式简化了对容器元素的遍历与处理。
Lambda 表达式通过捕获外部变量,直接在函数体内修改累加变量 `sum`,代码更加简洁明了。
```cpp
#include <vector>
#include <iostream>
#include <algorithm>
int main(){
std::vector<int> v = {4, 3, 2, 1};
int sum = 0;
std::for_each(v.begin(), v.end(), [&] (int vi){
sum += vi;
});
std::cout << sum << std::endl;
return 0;
}
[&]
表示捕获外部所有变量的引用,这里用来访问和修改外部变量sum
。- Lambda 参数
(int vi)
表示接收每个元素。 - Lambda 函数体将元素累加到
sum
。 - 最终输出累加结果
10
。
项目使用 CMake 设置 C++11 标准:
cmake_minimum_required(VERSION 3.12)
project(cpp11LambdaExpressionProject)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_executable(cpp_11_lambda_expression cpp_11_lambda_expression.cpp)
编译和运行命令:
cmake -B build
cmake --build build --target cpp_11_lambda_expression
./build/cpp_11_lambda_expression
输出结果:
10
Lambda 表达式使代码更加现代和灵活,配合 STL 算法可极大提升编写效率和可读性。
传统 C 语言中的动态数组求和示例
下面代码展示了用 C 语言动态分配数组并计算元素和的经典写法:
#include <stdlib.h>
#include <stdio.h>
int main(){
size_t nv = 4;
int *v = (int *)malloc(nv * sizeof(int));
v[0] = 4;
v[1] = 3;
v[2] = 2;
v[3] = 1;
int sum = 0;
for(size_t i=0; i<nv; i++)
sum += v[i];
printf("%d\n", sum);
free(v);
return 0;
}
- 通过
malloc
动态分配整型数组空间。 - 使用
for
循环遍历数组元素,累加求和。 - 结果输出为
10
。 - 使用完毕后调用
free
释放内存,避免内存泄漏。
相比 C++ 的 std::vector
,这种写法需要手动管理内存,容易出错。
C++ 容器和智能指针极大简化了动态内存管理,推荐在现代代码中优先使用。
运行命令及输出:
gcc ./oldc.c -o ./oldc
./oldc
输出结果:
10
使用 C++98 标准和 STL 容器的向量求和示例
下面的代码展示了在 C++98 标准下,使用 std::vector
容器和传统 for
循环来计算元素和的方法。
#include <vector>
#include <iostream>
int main(){
std::vector<int> v(4);
v[0] = 4;
v[1] = 3;
v[2] = 2;
v[3] = 1;
int sum = 0;
for(size_t i = 0; i < v.size(); i++)
sum += v[i];
std::cout << sum << std::endl;
return 0;
}
- 使用
std::vector<int> v(4);
创建固定大小为4的向量。 - 通过下标访问元素并赋值。
- 使用
for
循环遍历向量元素进行累加。 - 输出结果为
10
。
项目的 CMake 配置文件如下,指定使用 C++98 标准:
cmake_minimum_required(VERSION 3.10)
project(Cpp98StlProject)
set(CMAKE_CXX_STANDARD 98)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_executable(cpp_98_stl cpp_98_stl.cpp)
编译并运行命令:
cmake -B build
cmake --build build --target cpp_98_stl
./build/cpp_98_stl
输出结果:
10
该示例展示了 C++98 标准下 STL 容器的基本用法和传统循环结构,适合复习早期 C++ 标准的基础知识。
%%gadget -c cpp_11_lambda.cpp
#include<vector>
#include<iostream>
int main(){
std::vector<int> v = {4, 3, 2, 1};
int sum = 0;
for(size_t i=0; i<v.size(); i++)
sum += v[i];
std::cout << sum << std::endl;
return 0;
}
文件 cpp_11_lambda.cpp 创建成功,内容已写入。
%%gadget -c CMakeLists.txt
# 设置 CMake 最低版本要求
cmake_minimum_required(VERSION 3.10)
# 设置项目名称
project(Cpp11LambdaProject)
# 指定使用 C++11 标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# 添加可执行文件
add_executable(cpp_11_lambda cpp_11_lambda.cpp)
文件 CMakeLists.txt 创建成功,内容已写入。
!cmake -B build
!cmake --build build --target cpp_11_lambda
!build/cpp_11_lambda
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/build
gmake[1]: Warning: File 'CMakeFiles/Makefile2' has modification time 1.6 s in the future
gmake[2]: Warning: File 'CMakeFiles/Makefile2' has modification time 1.6 s in the future
gmake[3]: Warning: File 'CMakeFiles/cpp_11_lambda.dir/progress.make' has modification time 1.5 s in the future
[35m[1mConsolidate compiler generated dependencies of target cpp_11_lambda[0m
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
gmake[3]: Warning: File 'CMakeFiles/cpp_11_lambda.dir/progress.make' has modification time 1.1 s in the future
[ 50%] [32mBuilding CXX object CMakeFiles/cpp_11_lambda.dir/cpp_11_lambda.cpp.o[0m
[100%] [32m[1mLinking CXX executable cpp_11_lambda[0m
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
[100%] Built target cpp_11_lambda
gmake[2]: warning: Clock skew detected. Your build may be incomplete.
gmake[1]: warning: Clock skew detected. Your build may be incomplete.
10
%%gadget -c cpp_11_range_based_for_loop.cpp
#include<vector>
#include<iostream>
int main(){
std::vector<int> v = {4, 3, 2, 1};
int sum = 0;
for (int vi:v)
sum += vi;
std::cout << sum << std::endl;
return 0;
}
文件 cpp_11_range_based_for_loop.cpp 创建成功,内容已写入。
%%gadget -c CMakeLists.txt
# 设置 CMake 最低版本要求
cmake_minimum_required(VERSION 3.10)
# 设置项目名称
project(cpp11RangeBasedForLoopProject)
# 指定使用 C++11 标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# 添加可执行文件
add_executable(cpp_11_range_based_for_loop cpp_11_range_based_for_loop.cpp)
文件 CMakeLists.txt 创建成功,内容已写入。
!cmake -B build
!cmake --build build --target cpp_11_range_based_for_loop
!build/cpp_11_range_based_for_loop
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/build
gmake: Warning: File 'Makefile' has modification time 1.4 s in the future
gmake[1]: Warning: File 'CMakeFiles/Makefile2' has modification time 1.4 s in the future
gmake[2]: Warning: File 'CMakeFiles/Makefile2' has modification time 1.3 s in the future
gmake[3]: Warning: File 'CMakeFiles/cpp_11_range_based_for_loop.dir/progress.make' has modification time 1.2 s in the future
[35m[1mConsolidate compiler generated dependencies of target cpp_11_range_based_for_loop[0m
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
gmake[3]: Warning: File 'CMakeFiles/cpp_11_range_based_for_loop.dir/progress.make' has modification time 0.75 s in the future
[ 50%] [32mBuilding CXX object CMakeFiles/cpp_11_range_based_for_loop.dir/cpp_11_range_based_for_loop.cpp.o[0m
[100%] [32m[1mLinking CXX executable cpp_11_range_based_for_loop[0m
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
[100%] Built target cpp_11_range_based_for_loop
gmake[2]: warning: Clock skew detected. Your build may be incomplete.
gmake[1]: warning: Clock skew detected. Your build may be incomplete.
gmake: warning: Clock skew detected. Your build may be incomplete.
10
%%gadget -c cpp_11_for_each.cpp
#include<vector>
#include<iostream>
#include <algorithm>
int sum = 0;
void func(int vi){
sum += vi;
}
int main(){
std::vector<int> v = {4, 3, 2, 1};
std::for_each(v.begin(), v.end(), func);
std::cout << sum << std::endl;
return 0;
}
文件 cpp_11_for_each.cpp 创建成功,内容已写入。
%%gadget -c CMakeLists.txt
# 设置 CMake 最低版本要求
cmake_minimum_required(VERSION 3.12)
# 设置项目名称
project(cpp11ForEachProject)
# 指定使用 C++11 标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# 添加可执行文件
add_executable(cpp_11_for_each cpp_11_for_each.cpp)
文件 CMakeLists.txt 创建成功,内容已写入。
!cmake -B build
!cmake --build build --target cpp_11_for_each
!build/cpp_11_for_each
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/build
gmake: Warning: File 'Makefile' has modification time 1.1 s in the future
gmake[1]: Warning: File 'CMakeFiles/Makefile2' has modification time 1.2 s in the future
gmake[2]: Warning: File 'CMakeFiles/Makefile2' has modification time 1.1 s in the future
gmake[3]: Warning: File 'CMakeFiles/cpp_11_for_each.dir/progress.make' has modification time 0.96 s in the future
[35m[1mConsolidate compiler generated dependencies of target cpp_11_for_each[0m
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
gmake[3]: Warning: File 'CMakeFiles/cpp_11_for_each.dir/progress.make' has modification time 0.51 s in the future
[ 50%] [32mBuilding CXX object CMakeFiles/cpp_11_for_each.dir/cpp_11_for_each.cpp.o[0m
[100%] [32m[1mLinking CXX executable cpp_11_for_each[0m
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
[100%] Built target cpp_11_for_each
gmake[2]: warning: Clock skew detected. Your build may be incomplete.
gmake[1]: warning: Clock skew detected. Your build may be incomplete.
gmake: warning: Clock skew detected. Your build may be incomplete.
10
%%gadget -c cpp_11_lambda_expression.cpp
#include<vector>
#include<iostream>
#include <algorithm>
int main(){
std::vector<int> v = {4, 3, 2, 1};
int sum = 0;
std::for_each(v.begin(), v.end(), [&] (int vi){
sum += vi;
});
std::cout << sum << std::endl;
return 0;
}
文件 cpp_11_lambda_expression.cpp 创建成功,内容已写入。
%%gadget -c CMakeLists.txt
# 设置 CMake 最低版本要求
cmake_minimum_required(VERSION 3.12)
# 设置项目名称
project(cpp11LambdaExpressionProject)
# 指定使用 C++11 标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# 添加可执行文件
add_executable(cpp_11_lambda_expression cpp_11_lambda_expression.cpp)
文件 CMakeLists.txt 创建成功,内容已写入。
!cmake -B build
!cmake --build build --target cpp_11_lambda_expression
!build/cpp_11_lambda_expression
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/build
gmake: Warning: File 'Makefile' has modification time 0.97 s in the future
gmake[1]: Warning: File 'CMakeFiles/Makefile2' has modification time 1 s in the future
gmake[2]: Warning: File 'CMakeFiles/Makefile2' has modification time 0.91 s in the future
gmake[3]: Warning: File 'CMakeFiles/cpp_11_lambda_expression.dir/progress.make' has modification time 0.84 s in the future
[35m[1mConsolidate compiler generated dependencies of target cpp_11_lambda_expression[0m
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
gmake[3]: Warning: File 'CMakeFiles/cpp_11_lambda_expression.dir/progress.make' has modification time 0.49 s in the future
[ 50%] [32mBuilding CXX object CMakeFiles/cpp_11_lambda_expression.dir/cpp_11_lambda_expression.cpp.o[0m
[100%] [32m[1mLinking CXX executable cpp_11_lambda_expression[0m
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
[100%] Built target cpp_11_lambda_expression
gmake[2]: warning: Clock skew detected. Your build may be incomplete.
gmake[1]: warning: Clock skew detected. Your build may be incomplete.
gmake: warning: Clock skew detected. Your build may be incomplete.
10
%%gadget -c cpp_14_lambda_auto.cpp
#include<vector>
#include<iostream>
#include <algorithm>
int main(){
std::vector<int> v = {4, 3, 2, 1};
int sum = 0;
std::for_each(v.begin(), v.end(), [&] (auto vi){
sum += vi;
});
std::cout << sum << std::endl;
return 0;
}
文件 cpp_14_lambda_auto.cpp 创建成功,内容已写入。
%%gadget -c CMakeLists.txt
# 设置 CMake 最低版本要求
cmake_minimum_required(VERSION 3.12)
# 设置项目名称
project(cpp14LambdaAutoProject)
# 指定使用 C++14 标准
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# 添加可执行文件
add_executable(cpp_14_lambda_auto cpp_14_lambda_auto.cpp)
文件 CMakeLists.txt 创建成功,内容已写入。
!cmake -B build
!cmake --build build --target cpp_14_lambda_auto
!build/cpp_14_lambda_auto
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/build
gmake: Warning: File 'Makefile' has modification time 0.68 s in the future
gmake[1]: Warning: File 'CMakeFiles/Makefile2' has modification time 0.72 s in the future
gmake[2]: Warning: File 'CMakeFiles/Makefile2' has modification time 0.56 s in the future
gmake[3]: Warning: File 'CMakeFiles/cpp_14_lambda_auto.dir/progress.make' has modification time 0.46 s in the future
[35m[1mConsolidate compiler generated dependencies of target cpp_14_lambda_auto[0m
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
gmake[3]: Warning: File 'CMakeFiles/cpp_14_lambda_auto.dir/compiler_depend.make' has modification time 0.88 s in the future
[ 50%] [32mBuilding CXX object CMakeFiles/cpp_14_lambda_auto.dir/cpp_14_lambda_auto.cpp.o[0m
[100%] [32m[1mLinking CXX executable cpp_14_lambda_auto[0m
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
[100%] Built target cpp_14_lambda_auto
gmake[2]: warning: Clock skew detected. Your build may be incomplete.
gmake[1]: warning: Clock skew detected. Your build may be incomplete.
gmake: warning: Clock skew detected. Your build may be incomplete.
10
!cmake -B build
!cmake --build build --target cpp_14_lambda_auto
!build/cpp_14_lambda_auto
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/build
gmake: Warning: File 'Makefile' has modification time 0.48 s in the future
gmake[1]: Warning: File 'CMakeFiles/Makefile2' has modification time 0.61 s in the future
gmake[2]: Warning: File 'CMakeFiles/Makefile2' has modification time 0.53 s in the future
gmake[3]: Warning: File 'CMakeFiles/cpp_14_lambda_auto.dir/progress.make' has modification time 0.44 s in the future
[35m[1mConsolidate compiler generated dependencies of target cpp_14_lambda_auto[0m
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
gmake[3]: Warning: File 'CMakeFiles/cpp_14_lambda_auto.dir/progress.make' has modification time 0.15 s in the future
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
[100%] Built target cpp_14_lambda_auto
gmake[2]: warning: Clock skew detected. Your build may be incomplete.
gmake[1]: warning: Clock skew detected. Your build may be incomplete.
gmake: warning: Clock skew detected. Your build may be incomplete.
10
%%gadget -c cpp_17_CTAD.cpp
#include<vector>
#include<iostream>
#include <algorithm>
int main(){
std::vector<int> v = {4, 3, 2, 1};
int sum = 0;
std::for_each(v.begin(), v.end(), [&] (auto vi){
sum += vi;
});
std::cout << sum << std::endl;
return 0;
}
文件 cpp_17_CTAD.cpp 创建成功,内容已写入。
%%gadget -c CMakeLists.txt
# 设置 CMake 最低版本要求
cmake_minimum_required(VERSION 3.12)
# 设置项目名称
project(cpp17CTADProject)
# 指定使用 C++17 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# 添加可执行文件
add_executable(cpp_17_CTAD cpp_17_CTAD.cpp)
文件 CMakeLists.txt 创建成功,内容已写入。
!cmake -B build
!cmake --build build --target cpp_17_CTAD
!build/cpp_17_CTAD
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/build
gmake: Warning: File 'Makefile' has modification time 0.35 s in the future
gmake[1]: Warning: File 'CMakeFiles/Makefile2' has modification time 0.45 s in the future
gmake[2]: Warning: File 'CMakeFiles/Makefile2' has modification time 0.34 s in the future
gmake[3]: Warning: File 'CMakeFiles/cpp_17_CTAD.dir/progress.make' has modification time 0.23 s in the future
[35m[1mConsolidate compiler generated dependencies of target cpp_17_CTAD[0m
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
gmake[3]: Warning: File 'CMakeFiles/cpp_17_CTAD.dir/compiler_depend.make' has modification time 0.51 s in the future
[ 50%] [32mBuilding CXX object CMakeFiles/cpp_17_CTAD.dir/cpp_17_CTAD.cpp.o[0m
[100%] [32m[1mLinking CXX executable cpp_17_CTAD[0m
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
[100%] Built target cpp_17_CTAD
gmake[2]: warning: Clock skew detected. Your build may be incomplete.
gmake[1]: warning: Clock skew detected. Your build may be incomplete.
gmake: warning: Clock skew detected. Your build may be incomplete.
10
%%gadget -c cpp_17_numeric.cpp
#include<vector>
#include<iostream>
#include <numeric>
int main(){
std::vector v = {4, 3, 2, 1};
int sum = std::reduce(v.begin(), v.end());
std::cout << sum << std::endl;
return 0;
}
文件 cpp_17_numeric.cpp 创建成功,内容已写入。
%%gadget -c CMakeLists.txt
# 设置 CMake 最低版本要求
cmake_minimum_required(VERSION 3.12)
# 设置项目名称
project(cpp17CTADProject)
# 指定使用 C++17 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# 添加可执行文件
add_executable(cpp_17_numeric cpp_17_numeric.cpp)
文件 CMakeLists.txt 创建成功,内容已写入。
!cmake -B build
!cmake --build build --target cpp_17_numeric
!build/cpp_17_numeric
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/build
gmake[1]: Warning: File 'CMakeFiles/Makefile2' has modification time 0.12 s in the future
gmake[2]: Warning: File 'CMakeFiles/Makefile2' has modification time 0.0069 s in the future
[35m[1mConsolidate compiler generated dependencies of target cpp_17_numeric[0m
gmake[3]: Warning: File 'CMakeFiles/cpp_17_numeric.dir/compiler_depend.make' has modification time 0.33 s in the future
[ 50%] [32mBuilding CXX object CMakeFiles/cpp_17_numeric.dir/cpp_17_numeric.cpp.o[0m
[100%] [32m[1mLinking CXX executable cpp_17_numeric[0m
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
[100%] Built target cpp_17_numeric
gmake[2]: warning: Clock skew detected. Your build may be incomplete.
gmake[1]: warning: Clock skew detected. Your build may be incomplete.
10
%%gadget -c cpp_20_ranges.cpp
#include<vector>
#include<iostream>
#include<numeric>
#include<ranges>
#include<cmath>
int main(){
std::vector v = {4, 3, 2, 1, 0, -1, -2};
for (auto &&vi : v
| std::views::filter([](auto &&x) {return x >= 0; })
| std::views::transform([] (auto &&x) { return sqrtf(x); })
){
std::cout << vi << std::endl;
}
return 0;
}
文件 cpp_20_ranges.cpp 创建成功,内容已写入。
%%gadget -c CMakeLists.txt
# 设置 CMake 最低版本要求
cmake_minimum_required(VERSION 3.12)
# 设置项目名称
project(cpp20RangesProject)
# 指定使用 C++20 标准
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# 添加可执行文件
add_executable(cpp_20_ranges cpp_20_ranges.cpp)
文件 CMakeLists.txt 创建成功,内容已写入。
!cmake -B build
!cmake --build build --target cpp_20_ranges
!build/cpp_20_ranges
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/build
[35m[1mConsolidate compiler generated dependencies of target cpp_20_ranges[0m
[ 50%] [32mBuilding CXX object CMakeFiles/cpp_20_ranges.dir/cpp_20_ranges.cpp.o[0m
[100%] [32m[1mLinking CXX executable cpp_20_ranges[0m
[100%] Built target cpp_20_ranges
2
1.73205
1.41421
1
0
%%gadget -c cpp_20_module.cpp
import <vector>;
import <iostream>;
import <numeric>;
import <ranges>;
import <cmath>;
int main(){
std::vector v = {4, 3, 2, 1, 0, -1, -2};
for (auto &&vi : v
| std::views::filter([](auto &&x) {return x >= 0; })
| std::views::transform([] (auto &&x) { return sqrtf(x); })
){
std::cout << vi << std::endl;
}
return 0;
}
文件 cpp_20_module.cpp 创建成功,内容已写入。
%%gadget -c CMakeLists.txt
# 设置 CMake 最低版本要求
cmake_minimum_required(VERSION 3.12)
# 设置项目名称
project(cpp20ModuleProject)
# 指定使用 C++20 标准
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# 添加可执行文件
add_executable(cpp_20_module cpp_20_module.cpp)
文件 CMakeLists.txt 创建成功,内容已写入。
!cmake -B build
!cmake --build build --target cpp_20_module
!build/cpp_20_module
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/build
[35m[1mConsolidate compiler generated dependencies of target cpp_20_module[0m
[ 50%] [32mBuilding CXX object CMakeFiles/cpp_20_module.dir/cpp_20_module.cpp.o[0m
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:2:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kvector[m[K’ was not declared in this scope
2 | import <[01;31m[Kvector[m[K>;
| [01;31m[K^~~~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:2:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kvector[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:2:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kvector[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:2:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kvector[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:2:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kvector[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:2:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kvector[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:2:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kvector[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:2:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kvector[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:2:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kvector[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:2:1:[m[K [01;31m[Kerror: [m[K‘[01m[Kimport[m[K’ does not name a type
2 | [01;31m[Kimport[m[K <vector>;
| [01;31m[K^~~~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:2:1:[m[K [01;36m[Knote: [m[KC++20 ‘[01m[Kimport[m[K’ only available with ‘[01m[K-fmodules-ts[m[K’, which is not yet enabled with ‘[01m[K-std=c++20[m[K’
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:3:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kiostream[m[K’ was not declared in this scope
3 | import <[01;31m[Kiostream[m[K>;
| [01;31m[K^~~~~~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:3:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kiostream[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:3:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kiostream[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:3:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kiostream[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:3:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kiostream[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:3:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kiostream[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:3:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kiostream[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:3:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kiostream[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:3:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kiostream[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:3:1:[m[K [01;31m[Kerror: [m[K‘[01m[Kimport[m[K’ does not name a type
3 | [01;31m[Kimport[m[K <iostream>;
| [01;31m[K^~~~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:3:1:[m[K [01;36m[Knote: [m[KC++20 ‘[01m[Kimport[m[K’ only available with ‘[01m[K-fmodules-ts[m[K’, which is not yet enabled with ‘[01m[K-std=c++20[m[K’
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:4:9:[m[K [01;31m[Kerror: [m[K‘[01m[Knumeric[m[K’ was not declared in this scope
4 | import <[01;31m[Knumeric[m[K>;
| [01;31m[K^~~~~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:4:9:[m[K [01;31m[Kerror: [m[K‘[01m[Knumeric[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:4:9:[m[K [01;31m[Kerror: [m[K‘[01m[Knumeric[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:4:9:[m[K [01;31m[Kerror: [m[K‘[01m[Knumeric[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:4:9:[m[K [01;31m[Kerror: [m[K‘[01m[Knumeric[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:4:9:[m[K [01;31m[Kerror: [m[K‘[01m[Knumeric[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:4:9:[m[K [01;31m[Kerror: [m[K‘[01m[Knumeric[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:4:9:[m[K [01;31m[Kerror: [m[K‘[01m[Knumeric[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:4:9:[m[K [01;31m[Kerror: [m[K‘[01m[Knumeric[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:4:1:[m[K [01;31m[Kerror: [m[K‘[01m[Kimport[m[K’ does not name a type
4 | [01;31m[Kimport[m[K <numeric>;
| [01;31m[K^~~~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:4:1:[m[K [01;36m[Knote: [m[KC++20 ‘[01m[Kimport[m[K’ only available with ‘[01m[K-fmodules-ts[m[K’, which is not yet enabled with ‘[01m[K-std=c++20[m[K’
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:5:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kranges[m[K’ was not declared in this scope
5 | import <[01;31m[Kranges[m[K>;
| [01;31m[K^~~~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:5:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kranges[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:5:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kranges[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:5:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kranges[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:5:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kranges[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:5:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kranges[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:5:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kranges[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:5:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kranges[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:5:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kranges[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:5:1:[m[K [01;31m[Kerror: [m[K‘[01m[Kimport[m[K’ does not name a type
5 | [01;31m[Kimport[m[K <ranges>;
| [01;31m[K^~~~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:5:1:[m[K [01;36m[Knote: [m[KC++20 ‘[01m[Kimport[m[K’ only available with ‘[01m[K-fmodules-ts[m[K’, which is not yet enabled with ‘[01m[K-std=c++20[m[K’
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:6:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kcmath[m[K’ was not declared in this scope
6 | import <[01;31m[Kcmath[m[K>;
| [01;31m[K^~~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:6:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kcmath[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:6:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kcmath[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:6:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kcmath[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:6:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kcmath[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:6:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kcmath[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:6:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kcmath[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:6:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kcmath[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:6:9:[m[K [01;31m[Kerror: [m[K‘[01m[Kcmath[m[K’ was not declared in this scope
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:6:1:[m[K [01;31m[Kerror: [m[K‘[01m[Kimport[m[K’ does not name a type
6 | [01;31m[Kimport[m[K <cmath>;
| [01;31m[K^~~~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:6:1:[m[K [01;36m[Knote: [m[KC++20 ‘[01m[Kimport[m[K’ only available with ‘[01m[K-fmodules-ts[m[K’, which is not yet enabled with ‘[01m[K-std=c++20[m[K’
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:[m[K In function ‘[01m[Kint main()[m[K’:
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:9:10:[m[K [01;31m[Kerror: [m[K‘[01m[Kvector[m[K’ is not a member of ‘[01m[Kstd[m[K’
9 | std::[01;31m[Kvector[m[K v = {4, 3, 2, 1, 0, -1, -2};
| [01;31m[K^~~~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:1:1:[m[K [01;36m[Knote: [m[K‘[01m[Kstd::vector[m[K’ is defined in header ‘[01m[K<vector>[m[K’; did you forget to ‘[01m[K#include <vector>[m[K’?
+++ |+[32m[K#include <vector>[m[K
1 |
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:10:22:[m[K [01;31m[Kerror: [m[K‘[01m[Kv[m[K’ was not declared in this scope; did you mean ‘[01m[Kvi[m[K’?
10 | for (auto &&vi : [01;31m[Kv[m[K
| [01;31m[K^[m[K
| [32m[Kvi[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:11:13:[m[K [01;31m[Kerror: [m[K‘[01m[Kstd::views[m[K’ has not been declared
11 | | std::[01;31m[Kviews[m[K::filter([](auto &&x) {return x >= 0; })
| [01;31m[K^~~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:12:13:[m[K [01;31m[Kerror: [m[K‘[01m[Kstd::views[m[K’ has not been declared
12 | | std::[01;31m[Kviews[m[K::transform([] (auto &&x) { return sqrtf(x); })
| [01;31m[K^~~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:14:14:[m[K [01;31m[Kerror: [m[K‘[01m[Kcout[m[K’ is not a member of ‘[01m[Kstd[m[K’
14 | std::[01;31m[Kcout[m[K << vi << std::endl;
| [01;31m[K^~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:1:1:[m[K [01;36m[Knote: [m[K‘[01m[Kstd::cout[m[K’ is defined in header ‘[01m[K<iostream>[m[K’; did you forget to ‘[01m[K#include <iostream>[m[K’?
+++ |+[32m[K#include <iostream>[m[K
1 |
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:14:33:[m[K [01;31m[Kerror: [m[K‘[01m[Kendl[m[K’ is not a member of ‘[01m[Kstd[m[K’
14 | std::cout << vi << std::[01;31m[Kendl[m[K;
| [01;31m[K^~~~[m[K
[01m[K/mnt/c/Users/Jeff/Desktop/cpp_course_from_scratch/cpp_20_module.cpp:1:1:[m[K [01;36m[Knote: [m[K‘[01m[Kstd::endl[m[K’ is defined in header ‘[01m[K<ostream>[m[K’; did you forget to ‘[01m[K#include <ostream>[m[K’?
+++ |+[32m[K#include <ostream>[m[K
1 |
gmake[3]: *** [CMakeFiles/cpp_20_module.dir/build.make:76: CMakeFiles/cpp_20_module.dir/cpp_20_module.cpp.o] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/cpp_20_module.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/cpp_20_module.dir/rule] Error 2
gmake: *** [Makefile:124: cpp_20_module] Error 2
/bin/bash: line 1: build/cpp_20_module: No such file or directory
cmake is not support c++ 20 module currently
%%gadget -c cpp_20_module.cpp
import std.vector;
import std.iostream;
import std.numeric;
import std.ranges;
import std.cmath;
int main(){
std::vector v = {4, 3, 2, 1, 0, -1, -2};
for (auto &&vi : v
| std::views::filter([](auto &&x) {return x >= 0; })
| std::views::transform([] (auto &&x) { return sqrtf(x); })
){
std::cout << vi << std::endl;
}
return 0;
}
文件 cpp_20_module.cpp 创建成功,内容已写入。
!g++-10 -std=c++20 -fmodules-ts ./cpp_20_module.cpp -o ./cpp_20_module
[01m[Kg++-10:[m[K [01;31m[Kerror: [m[Kunrecognized command-line option ‘[01m[K-fmodules-ts[m[K’; did you mean ‘[01m[K-fmodules[m[K’?
%%gadget -c cpp_20_module.cpp
import <vector>;
import <iostream>;
import <numeric>;
import <ranges>;
import <cmath>;
int main(){
std::vector v = {4, 3, 2, 1, 0, -1, -2};
for (auto &&vi : v
| std::views::filter([](auto &&x) {return x >= 0; })
| std::views::transform([] (auto &&x) { return sqrtf(x); })
){
std::cout << vi << std::endl;
}
return 0;
}
文件 cpp_20_module.cpp 创建成功,内容已写入。
!clang++ -std=c++20 -fmodules -fcxx-modules ./cpp_20_module.cpp -o ./cpp_20_module
[1m./cpp_20_module.cpp:2:8: [0m[0;1;31merror: [0m[1mheader file <vector> (aka '/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/vector') cannot be imported because it is not known to be a header unit[0m
import <vector>;
[0;1;32m ^
[0m[1m./cpp_20_module.cpp:3:8: [0m[0;1;31merror: [0m[1mheader file <iostream> (aka '/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/iostream') cannot be imported because it is not known to be a header unit[0m
import <iostream>;
[0;1;32m ^
[0m[1m./cpp_20_module.cpp:4:8: [0m[0;1;31merror: [0m[1mheader file <numeric> (aka '/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/numeric') cannot be imported because it is not known to be a header unit[0m
import <numeric>;
[0;1;32m ^
[0m[1m./cpp_20_module.cpp:5:8: [0m[0;1;31merror: [0m[1mheader file <ranges> (aka '/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ranges') cannot be imported because it is not known to be a header unit[0m
import <ranges>;
[0;1;32m ^
[0m[1m./cpp_20_module.cpp:6:8: [0m[0;1;31merror: [0m[1mheader file <cmath> (aka '/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/cmath') cannot be imported because it is not known to be a header unit[0m
import <cmath>;
[0;1;32m ^
[0m[1m./cpp_20_module.cpp:9:5: [0m[0;1;31merror: [0m[1muse of undeclared identifier 'std'[0m
std::vector v = {4, 3, 2, 1, 0, -1, -2};
[0;1;32m ^
[0m[1m./cpp_20_module.cpp:11:8: [0m[0;1;31merror: [0m[1muse of undeclared identifier 'std'[0m
| std::views::filter([](auto &&x) {return x >= 0; })
[0;1;32m ^
[0m[1m./cpp_20_module.cpp:12:8: [0m[0;1;31merror: [0m[1muse of undeclared identifier 'std'[0m
| std::views::transform([] (auto &&x) { return sqrtf(x); })
[0;1;32m ^
[0m[1m./cpp_20_module.cpp:14:9: [0m[0;1;31merror: [0m[1muse of undeclared identifier 'std'[0m
std::cout << vi << std::endl;
[0;1;32m ^
[0m[1m./cpp_20_module.cpp:14:28: [0m[0;1;31merror: [0m[1muse of undeclared identifier 'std'[0m
std::cout << vi << std::endl;
[0;1;32m ^
[0m10 errors generated.
%%gadget -c cpp_20_module_auto.cpp
import <vector>;
import <iostream>;
import <numeric>;
import <ranges>;
import <cmath>;
void myfunc(auto &&v){
for (auto &&vi : v
| std::views::filter([](auto &&x) {return x >= 0; })
| std::views::transform([] (auto &&x) { return sqrtf(x); })
){
std::cout << vi << std::endl;
}
}
int main(){
std::vector v = {4, 3, 2, 1, 0, -1, -2};
myfunc(v);
return 0;
}
文件 cpp_20_module_auto.cpp 创建成功,内容已写入。
%%gadget -c cpp_20_coroutine_generator.cpp
import <vector>;
import <iostream>;
import <numeric>;
import <ranges>;
import <cmath>;
import <generator>;
std::generator<int> myfunc(auto &&v){
for (auto &&vi : v
| std::views::filter([](auto &&x) {return x >= 0; })
| std::views::transform([] (auto &&x) { return sqrtf(x); })
){
co_yield vi;
}
}
int main(){
std::vector v = {4, 3, 2, 1, 0, -1, -2};
for (auto &&vi : myfunc(v))
std::cout << vi << std::endl;
return 0;
}
文件 cpp_20_coroutine_generator.cpp 创建成功,内容已写入。
%%gadget -c cpp_20_coroutine_generator_format.cpp
import <vector>;
import <iostream>;
import <numeric>;
import <ranges>;
import <cmath>;
import <generator>;
std::generator<int> myfunc(auto &&v){
for (auto &&vi : v
| std::views::filter([](auto &&x) {return x >= 0; })
| std::views::transform([] (auto &&x) { return sqrtf(x); })
){
co_yield vi;
}
}
int main(){
std::vector v = {4, 3, 2, 1, 0, -1, -2};
for (auto &&vi : myfunc(v))
std::format_to(std::cout, "num is: {}\n", vi);
return 0;
}
文件 cpp_20_coroutine_generator_format.cpp 创建成功,内容已写入。