在while循环中清除stringstream?

时间:2014-03-24 18:29:02

标签: c++

所以我一直在寻找如何将我的字符串向量转换为双向量的答案。每个人似乎都认为

ss.str(std::string());
ss.str();
ss.str("");

应该有效。虽然我已经尝试过了,但这是不行的。

我在使用stringstream之前已经完成了这个,但这次流中的缓存并没有清除,我的输出只是一堆连续连接的数字。

例如,我输入7 9 58 33.我得到:

7 79 7958 795833

这是在一个循环中,我假设是复杂的事情,但我不认为这应该是一个问题。这是代码。我所指的函数调用是real2double。问题出在函数的最后一个for循环中。

#include <iostream>
#include <String>
#include <vector>
#include <sstream>

using namespace std;

#ifndef Comp_Num
class Comp_Num
{
private:
    std::vector <double> real;
    std::vector <double> imag;
    double real_in, imag_in;
    string operation;
public:
    Comp_Num();
    Comp_Num(std::vector <string> complex_nums);
    static std::vector <string> get_input();
    static std::vector <string> get_real(std::vector <string> complexnum1);
    static std::vector <string> get_imag(std::vector <string> complexnum2);
    static void display(std::vector <string> display1);
    static void display2(std::vector <double> display3);
    static std::vector <string> set_compnum(std::vector <string> set_num);
    static std::vector <string> comp2real(std::vector <string> c2r);
    static vector <double> real2double (std::vector<string> r2d);
    ~Comp_Num();
/*
    double Comp_Num::operator+ (double add_num);
    double Comp_Num::operator- (double sub_num);
    double Comp_Num::operator* (double mul_num);
    double Comp_Num::operator/ (double div_num);
*/
};
#endif !Comp_Num

Comp_Num::Comp_Num(std::vector <string> complex_nums)
{


    /*  
    std::vector <string> input_fin;
    input_fin = Comp_Num::get_input();
    Comp_Num::display(real1);
    std::cout<<"What operation would you like to perform\n";
    std::cout<<"You can choose from addition, subtraction, multiplication, or division.\n";
    std::cin>>operation>>"\n";
    */  
};

std::vector <string> Comp_Num::set_compnum(std::vector <string> set_num)
{
    std::vector<string>comp1=Comp_Num::get_real(set_num);
    std::vector <string>comp2= Comp_Num::get_imag(set_num);
    std::vector <string>cmp2rl_fin = Comp_Num::comp2real(comp2);
    std::vector <double>finale = Comp_Num::real2double(cmp2rl_fin);
    Comp_Num::display(comp1);
    Comp_Num::display(comp2);
    Comp_Num::display(cmp2rl_fin);
    Comp_Num::display2(finale);
    system("pause");
    return(comp2);
};

std::vector <string> get_input()
{
    std::string usr_input, input1;
    std::vector <string> complex;
    std::cout<<"Enter the real or imaginary numbers you would like to perform your operation on.\n";
    std::cout<<"Enter abc for your last value.\n";

    std::getline(std::cin, usr_input);
    std::istringstream input(usr_input);
    while(input >> input1)
    {
        if(input1 == "abc")
            break;
        complex.push_back(input1);
    }
    std::cout<<'\n';
    return (complex);
};

std::vector <string> Comp_Num::get_imag(std::vector <string> complexnum2)
{
    std::vector <string> imag1;
    for(unsigned int i = 0; i < complexnum2.size(); ++i)
    {
        std::string str2 = "";
        std::vector<char> char1(complexnum2[i].begin(), complexnum2[i].end());
        for(unsigned int r = 0; r < char1.size(); ++r)
        {
            if(char1[r] >= '0' && char1[r] <= '9' || char1[r] == '-' || char1[r] == '.' || char1[r] == 'i')
            {
                str2 += char1[r];
                if(char1[r] == 'i')
                {
                    imag1.push_back(str2);  
                    continue;
                }
            }
        }
    }
    return(imag1);
};

std::vector <string> Comp_Num::get_real(std::vector <string> complexnum1)
{
    std::vector <string> real1;
    for(unsigned int i = 0; i < complexnum1.size(); ++i)
    {
        std::string str2 = "";
        std::vector<char> char1(complexnum1[i].begin(), complexnum1[i].end());
        for(unsigned int r = 0; r < char1.size(); ++r)
        {
            if(char1[r] >= '0' && char1[r] <= '9' || char1[r] == '-' || char1[r] == '.' || char1[r] == 'i')
            {
                str2 += char1[r];
                if(char1[r] == 'i')
                {
                    str2="";
                    break;
                }
            }
        }
        real1.push_back(str2);
    }
    return(real1);
};

void Comp_Num::display (std::vector <string> display1)
{
    unsigned int i = 0;
    while (i < display1.size())
    {
        std::cout<<display1[i]<<" ";
        i++;
    }
    std::cout<<std::endl;
};

void Comp_Num::display2 (std::vector <double> display3)
{
    unsigned int i = 0;
    while (i < display3.size())
    {
        std::cout<<display3[i]<<" ";
        i++;
    }
    std::cout<<std::endl;
};

std::vector <string> Comp_Num::comp2real(std::vector <string> c2r)
{
    std::vector <string> real2;
    for(unsigned int i = 0; i < c2r.size(); ++i)
    {
        std::string str2 = "";
        std::vector<char> char1(c2r[i].begin(), c2r[i].end());
        for(unsigned int r = 0; r < char1.size(); ++r)
        {
            if(char1[r] >= '0' && char1[r] <= '9' || char1[r] == '-' || char1[r] == '.')
            {
                str2 += char1[r];
            }
        }
        real2.push_back(str2);
    }
    return(real2);
}

std::vector<double> Comp_Num::real2double (std::vector<string> r2d)
{
    double y;
    std::vector<double> final_r2d;
    std::string str, str2;
    std::vector<string>str3;
    for(unsigned int i = 0; i < r2d.size(); ++i)
    {
        std::vector<char> ch(r2d[i].begin(), r2d[i].end());
        for(unsigned int r = 0; r < ch.size(); ++r)
        {
            if(ch[r] >= '0' && ch[r] <= '9' || ch[r] == '-' || ch[r] == '.')
            {
                str2 += ch[r];

            }
        }
        str3.push_back(str2);
    }
    for(unsigned int r = 0; r < str3.size(); r++)
    {
//      std::string open = "";
//      istringstream to_double(open);
        str = str3[r];
        std::istringstream to_double(str);
        while(to_double >> y)
        {
            final_r2d.push_back(y);
            to_double.str(std::string());
        }

    }
    return(final_r2d);
};

1 个答案:

答案 0 :(得分:1)

问题出在real2double的第一个循环中,你继续追加变量

str2 += ch[r];

您必须将声明移动到循环中:

for(unsigned int i = 0; i < r2d.size(); ++i)
{
  std::string str2;

<强>后来 废除代码重复:

template <class T>
static void display(std::vector <T> display);

template <class T>
void Comp_Num::display (std::vector <T> dis) {
  for( typename std::vector<T>::iterator it = dis.begin() ; it != dis.end(); ++it ){
    std::cout << *it << " ";
  }
  std::cout<<std::endl;
}

尽可能避免循环:

std::vector<double> Comp_Num::real2double (std::vector<string> r2d) {
  for( std::vector<string>::iterator it = r2d.begin() ; it != r2d.end(); ++it ){
    int iPos = (*it).find_first_not_of( "0123456789-." );
    double y;
    std::istringstream( (*it).substr( 0, iPos ) ) >> y;
    final_r2d.push_back( y );
  }
  return final_r2d;
}