简体   繁体   English

尝试使用stringstream将字符串转换为int

[英]Trying to convert a string into a int using stringstream

I'm trying check if a string representation equals given integer. 我正在尝试检查字符串表示是否等于给定的整数。 I'm meant to use stringstream for this in a function. 我打算在函数中使用stringstream。 I also have an operator= for this as well. 我也有一个operator=这个。

I'm a little confused on how to execute these together and if I'm missing something. 我有点困惑如何一起执行这些,如果我错过了什么。 This is the last bit to an assignment I have, this is just a small snippet of my whole program. 这是我的作业的最后一点,这只是我整个程序的一小部分。 I can't find many guides on this, and I sense they all direct me to atoi or atod, which I'm not allowed to use. 我找不到很多这方面的指南,我觉得他们都指引我到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 

In this header I'm not sure what argument I'm to use for the = operator. 在这个标题中,我不确定我将用于=运算符的参数。

#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;
}

Sorry if its a bad question I'm not too familiar with this type of assignment and will take any help I can get. 对不起,如果这是一个糟糕的问题,我不太熟悉这种类型的任务,并会接受我能得到的任何帮助。 Thanks. 谢谢。

你可以使用std::to_strig() ,它允许你从int转换为代表相同数字的字符串。

So if i understand correctly, you want to overload operator = , and that is a bad idea, since operator= is used for assignment not for comparison. 所以,如果我理解正确,你想重载operator = ,这是一个坏主意,因为operator=用于赋值而不是用于比较。

The correct operator signature is: 正确的操作员签名是:

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

Since you can't compare string to integer (they are different types), you need to write your comparisment function, since you don't have one i will write one for you: 由于你无法比较字符串与整数(它们是不同的类型),你需要编写你的比较函数,因为你没有,我会为你写一个:

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

Last but not least, you need to merge both of those, into one convenient operator: 最后但并非最不重要的是,您需要将这两者合并到一个方便的运算符中:

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

Now you can use this operator, just like any other: 现在您可以像使用其他任何操作符一样使用此运算符:

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

I hope this helps. 我希望这有帮助。

If you are allowed to use std::to_string then it would be the best. 如果你被允许使用std::to_string那么它将是最好的。

Otherwise, you could create a function to handle the equality between the string and the integer with the use of std::stringstream : 否则,您可以使用std::stringstream创建一个函数来处理字符串和整数之间的相等性:

Example: 例:

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;
}

Combine this with an if statement: 将其与if语句结合使用:

int main()
{
    Integer i{100};

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM