在阅读有关打印字符串的过程中,我通过一个声明" printf逐个写入字符,直到它遇到一个空字符.如果缺少空字符,则printf继续超过字符串的结尾,直到最终它在字符串中找到一个空字符.记忆".所以我写了一些代码:
情况1:
char arr[4] = { 'a', 'b', 'c' } ;
if (arr[3]== '\0')
printf ("%s",arr);
Run Code Online (Sandbox Code Playgroud)
输出是abc.
这是否意味着编译器已自动存储'\0'在arr[3].因为根据声明,printf只会在遇到时终止'\0'.
案例2:
char arr[3] = { 'a', 'b', 'c' } ;
if (arr[3]== '\0')
printf ("%s",arr);
Run Code Online (Sandbox Code Playgroud)
abc尽管没有数组块arr[3]存在,但输出又是 ,所以为什么不是错误呢?printf也已打印abc并停止,这意味着它必须遇到'\0'.那么这意味着编译器在arr[2]存储后会创建一个额外的数组块'\0'.如果这样那么数组大小必须增加到4个字节(每个字符类型字符为1个字节).但是执行语句printf ("%d",sizeof (arr));会给出输出3,表明数组大小没有增加,表明没有arr[3].那条件怎么if (arr[3]== '\0')变成了真的呢?
案例3:
char arr[3] = "abc";
if (arr[3]== '\0') …Run Code Online (Sandbox Code Playgroud) 通过插入函数“ insert(A,n)”将新元素插入堆中需要O(log n)时间(其中n是数组“ A”中的元素数)。插入函数如下:
void insert(int A[], int n)
{
int temp,i=n;
cout<<"Enter the element you want to insert";
cin>>A[n];
temp=A[n];
while(i>0 && temp>A[(i-1)/2])
{
A[i]=A[(i-1)/2];
i=(i-1)/2;
}
A[i]=temp;
}
Run Code Online (Sandbox Code Playgroud)
插入函数的时间复杂度为O(log n)。
将数组转换为堆数组的函数为:
void create_heap()
{
int A[50]={10,20,30,25,5,6,7};
//I have not taken input in array A from user for simplicity.
int i;
for(i=1;i<7;i++)
{
insert(A,i);
}
}
Run Code Online (Sandbox Code Playgroud)
假定该函数的时间复杂度为O(nlogn)。
->但是插入函数在每个调用中最多有一个要比较的元素“ i”。即,对于每个调用,循环的一次运行中的时间复杂度为O(log i)。
->因此,首先是log1,然后是log2,然后是log3,依此类推,直到log6。
->因此,对于数组的n个元素,总时间复杂度将为log2 + log3 + log4 + .... logn
->这将是log(2x3x4x ... xn)= log(n!)
那么为什么时间复杂度不是O(log(n!))而是O(nlogn)?
我的页面上有一个类型为“ file ”的输入。另外,我在页面上有一个按钮“添加输入”,它在页面上添加一个新的文件输入元素,当用户通过选择新文件更改任何这些输入的值时,我想运行一个函数。这是我的代码。
超文本标记语言
<div class="main">
<div id="InpDiv">
<input type="file" name="fname">
</div>
</div>
<div>
<button onclick="addInp()">Add Inputs</button>
</div>
Run Code Online (Sandbox Code Playgroud)
JS
// Function that is going to run after change in file input.
$(document).ready(function(){
$("input[name='fname']").each(function(){
$(this).change(function(){
alert('hello');
// Do some work here.
});
});
});
// Function that adds new file inputs.
function addInp()
{
$('#InpDiv').clone().appendTo(".main");
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是 .change() 方法对于页面上已经存在的那些输入工作正常,但对于那些新创建的输入中的值更改则不起作用(此处不会提示“hello”消息) 。我在页面上已经存在了多个文件输入,并且 .change() 适用于所有这些输入,但它不适用于任何新创建的输入。
事件是否无法与页面上动态添加的元素一起使用?如果是这样,那么我将如何完成这项工作?