矢量push_back和pop_back

时间:2018-11-23 23:38:52

标签: c++ vector

我正在做作业。我已经编码了分配,并且一切正常,除了我的输出缺少数字。我还附上了示例输出。在将数学表达式输入到字符串(例如35 * 4 - 6 / (9 + 3))中后,将删除数字。按正常顺序,这3个将丢失。相反,这9个将丢失。我不明白为什么会这样。

任何帮助,或者我希望提供一些指导,将不胜感激。我使用的push_back错误吗?代码如下:

#include<iostream>
#include<string>
#include<vector>
using namespace std;

vector<string> split(string);

vector<string> splitback(string);

int main()
{
vector<string>  vectorExpression;
string          expression;

cout << "Enter an expression :";
    getline(cin, expression);

vectorExpression = split(expression);

for (int i = 0; i < vectorExpression.size(); i++)
{
    cout << vectorExpression[i] << endl;
}
    cout << endl;

vectorExpression = splitback(expression);

for (int i = 0; i < vectorExpression.size(); i++)
{
    cout << vectorExpression[i] << endl;
}

system("pause");
return 0;
}

vector<string> split(string expression)
{
vector<string>  splitExpression;
string          digit = "",
                x = "";

for (int i = 0; i < expression.size(); i++)
{
    if (expression[i] >= '0' && expression[i] <= '9')
    {
        digit = digit + expression[i];  
    }
    else if (expression[i] != ' ')  
    {
        x = ""; 
        x = x + expression[i];  
        splitExpression.push_back(x);   
    }
    else
    {
        if (digit.size() > 0) 
        {
            splitExpression.push_back(digit);   
            digit = "";
        }
    }
}

if (digit.size() > 0) 
{
    splitExpression.push_back(digit);
}

return splitExpression;
}

vector<string> splitback(string expression)
{
vector<string>  splitBackExpression;
string          number = "",
                x = "";

for (int i = expression.size() - 1; i >= 0; i--)
{
    if (expression[i] >= '0' && expression[i] <= '9')
    {
        number = expression[i] + number;
    }
    else if (expression[i] != ' ')
    {
        x = "";
        x = x + expression[i];
        splitBackExpression.push_back(x);
    }

    if (number.size() > 0)
    {
        splitBackExpression.push_back(number);
        number = "";
    }
}
return splitBackExpression;
}

Problem Example Screenshot

2 个答案:

答案 0 :(得分:0)

您的split函数是错误的,您忘记添加数字,正确的将是

vector<string> split(string expression)
{
    vector<string>  splitExpression;
    string          digit = "",
                    x = "";

    for (int i = 0; i < expression.size(); i++)
    {
        if (expression[i] >= '0' && expression[i] <= '9')
        {
            digit = digit + expression[i];  
        }
        else  
        {
            // don't forget digit if there was one
            if (digit.size() > 0) 
            {
                splitExpression.push_back(digit);   
                digit = "";
            } 
            // also add what comes next if not a whitespace
            if (expression[i] != ' ')
            {
                x = expression[i];
                splitExpression.push_back(x);
            }
        }
    }

    if (digit.size() > 0) 
    {
        splitExpression.push_back(digit);
    }

    return splitExpression;
}

答案 1 :(得分:0)

这是我自己问题的最终答案,或者至少是我从这里得到帮助的解决方案。

#include<iostream>
#include<string>
#include<vector>
using namespace std;

vector<string> split(const string&);    

vector<string> splitback(const string&);    

int main()
{
    vector<string>  forwardExpression,
                    reverseExpression;
    string          expression;

    cout << "Enter an expression :";
        getline(cin, expression);

    //////////////////////////////////////////////////
    //         FORWARD DISPLAY OF EXPRESSION        //
    //////////////////////////////////////////////////
    forwardExpression = split(expression);

    cout << "'" << expression << "'" << " split into individual entities forwards         
    yields:" << endl;
        cout << endl;

    for (int i = 0; i < forwardExpression.size(); i++)
    {
        cout << forwardExpression[i] << endl;
    }
        cout << endl;

    //////////////////////////////////////////////////
    //        BACKWARD DISPLAY OF EXPRESSION        //
    //////////////////////////////////////////////////
    reverseExpression = splitback(expression);

    cout << "'" << expression << "'" << " split into individual entities     
    backwards yields:" << endl;
        cout << endl;

    for (int i = 0; i < reverseExpression.size(); i++)
    {
        cout << reverseExpression[i] << endl;
    }
        cout << endl;

    system("pause");
    return 0;
}

//////////////////////////////////////////////////
//        FORWARD FUNCTION OF EXPRESSION        //
//////////////////////////////////////////////////
vector<string> split(const string &expression)
{
    vector<string>  splitExpression;
    string          digit = "",
                    x = "";

    for (int i = 0; i < expression.size(); i++)     
    {
        if (expression[i] >= '0' && expression[i] <= '9')
        {
            digit = digit + expression[i];  
        }
        else
        {
            if (digit.size() > 0)   
            {
                splitExpression.push_back(digit);   
                digit = ""; 
            }           
            if (expression[i] != ' ')   
            {
                x = expression[i];  
                splitExpression.push_back(x);    
            }
        }
    }

    if (digit.size() > 0)   
    {
        splitExpression.push_back(digit);   
    }

    return splitExpression; 
}

//////////////////////////////////////////////////
//       BACKWARD FUNCTION OF EXPRESSION        //
//////////////////////////////////////////////////
vector<string> splitback(const string &expression)
{
    vector<string>  splitBackExpression;
    string          digit = "",
                    x = "";

    for (int i = expression.size() - 1; i >= 0; i--)
    {
        if (expression[i] >= '0' && expression[i] <= '9')   
        {
            digit = expression[i] + digit;  
        }
        else
        {
            if (digit.size() > 0)   
            {
                splitBackExpression.push_back(digit);   
                digit = ""; 
            }

            if (expression[i] != ' ')   
            {
                x = expression[i];  
                splitBackExpression.push_back(x);    
            }
        }
    }

    if (digit.size() > 0)   
    {
        splitBackExpression.push_back(digit);   
    }

    return splitBackExpression; 
}