C ++语法问题:如果var!= type int [英] C++ syntax question: if var != type int
问题描述
我想写一个if语句,但找不到正确的表达式使用。我想这样写:
if(var!= type(int))
但是,我不确定如何去做这个方法,这个方法不工作。
至少我沿着正确的方向思考?
重载函数:
void foo(int i)
{
// stuff
}
void foo(float f)
{
// stuff
}
int main(void)
$ {
int i = 10;
float f = 1.0f;
foo(i); // calls foo(int)
foo(f); // call foo(float)
}
如果你想要 int
- 特殊行为,然后在所有其他情况下,可以使用模板:
模板< typename T>
void foo(T t)
{
// T is something
}
template<>
void foo(int i)
{
// for int's only
}
int main(void)
{
int i = 10;
float f = 1.0f;
double d = 2.0;
foo(i); // calls specialized foo
foo(f); // calls generic foo
foo(d); // calls generic foo
}
根据你的评论(手头的任务是一个简单的程序:获取两个用户输入的整数并添加它们。将输入限制为整数,我可以在Python中做,如果num1!= type(int):print你没有输入整数,请输入一个整数else:continue),你想要这样的:
#include< iostream>
int main(void)
{
int i;
std :: cin>>一世;
if(std :: cin.fail())
{
std :: cout< 无效! << std :: endl;
}
else
{
// ...
}
}
这会通知诸如@#$,r13等无效输入,但不会捕获34fg $#%,因为它将读取 int
,并分别停在fg和$#%。
要检查,你必须读入一行输入,然后尝试将该行转换为所需的类型。 (感谢,litb )。这意味着您的问题更多喜欢这个问题 :
#include< iostream>
#include< sstream>
#include< string>
int main(void)
{
std :: string input;
std :: getline(std :: cin,input);
std :: stringstream ss(input);
int i;
ss>>一世;
if(ss.fail()||!(ss>> std :: ws).eof())
{
std :: cout< ; 无效! << std :: endl;
}
else
{
// ...
}
}
这样做:获取输入,并将其放入 stringstream
。然后解析 int
后,流出任何剩余的空白。之后,如果 eof
为false,这意味着有剩余字符;输入无效。
这更容易使用包装在函数中。在另一个问题,该演员被重新考虑了;在这个问题,我们使用cast,但包装输入与它。
#include< iostream>
#include< sstream>
#include< string>
bool parse_int(int& i)
{
std :: string input;
std :: getline(std :: cin,Input);
std :: stringstream ss(input);
ss>>一世;
return!(ss.fail()||!(ss>> std :: ws).eof());
}
int main(void)
{
int i;
if(!parse_int(i))
{
std :: cout< 无效! << std :: endl;
}
else
{
// ...
}
}
或更一般地:
#include< iostream>
$ p $
#include< sstream>
#include< string>
template< typename T>
bool parse_type(T& t)
{
std :: string input;
std :: getline(std :: cin,input);
std :: stringstream ss(input);
ss>> t;
return!(ss.fail()||!(ss>> std :: ws).eof());
}
int main(void)
{
int i;
if(!parse_type(i))
{
std :: cout< 无效! << std :: endl;
}
else
{
// ...
}
}
如果你没有例外,使用
lexical_cast
(来自boost或者faked,请参阅其他问题linked in-code [same如上面的链接]),你的代码看起来像这样:#include< iostream>
#include< sstream>
#include< string>
/ *来自问题的假的词法:
http://stackoverflow.com/questions/1243428/convert-string-to-int-with-bool-fail-in-c /
* /
template< typename T>
T lexical_cast(const std :: string& s)
{
std :: stringstream ss(s);
T结果;
if((ss>> result).fail()||!(ss>> std :: ws).eof())
{
throw std :: bad_cast(Bad cast。);
}
返回结果;
}
template< typename T>
T parse_type(void)
{
std :: string input;
std :: getline(std :: cin,input);
return lexical_cast< T>(input);
}
int main(void)
{
try
{
int i = parse_type< int&
float f = parse_type& float>();
}
catch(const std :: exception& e)
{
std :: cout< e.what()< std :: endl;
}
}
我不认为boost有一个不抛弃版本的词法转换,所以我们可以通过捕获
bad_cast
',使这个代码的真/假,而不是异常版本,如下所示。再次,这与boost
或自定义词法转换工作。 (任何执行词法转换并引发bad_cast
):include< iostream>
#include< sstream>
#include< string>
/ *来自问题的假的词法:
http://stackoverflow.com/questions/1243428/convert-string-to-int-with-bool-fail-in-c /
* /
template< typename T>
T lexical_cast(const std :: string& s)
{
std :: stringstream ss(s);
T结果;
if((ss>> result).fail()||!(ss>> std :: ws).eof())
{
throw std :: bad_cast(Bad cast。);
}
返回结果;
}
template< typename T>
bool parse_type(T& t)
{
std :: string input;
std :: getline(std :: cin,input);
try
{
t = lexical_cast< T>(input);
return true;
}
catch(const std :: bad_cast& e)
{
return false;
}
}
int main(void)
{
int i;
if(!parse_type(i))
{
std :: cout< 糟糕的演员。 << std :: endl;
}
}
现在回到
bool
结果,除了我们通过使用现有的lexical_cast
函数避免代码重复。
当然可以选择您要使用的方法。
I am trying to write an if statement but cannot find the proper expression form to use. I'm thinking of writing something like this:
if ( var != type(int) )
However, I am unsure exactly how to go about doing this, and this method does not work.
Am I at least thinking along the right lines?
解决方案It sounds like you're trying to overload a function:
void foo(int i) { // stuff } void foo(float f) { // stuff } int main(void) { int i = 10; float f = 1.0f; foo(i); // calls foo(int) foo(f); // calls foo(float) }
If you want
int
-special behavior and then something else in all other cases, you can use templates:template <typename T> void foo(T t) { // T is something } template <> void foo(int i) { // for int's only } int main(void) { int i = 10; float f = 1.0f; double d = 2.0; foo(i); // calls specialized foo foo(f); // calls generic foo foo(d); // calls generic foo }
According to your comment ("Task at hand is a simple program: Take two user inputted integers and add them. Restrict input to integer only. I can do it in Python and I am thinking too along those lines. if num1 != type(int): print "You did not enter an integer, please enter a integer." else: continue"), you want something like this:
#include <iostream> int main(void) { int i; std::cin >> i; if (std::cin.fail()) { std::cout << "Not valid!" << std::endl; } else { // ... } }
This will notify invalid input such as "@#$", "r13", but does not catch cases such as "34fg", "12$#%", because it will read the
int
, and stop at "fg" and "$#%", respectively.To check that, you will have to read in a line of input, and then try to convert that line into the type you want. (Thanks, litb). That means your question is more like this question:
#include <iostream> #include <sstream> #include <string> int main(void) { std::string input; std::getline(std::cin, input); std::stringstream ss(input); int i; ss >> i; if (ss.fail() || !(ss >> std::ws).eof()) { std::cout << "Not valid!" << std::endl; } else { // ... } }
This does the following: get input, and put it into a
stringstream
. Then after parsing theint
, stream out any remaining white space. After that, ifeof
is false, this means there are left-over characters; the input was invalid.This is much easier to use wrapped in a function. In the other question, the cast was re-factored away; in this question we're using the cast, but wrapping the input along with it.
#include <iostream> #include <sstream> #include <string> bool parse_int(int& i) { std::string input; std::getline(std::cin, input); std::stringstream ss(input); ss >> i; return !(ss.fail() || !(ss >> std::ws).eof()); } int main(void) { int i; if (!parse_int(i)) { std::cout << "Not valid!" << std::endl; } else { // ... } }
Or more generically:
#include <iostream> #include <sstream> #include <string> template <typename T> bool parse_type(T& t) { std::string input; std::getline(std::cin, input); std::stringstream ss(input); ss >> t; return !(ss.fail() || !(ss >> std::ws).eof()); } int main(void) { int i; if (!parse_type(i)) { std::cout << "Not valid!" << std::endl; } else { // ... } }
This let's you parse other types with error checking.
If you're okay with exceptions, using
lexical_cast
(either from boost, or "faked", see the other question linked in-code [same as above link]), your code would look something like this:#include <iostream> #include <sstream> #include <string> /* Faked lexical-cast from question: http://stackoverflow.com/questions/1243428/convert-string-to-int-with-bool-fail-in-c/ */ template <typename T> T lexical_cast(const std::string& s) { std::stringstream ss(s); T result; if ((ss >> result).fail() || !(ss >> std::ws).eof()) { throw std::bad_cast("Bad cast."); } return result; } template <typename T> T parse_type(void) { std::string input; std::getline(std::cin, input); return lexical_cast<T>(input); } int main(void) { try { int i = parse_type<int>(); float f = parse_type<float>(); } catch (const std::exception& e) { std::cout << e.what() << std::endl; } }
I don't think boost has a no-throw version of lexical cast, so we can make a true/false rather than exception version of this code by catching
bad_cast
's, as follows. Once again, this works with eitherboost
or a custom lexical cast. (Anything that does a lexical cast and throwsbad_cast
):#include <iostream> #include <sstream> #include <string> /* Faked lexical-cast from question: http://stackoverflow.com/questions/1243428/convert-string-to-int-with-bool-fail-in-c/ */ template <typename T> T lexical_cast(const std::string& s) { std::stringstream ss(s); T result; if ((ss >> result).fail() || !(ss >> std::ws).eof()) { throw std::bad_cast("Bad cast."); } return result; } template <typename T> bool parse_type(T& t) { std::string input; std::getline(std::cin, input); try { t = lexical_cast<T>(input); return true; } catch (const std::bad_cast& e) { return false; } } int main(void) { int i; if (!parse_type(i)) { std::cout << "Bad cast." << std::endl; } }
Now it's back to a
bool
result, except we avoid code duplication by using existinglexical_cast
functions.You can of course choose which method you would like to use.
这篇关于C ++语法问题:如果var!= type int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!