首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在字符之后插入空格函数在代码外部有效,但在C++中出现内部返回错误

在字符之后插入空格函数在代码外部有效,但在C++中出现内部返回错误
EN

Stack Overflow用户
提问于 2018-06-11 23:58:59
回答 1查看 67关注 0票数 -1

我做了一个在特定字符后插入空格的函数,它适用于任何长度的字符。就像现在

代码语言:javascript
复制
using namespace std;
insert_spaces_before_delims(string a)
{
 vector<int> found;
 int temp;
 int i=0;
//string a="Ahmeed+Khaled+awwad=Ahmedd-AWWAd";                //string be a parameter
 temp = a.find_first_of("+-=");                              // chars be parameters if need to change
 found.push_back(temp);
 a.insert(found[i]," ");
 while(a.find_first_of("+-=",found[i]+2)!= string::npos)
 {
  temp = a.find_first_of("+-=",found[i]+2);
  found.push_back(temp);
  a.insert(found[i+1]," ");
  i++;
 }
}
int main(void)
{
 string equation;
 getline(cin,equation);
 insert_spaces_before_delims(equation);
 //the output is the string with spaces before every +,- and = 
}

当返回错误时,我调试函数"terminate called this an instance of 'std::out_of_range‘what():basic_string::replace:__pos ( is 4294967295) > this->size() (What is 0)“

通过条件(==string::npos)为什么它再次出现在代码中并停止代码继续,只需返回错误并结束程序。

代码语言:javascript
复制
struct variable_content
{
//string coefficient;                 // coefficient of x ( value before x)
    int value;                           // value of X = coefficient but in another type
    string order;                       // order (number ) of x (number after x)
};
spilit__each_string(string,char,variable_content,vector<string>,vector<variable_content>);
spliting_each_variable(char ,variable_content ,vector<string> ,vector<variable_content>);
insert_spaces_before_delims(string );

int main()
{
  int number_equations;
   string equation;                             //receive string
   vector<string> equations;                    // vector for equations input from user
   vector<string> variables;                    // initial empty vector of string for each var as strings at all
   vector<variable_content> variable;           //initial empty vector of struct for each var
   char delim[] = " ";
   char delim1[]="xX";
   cin>>number_equations;
   number_equations++;
   variable_content temp;
   for (int i=0; i<number_equations;i++)
   {
     getline(cin,equation);                             //worked

//\*\*\*the problem function , if i commented it the code works fine\*\*\*\* insert\_spaces\_before\_delims(equation); //return error and stop the code equations.push\_back(equation); //worked //make a temp struct and put tok1 in coefficient of it spilit\_\_each\_string(equation, delim,temp,variables,variable);//worked spliting\_each\_variable(delim1,temp ,variables,variable); //worked } //printing vector of struct of every variable for(int y=0; y<variable.size();y++) { cout<<variable[y].value<<"\t"<<variable[y].order<<endl; } }

整个代码在github.https://github.com/AhmedKAwwad/Split-String-into-vector/blob/master/main.cpp上的重要部分和链接

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-13 06:38:13

https://ericlippert.com/2014/03/05/how-to-debug-small-programs/这个链接让我调试我的代码,发现在我的代码中混合了cin>>和getline会出错,然后我使用getline,然后将字符串转换为整数谢谢评论

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50801776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档