既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
1
2
3
4
Press any key to continue . . .
这个知识点帮我解决一个棘手的问题:
问题背景: 上层传给我一个路径字符串 内容是四个文件 分别以分好分割 a/file1.txt;b/file2.tx2;c/file3.txt;d/file4.txt
我需要提取这四个文件并且做性对应的处理,这里我需要解决两个问题,字符串路径的分割处理和数组的循环
@echo off
set filelist=a/file1.txt;b/file2.tx2;c/file3.txt;d/file4.txt
for %%a in (%filelist%) do ( echo %%~nxa)
pause
输出结果:
file1.txt
file2.tx2
file3.txt
file4.txt
Press any key to continue . . .
说明:
%%~n 代表文件名
%%~x 代表文件扩展名
%%~nx 代表文件名和扩展名这里是引用
访问数组
可以使用下标语法从数组中检索值,将要检索的值的索引直接在方括号中传递到数组的名称之后。
@echo off
set a[0]=1
set a[1]=2
set a[2]=3
echo The first element of the array is %a[0]%
echo The second element of the array is %a[1]%
echo The third element of the array is %a[2]%
输出结果:
The first element of the array is 1
The second element of the array is 2
The third element of the array is 3
遍历数组
通过使用’ for '循环并遍历数组中的每个元素来实现对数组的迭代。下面的例子展示了一种实现数组的简单方法。
@echo off
setlocal enabledelayedexpansion
set topic[0]=comments
set topic[1]=variables
set topic[2]=Arrays
set topic[3]=Decision making
set topic[4]=Time and date
set topic[5]=Operators
for /l %%n in (0,1,5) do (
echo !topic[%%n]!
)
pause
输出结果:
The first element of the array is 1
The second element of the array is 2
The third element of the array is 3
for /l %%n in (0,1,5) 这行代码是for 循环的一种使用方式 for /L %%变量 in (起始值,每次增值,结束时的比较值) do 命令 , in (0,1,5) 会生成一个序列 [0,1,2,3,4],具体参考bat for in用法
求数组长度
数组的长度是通过迭代数组中的值列表来完成的,因为没有直接函数来确定数组中的元素数量。
@echo off
set Arr[0]=1
set Arr[1]=2
set Arr[2]=3
set Arr[3]=4
set "x=0"
:SymLoop
if defined Arr[%x%] (
call echo %%Arr[%x%]%%
set /a "x+=1"
GOTO :SymLoop
)
echo "The length of the array is" %x%
pause
输出结果:
1
2
3
4
"The length of the array is" 4
Press any key to continue . . .
数组结构体
结构也可以在批处理文件中实现,使用一点额外的代码来实现。下面的示例展示了如何实现这一点。
@echo off
set obj[0].Name=Joe
set obj[0].ID=1
set obj[1].Name=Mark
set obj[1].ID=2
set obj[2].Name=Mohan
set obj[2].ID=3
FOR /L %%i IN (0 1 2) DO (
call echo Name = %%obj[%%i].Name%%
call echo Value = %%obj[%%i].ID%%
)
pause
输出结果:
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
1720)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新