导航:首页 > 操作系统 > linux数组长度

linux数组长度

发布时间:2022-06-10 09:16:56

linux shell 数组array基础和$a的区别

Linux Bash中,数组变量的赋值有两种方法:
(1) name = (value1 ... valuen) // 此时下标从0开始;
(2) name[index] = value
下面以一个简单的脚本来说明,脚本内容如下:
#!/bin/bash
#定义数组
A=(a b c def)
#把数组按字符串显示输出
echo ${A[@]}
#屏幕显示:a b c def
#数组的长度表示${#A[*]}
len=${#A[*]}
echo ${#A[*]}
#屏幕显示:4
#改变数组元素的值
A[3]='vivian'
echo ${A[*]}
#屏幕显示:a b c vivian
#循环输出数组元素
i=0
while [ $i -lt $len ]
do
echo ${A[$i]}
let i++
done
#屏幕输出:
# a
# b
# c
# vivian

#循环输出数组元素的另一种写法,注意,在条件中,用#a[@]取其个数。
for ((i=0;i<${#A[@]};i=$i+1))
do
echo ${A[$i]}
done
#循环输出数组元素的另一种写法,注意,在条件中,引用变量一定要用双引号 ,否则报错。
for (( j=0; j<"$len"; j=j+1 )) //len表示数组长度值
do
echo ${A[$j]}
done
#循环输出数组元素的另一种写法,注意,${A[*]}不能写成$A ,$A默认是第一个元素。 如果A="a b c ded",就可以写$A,
for value in ${A[*]}
do
echo $value
done
ps:
若a=(1 2 3 4)表示所有元素,则其只能用${a[*]}或者${a[@]}来表示。在a=(1 2 3 4)中,$a只是表示第一个元素1。
若a="1 2 3 4"表示所有元素,则其可以用${a[*]}或者${a[@]}或者$a来表示。
假如a="1 2 3 4",则array=($a)就相当于a=(1 2 3 4),不信你可以试试echo ${array[@]}。
上面的例子还可以改写成以下内容:
for value in $A
do
echo $value
done

Ⅱ Linux求数组长度为10000的浮点数(精确小数点右4位)计算值

既然题主没有说要求用什么语言,那我就用c++11实现了。

#include<iostream>

#include<random>

#include<thread>

#include<chrono>

#include<algorithm>

#include<iomanip>


usingnamespacestd;


constintsize=10000;

floattable[size];


intmain(){

random_deviceengine;

uniform_real_distribution<float>dist(0,1);

floatsum;


for(auto&i:table){

i=dist(engine);

}


autot_start=chrono::system_clock::now();

sum=accumulate(table,table+size,0.0);

autot_end=chrono::system_clock::now();

autoration=std::chrono::ration_cast<std::chrono::microseconds>(t_end-t_start).count();

cout<<"sumofthemainthread:"<<fixed<<setprecision(4)<<sum<<endl;

cout<<"timeelapsed:"<<ration<<"microseconds"<<endl;


floatsum_child[4];

autofun=[&](intindex){

sum_child[index]=accumulate(table+index*size/4,table+(index+1)*size/4,0.0);

};


t_start=chrono::system_clock::now();

threadthrd_table[4]={

thread(fun,0),thread(fun,1),thread(fun,2),thread(fun,3)

};

for(auto&thrd:thrd_table){

thrd.join();

}

sum=0;

sum=accumulate(sum_child,sum_child+4,0.0);

t_end=chrono::system_clock::now();

ration=std::chrono::ration_cast<std::chrono::microseconds>(t_end-t_start).count();

cout<<"sumofchildthreads:"<<fixed<<setprecision(4)<<sum<<endl;

cout<<"timeelapsed:"<<ration<<"microseconds"<<endl;


return0;

}


编译

g++-std=c++11test.cc-lpthread-otest

运行:

./test

结果:

sumofthemainthread:4976.8721

timeelapsed:0ms

sumofchildthreads:4976.8721

timeelapsed:0ms


由于随机性每次加和的数值不同,但是精确到毫秒时,时间测出来妥妥的都是零。就是数据量太小,实际运行时间在微秒量级,当然看不出来。

精度改为微秒以后:

sumofthemainthread:4957.9878

timeelapsed:113microseconds

sumofchildthreads:4957.9878

timeelapsed:560microseconds

多线程反而比单线程慢,因为启动线程本身也需要时间。

数据量再增大1000倍:

sumofthemainthread:4999892.0000

timeelapsed:25313microseconds

sumofchildthreads:4999892.0000

timeelapsed:8986microseconds

这回看着正常多了吧

Ⅲ C语言strlen求数组长度。为什么会是这样,linux下GCC编译器。

你换台机器就不是58的值了!所谓的固定,是你机器内存情况刚好处在那种状态下。

你没有,strlen()就会自动去找位置,这个0位置在什么位置是不确定的。

strlen(s)函数,从s首地址开始一直统计到位置,其中有几个字节就输出长度为几!

#include<stdio.h>
#include<string.h>
voidmain()
{
inti=0;//这里加上这个,你再去试,结果一定会有变化的,原理,自己思考一下吧
charp[]={1,2,3,4,5,66,7,};
charq[]={1,2,3,4};
charr[]={1,2,3,4};
printf("length:%d ",strlen(p));
printf("length2:%d ",strlen(q));
printf("length3:%d ",strlen(r));
}

Ⅳ linux中,c++ 结构体定义可变长数组成员时,为什么可变数组的长度一定要放在临近位置 具体看下面例子。

单从两个结构上看都没啥问题,之所以你说的出错是因为,客户端 和 服务端,的结构不一致导致的。 你的数据过去,服务端可能会先把数据扔到内存中,然后直接memcpy方式赋值给结构体对象。这就会导致a 和length是反的。

Ⅳ linux 命令 echo ${#array_var[*]} 中#的作用是什么

返回数组的长度比如$array_var="abcd"${#array_var}就返回4。

linux命令是对Linux系统进行管理的命令。对于Linux系统来说,无论是中央处理器、内存、磁盘驱动器、键盘、鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心,与之前的DOS命令类似。

如果你的英文足够好,那完全可以不靠任何人就精通linux,只要你会用man。Man实际上就是查看指令用法的help,学习任何一种UNIX类的操作系统最重要的就是学会使用man这个辅助命令。

Ⅵ shell 怎么获取数组长度

arr=(12345)
len=${#arr[@]}
echo$len

关于shell数组的更多操作,参见我的空间文章《shell数组与awk数组》

http://hi..com/eamontse/item/cb93d2457a1d91e51281daef

Ⅶ linux下java实现ftp下载,ftpClient.listFiles();返回的数组长度一直是0

如果你加了ftpClient.enterRemotePassiveMode();还是不行,那么就可能是你commons-net 的jia包版本太低,你可以试着引入 commons-net-3.3.jar以及以上版本的jar应该就可以了。

Ⅷ linux c 数组大小

自从C99标准开始,C语言支持变长数组,数组大小可以是变量

Ⅸ linux脚本,在一个循环中赋值一个数组

数组的赋值操作有问题,改成下面这样:
for ubootfile in $uboot_list
do
FILE=`find . -name $ubootfile -print -quit`
if [ -n "$FILE" ]; then
a=(${a[@]} $FILE)
else
b=(${b[@]} $FILE)
fi
done
echo "found files:"
echo ${a[@]}
echo "missing files:"
echo ${b[@]}

下面是数组操作的讲解,请参考:
数组作为一种特殊的数据结构在任何一种编程语言中都有它的一席之地,当然bashshell也不例外。本文就shell数组来做一个小的总结。
在这里只讨论一维数组的情况,关于多维数组(事实上,你得用一维数组的方法来模拟),不涉及。这里包括数组的复制,计算,删除,替换。

数组的声明:
1)array[key]=value # array[0]=one,array[1]=two
2)declare -a array # array被当作数组名
3)array=(value1 value2 value3 ... )
4)array=([1]=one [2]=two [3]=three ... )
5)array="one two three" # echo ${array[0|@|*]},把array变量当作数组来处理,但数组元素只有字符串本身
数组的访问:
1)${array[key]} # ${array[1]}
数组的删除
1)unsetarray[1] # 删除数组中第一个元素
2)unset array # 删除整个数组
计算数组的长度:
1)${#array}
2)${#array[0]}#同上。 ${#array[*]} 、${#array[@]}。注意同#{array:0}的区别
数组的提取
从尾部开始提取:
array=( [0]=one [1]=two [2]=three [3]=four )
${array[@]:1} # two three four,除掉第一个元素后所有元素,那么${array[@]:0}表示所有元素
${array[@]:0:2} # one two
${array[@]:1:2} # two three

子串删除
[root@localhostdev]# echo ${array[@]:0}
one two three four
[root@localhostdev]# echo ${array[@]#t*e} # 左边开始最短的匹配:"t*e",这将匹配到"thre"
one two e four
[root@localhostdev]# echo ${array[@]##t*e} # 左边开始最长的匹配,这将匹配到"three"
[root@localhostdev]# array=( [0]=one [1]=two [2]=three [3]=four )
[root@localhostdev]# echo ${array[@] %o} # 从字符串的结尾开始最短的匹配
one tw three four
[root@localhostdev]# echo ${array[@] %%o} # 从字符串的结尾开始最长的匹配
one tw three four
子串替换
[root@localhostdev]# array=( [0]=one [1]=two [2]=three [3]=four )
第一个匹配到的,会被删除
[root@localhostdev]# echo ${array[@] /o/m}
mne twm three fmur

所有匹配到的,都会被删除
[root@localhostdev]# echo ${array[@] //o/m}
mne twm three fmur
没有指定替换子串,则删除匹配到的子符
[root@localhostdev]# echo ${array[@] //o/}
ne tw three fur
替换字符串前端子串
[root@localhostdev]# echo ${array[@] /#o/k}
kne two three four
替换字符串后端子串
[root@localhostdev]# echo ${array[@] /%o/k}
one twk three four

Ⅹ Linux:难道这个结果就是查看到的数组长度

bash是linux的一种命令形式,就像cmd,后面的4是错误代码,command not found是错误描述,意思是没有找到命令,这个命令不存在,你写的东西有问题,或者是环境变量设置有问题。

阅读全文

与linux数组长度相关的资料

热点内容
解压视频白头小哥 浏览:748
cadq命令 浏览:954
python连接本地数据库报错 浏览:194
手机模拟加密禁卡操作 浏览:105
电荷数怎么算法 浏览:589
cad如何打开命令行 浏览:150
php图片限制大小 浏览:164
程序员一夜未归 浏览:592
苹果xsmaxapp怎么不显示更新 浏览:600
苹果app怎么清除角标 浏览:483
解压屁屁玩具脏了怎么办 浏览:670
算法识别自动折叠 浏览:9
dos命令遍历文件 浏览:456
翻译整个pdf 浏览:198
怎么给解压软件授权 浏览:621
怎么换手机桌面壁纸安卓 浏览:957
pdf转换阅读器 浏览:344
特斯拉怎么app预约充电 浏览:498
安卓怎么录像更清晰 浏览:919
怎么服务器输入命令没有显示出来 浏览:799