C++ 我的代码如何对c++;?
所以请不要太苛刻,我会尽量解释我的问题 我有一个较长的代码,它接收0、1或n个参数作为输入。 这些输入应该是字符串,但是如果我像这样运行程序: /任务2^!;: 我只得到: bash:!…:未找到事件 如何防止此错误,并仅打印错误消息 这是task2.cppC++ 我的代码如何对c++;?,c++,C++,所以请不要太苛刻,我会尽量解释我的问题 我有一个较长的代码,它接收0、1或n个参数作为输入。 这些输入应该是字符串,但是如果我像这样运行程序: /任务2^!;: 我只得到: bash:!…:未找到事件 如何防止此错误,并仅打印错误消息 这是task2.cpp #include <iostream> #include <string> #include <algorithm> #include <vector> #include <array&
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <array>
#include <functional>
#include <math.h>
#include <stdint.h>
using namespace std;
/// Build suffix array from text.
/// Input is an emtpy suffix array and the text.
/// Output is a suffix array (sorted).
void construct(std::vector<uint32_t>& sa, const std::string& text)
{
if(text.empty()) {
cout << "Text is empty" << endl;
return;
}
if (sa.size()!=0) {
sa.clear();
}
for (uint32_t a = 0; a<text.length(); ++a) {
sa.push_back(a);
}
sort(sa.begin(),sa.end(),[&text] (uint32_t s1, uint32_t s2) {
return(text.substr(s1)<=text.substr(s2));
});
return;
}
//Wir brauchen kein $ am Ende unseres Strings, da wir keinen Suffixbaum aufbauen und wir somit nicht das Problem haben
//werden, dass ein Suffix ein Prefix eines aderen Suffix ist.
/// search for a 'query' string in a suffix array 'sa' build from 'text'.
/// Results are returned in 'hits'.
void findmlr(uint32_t& mlr, const uint32_t L, const uint32_t R, const std::string& query, const std::vector<uint32_t>& sa, const std::string& text) {
uint32_t mlrl = mlr;
uint32_t mlrr = mlr;
/* for L */
for(uint32_t l = mlr; l<query.size(); ++l) {
if (query[mlr] == text[sa[L]+mlrl]) {
mlrl+=1;
}
else{
break;
}
}
/* for R */
for(uint32_t r = mlr; r<query.size(); ++r) {
//cout << query[mlr] << text[sa[R]+mlrr] << endl;
if (query[mlr] == text[sa[R]+mlrr]) {
mlrr+=1;
}
else{
break;
}
}
mlr = min(mlrl,mlrr);
return;
}
void find(std::vector<uint32_t>& hits, const std::string& query, const std::vector<uint32_t>& sa, const std::string& text){
if(hits.size()!=0) {
hits.clear();
}
if (cin.fail()) {
cin.clear();
cout << "unexpected input" << endl;
}
if (text.size()==0) {
cout << "text is empty";
return;
}
else if (query.size()==0) {
cout << "query is empty";
return;
}
/* finding Lp */
uint32_t Lp;
if (query <= text.substr(sa[0])){
Lp = 0;
}
else if (query > text.substr(sa[text.length()-1])){
Lp = text.length();
}
else {
uint32_t L = 0;
uint32_t R = text.length()-1;
uint32_t mlr = 0;
while (R-L > 1) {
uint32_t M = ceil((L+R)/2);
findmlr(mlr,L,R,query,sa,text);
if (query.substr(mlr) <= (text.substr(sa[M]+mlr))) {
R = M;
}
else {
L = M;
}
}
Lp = R;
}
for (uint32_t i = Lp; i < text.length(); ++i) {
if ((text.substr(sa[i])).substr(0,query.length()) == query) {
if(find(hits.begin(), hits.end(), sa[i]) != hits.end()){
//do nothing
}
else{
hits.push_back(sa[i]);
}
}
else {
break;
}
}
sort(hits.begin(), hits.end());
return;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
///从文本生成后缀数组。
///输入是emtpy后缀数组和文本。
///输出为后缀数组(已排序)。
void构造(std::vector和sa,const std::string和text)
{
if(text.empty()){
cout“Bash event not found”完全在程序外部;GNU Bash有一个(mis)特性,它可以对命令行中的!
字符做出反应,即使该字符位于双引号中
$ echo "blah!blah!"
bash: !blah!: event not found
$ echo 'blah!blah!'
blah!blah!
请注意,在第一种情况下,echo
甚至没有被调用;Bash无法按此方式处理命令行,并且从未调用过echo
。如果您看到此消息,您的程序根本没有被调用,因此这种情况与您的程序如何处理任何类型的输入无关
您必须使用单引号或反斜杠来转义!
。Bash在这里试图做的是在命令历史记录中查找以blah
开头的命令,并将其替换。这是“历史扩展”
在Bash手册页面的历史扩展下对该功能进行了详细描述。一定要显示您的代码。如果代码很长,请显示相关部分。我有一个较长的代码
我们可以看到一些这样长的代码吗?否则,无法帮助您。欢迎使用StackOverflow。请复制、粘贴您的代码,以便我们能够提供帮助。我有包含代码。为什么我已经被否决了?您需要在命令行/task2“^!;:”中用引号括起参数
-bash试图将您键入的字符解释为它能理解的东西,非常感谢您。我们仍然被要求为打印意外输入的无效输入包含一个例外。现在我想不出其他地方来放置它,然后是您输入0个参数的情况。@DanielGießing Right;以此类推“未找到事件”,您的程序根本没有被调用。Bash无法按此方式处理命令行。因此,这种情况不会说明您的程序的任何行为。
$ echo "blah!blah!"
bash: !blah!: event not found
$ echo 'blah!blah!'
blah!blah!