【问题标题】:Trying to convert a string into a int using stringstream尝试使用 stringstream 将字符串转换为 int
【发布时间】:2016-09-26 06:27:26
【问题描述】:

我正在尝试检查字符串表示是否等于给定的整数。我打算在函数中为此使用 stringstream。我也有一个operator=

我对如何一起执行这些操作以及我是否遗漏了一些东西感到有些困惑。这是我作业的最后一点,这只是我整个程序的一个小sn-p。我找不到很多这方面的指南,我感觉他们都将我引导到 atoi 或 atod,我不允许使用它们。

#ifndef INTEGER
#define INTEGER
using std::string;
class Integer
{
private:
    int intOne;
    string strOne;
public:
    Integer() {
        intOne = 0;
    }
    Integer(int y) {
        intOne = y;
    }
    Integer(string x) {
        strOne = x;
    }
    void equals(string a);  
    Integer &operator=(const string*);
    string toString();
};

#endif 

在此标头中,我不确定要为 = 运算符使用什么参数。

#include <iostream>
#include <sstream>
#include <string>
#include "Integer.h"
using namespace std;

Integer &Integer::operator=(const string*)
{
    this->equals(strOne);
    return *this;
}

void Integer::equals(string a)
{
    strOne = a;
    toString(strOne);
}

string Integer::toString()
{
    stringstream ss;
    ss << intOne;
    return ss.str();
}



 #include <iostream>
#include <cstdlib>
#include <conio.h>
#include <string>
#include <ostream>
using namespace std;
#include "Menu.h"
#include "Integer.h"
#include "Double.h"



int main()
{
    Integer i1;
    i1.equals("33");
    cout << i1;
}

对不起,如果这是一个不好的问题,我对这种类型的作业不太熟悉,我会寻求任何可以得到的帮助。谢谢。

【问题讨论】:

    标签: c++ operator-overloading stringstream


    【解决方案1】:

    您可以使用 std::to_strig() 将 int 转换为代表相同数字的字符串。

    【讨论】:

      【解决方案2】:

      所以如果我理解正确,你想重载运算符=,这是个坏主意,因为operator= 用于赋值而不是用于比较。

      正确的操作符签名是:

      ReturnType operator==(const TypeOne first, const TypeSecond second) [const] // if outside of class
      ReturnType operator==(const TypeSecond second) [const] // if inside class
      

      由于您无法将字符串与整数进行比较(它们是不同的类型),因此您需要编写比较函数,因为您没有比较函数,我会为您编写一个:

      bool is_int_equal_string(std::string str, int i)
      {
          std::string tmp;
          tmp << i;
          return tmp.str() == i;
      }
      

      最后但同样重要的是,您需要将两者合并为一个方便的运算符:

      // inside your Integer class
      bool operator==(std::string value) const
      {
          std::stringstream tmp;
          tmp << intOne;
          return tmp.str() == ref;
      }
      

      现在您可以像使用其他运算符一样使用此运算符了:

      Integer foo = 31;
      if (foo == "31")
          cout << "Is equal" << endl;
      else
          cout << "Is NOT equal" << endl;
      

      我希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        如果允许您使用std::to_string,那将是最好的。

        否则,您可以使用std::stringstream 创建一个函数来处理字符串和整数之间的相等性:

        例子:

        bool Integer::equal(const string& str)
        {
            stringstream ss(str);
            int str_to_int = 0;
            ss >> str_to_int;
        
            if (intOne == str_to_int)
                return true;
            else
                return false;
        }
        

        将此与if 语句结合起来:

        int main()
        {
            Integer i{100};
        
            if (i.equal("100"))
                cout << "true" << endl;
            else
                cout << "false" << endl;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-19
          • 1970-01-01
          • 2015-11-05
          • 1970-01-01
          相关资源
          最近更新 更多