[英]My c++ program is compiling fine, but not running . My program is running and stopping for a while. No error is showing
My c++ program is compiling with no error .我的 c++ 程序正在编译,没有错误。 I am running my program on vscode .我在vscode上运行我的程序。 In the same file, when i run this code.在同一个文件中,当我运行此代码时。
My system:我的系统:
#include<bits/stdc++.h>
using namespace std;
int main(){
count<<"Hello";
return 0;
}
It is running fine.它运行良好。
When in the same file, i put this code,当在同一个文件中时,我把这段代码,
#include<bits/stdc++.h>
using namespace std;
int removeAlt(string s);
int removeTwo(string s,char a,char b);
int alternate(string s);
int main(){
cout<<alternate("abaacdabd");
return 0;
}
int removeAlt(string s){
// remove consicutive characters
for(int i=0;i<s.size();){
if(s[i]==s[i+1]){
s.erase(i,1);
}
}
cout<<"Remove consitutive "<<s<<endl;
return s.size();
}
int removeTwo(string str,char a,char b){
for(int i=0;i<str.length();){
char ch=str[i];
if(str[i]==a||str[i]==b){
str.erase(i,1);
} else i++;
}
cout<<"removing "<<a<<" "<<b<<",, "<<str<<endl;
int res=removeAlt(str);
return res;
}
// Complete the alternate function below.
int alternate(string s) {
set<char>st;
// get different character of set
for(auto x:s)
st.insert(x);
int len=st.size(); // length of set
char setAr[len];
auto it=st.begin();
for(int i=0;i<len;i++){
setAr[i]=*it;
it++;
}
cout<<"Removing\n";
int up=len;
for(int i=0;i<up-1;i++){
for(int j=i+1;j<=up;j++){
cout<<"SetAr "<<i<<" "<<j<<endl;
len=max(0,removeTwo(s,setAr[i],setAr[j]));
}
}
return len;
}
It runs some statements, after that terminal pauses forever .它运行一些语句,之后该终端将永远暂停。
int removeAlt(string s){
// remove consicutive characters
for(int i=0;i<s.size();){
if(s[i]==s[i+1]){
s.erase(i,1);
}
}
cout<<"Remove consitutive "<<s<<endl;
return s.size();
}
You never increment i and so this function will run forever unless it erases everything.你永远不会增加 i ,所以这个 function 将永远运行,除非它删除所有内容。
int removeAlt(string s){
// remove consicutive characters
for(int i=0;i<s.size();){
if(s[i]==s[i+1]){
s.erase(i,1);
} else i++;
}
cout<<"Remove consitutive "<<s<<endl;
return s.size();
}
Increment i if next element is not same as current.如果下一个元素与当前元素不同,则增加 i。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.