问题描述
这是我的代码。我试图仅在用户输入“ [”符号时搜索。但是用户也可以输入普通单词,例如hello。
#include <iostream>
#include <iterator> // std::size()
int binsearch(int a[],int find,int l,int u) {
int n = u - l + 1;
if(n == 1) {
if(find == a[l])
return l;
else
return -1;
} else {
int mid = (l + u) / 2;
if(a[mid] == find)
return mid;
else if(a[mid] < find)
return binsearch(a,find,mid + 1,u);
else
return binsearch(a,l,mid - 1);
}
}
int main() {
int a[] = {1,54,76,89,123,145,198,230,345,654};
int l = 0,u = std::size(a) - 1,x;
std::cout << "Enter number which you want to find\n";
if(int find; std::cin >> find) {
x = binsearch(a,u);
if(x == -1)
std::cout << "Element is not found";
else
std::cout << "Element is present at " << x;
std::cout << '\n';
}
}
2020-08-23 17:22:36 5688 [Note] Plugin 'FEDERATED' is disabled.
2020-08-23 17:22:36 5688 [Note] InnoDB: Using atomics to ref count buffer pool pages
2020-08-23 17:22:36 5688 [Note] InnoDB: The InnoDB memory heap is disabled
2020-08-23 17:22:36 5688 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2020-08-23 17:22:36 5688 [Note] InnoDB: Memory barrier is not used
2020-08-23 17:22:36 5688 [Note] InnoDB: Compressed tables use zlib 1.2.3
2020-08-23 17:22:36 5688 [Note] InnoDB: Not using cpu crc32 instructions
2020-08-23 17:22:36 5688 [Note] InnoDB: Initializing buffer pool,size = 32.0M
2020-08-23 17:22:36 5688 [Note] InnoDB: Completed initialization of buffer pool
2020-08-23 17:22:37 5688 [Note] InnoDB: Highest supported file format is Barracuda.
2020-08-23 17:22:37 5688 [Note] InnoDB: The log sequence numbers 1771332 and 1771332 in ibdata files do not match the log sequence number 24084333 in the ib_logfiles!
2020-08-23 17:22:37 5688 [Note] InnoDB: Database was not shutdown normally!
2020-08-23 17:22:37 5688 [Note] InnoDB: Starting crash recovery.
2020-08-23 17:22:37 5688 [Note] InnoDB: Reading tablespace @R_119_4045@ion from the .ibd files...
2020-08-23 17:22:38 1640 InnoDB: Operating system error number 5 in a file operation.
InnoDB: The error means MysqLd does not have the access rights to
InnoDB: the directory. It may also be you have created a subdirectory
InnoDB: of the same name as a data file.
InnoDB: Error: Could not open single-table tablespace file table_a.ibd
InnoDB: We do not continue the crash recovery,because the table may become
InnoDB: corrupt if we cannot apply the log records in the InnoDB log to it.
InnoDB: To fix the problem and start MysqLd:
InnoDB: 1) If there is a permission problem in the file and MysqLd cannot
InnoDB: open the file,you should modify the permissions.
InnoDB: 2) If the table is not needed,or you can restore it from a backup,InnoDB: then you can remove the .ibd file,and InnoDB will do a normal
InnoDB: crash recovery and ignore that table.
InnoDB: 3) If the file system or the disk is broken,and you cannot remove
InnoDB: the .ibd file,you can set innodb_force_recovery > 0 in my.cnf
InnoDB: and force InnoDB to continue crash recovery here.
解决方法
您可以将source
callback
更新为使用response
,如下所示。获取lastTerm
并检查其是否以[
开头,然后仅返回过滤后的结果,否则返回空值,这将不会在自动完成中显示任何结果。
根据评论,它不适用于自动完成中的第二次选择,您需要添加focus
callback
,该内容与select
类似,几乎没有变化,因为它没有一行terms.push("");
。
source: function(request,response) {
let lastTerm = extractLast(request.term);
if (lastTerm.trim().startsWith('[')) {
// delegate back to autocomplete,but extract the last term
response($.ui.autocomplete.filter(availableTags,lastTerm));
} else {
response([]);
}
},focus: function(event,ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// terms.push(""); <-- comment this line from select
this.value = terms.join(",");
return false;
}
在下面尝试。
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
var availableTags = [
"[Hello]","[Hello World]","[Google","[New Life]","[World]","[Old]"
];
$("#tags").autocomplete({
source: function(request,response) {
let lastTerm = extractLast(request.term);
if (lastTerm.trim().startsWith('[')) {
// delegate back to autocomplete,but extract the last term
response($.ui.autocomplete.filter(availableTags,lastTerm));
} else {
response([]);
}
},ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// terms.push(""); <-- comment this line from select
this.value = terms.join(",");
return false;
},select: function(event,ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(",");
return false;
}
});
function edValueKeyPress() {}
<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js" jq=""></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
<div class="ui-widget">
<label for="tags">Search: </label>
<input type="text" id="tags" onkeypress="edValueKeyPress()" />
</div>