评估大于string.size()的负数 [英] negative number evaluating greater than string.size()

查看:61
本文介绍了评估大于string.size()的负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是示例代码,演示了我遇到的问题。

将数字传递给函数时,我将其与字符串的大小进行比较,并且

然后采取某些行动,不幸的是在测试期间我发现负数被视为> 0.我

在mingw编译器/ dev c ++ / windows xp上编译了以下内容。


如果我将string.size()替换为普通数字,它的行为正如所料?我

通知string.size()返回大小类型而不是int但是我如何处理

那个?


预先感谢您的帮助


#include< iostream>

#include< stdlib.h>

#include < string>

使用命名空间std;


void something(int);


int main()

{

something(-1);

system(PAUSE);

返回0;

}


void something(int number){

string str =" sdfjksdflksdjfkjsklfjsklfj" ;;

if(number< str.size()){cout<< "你好" << ENDL; }

else {cout<< "伊克" << ENDL; }

}

解决方案

Jason写道:

下面是示例代码,演示我遇到的一个问题。
当一个数字传递给一个函数时,我将它与一个字符串的大小进行比较然后采取某些行动,不幸的是在测试期间我发现了负数被视为> 0.我在mingw编译器/ dev c ++ / windows xp上编译了以下内容。

如果我将string.size()替换为普通数字,它的行为与预期一致?我注意string.size()返回大小类型而不是int但是我该怎么处理呢

提前感谢您的帮助




GCC sez:


signed_probs.cpp:函数`void something(int)'':

signed_probs。 cpp:17:警告:签名和未签名之间的比较

整数表达式


将第17行更改为:


if(number< static_cast< int>(str.size())){cout<< "你好" << ENDL; }


解决了这个问题。




" Jason" < JA *********** @ btinternet.com>在消息中写道

news:c1 ********** @ titan.btinternet.com ...

下面是示例代码,演示了我遇到的问题。
当一个数字传递给一个函数时,我将它与一个字符串的大小进行比较然后采取某些行动,不幸的是在测试期间我发现了这个消极数字被视为> 0.
我在mingw编译器/ dev c ++ / windows xp上编译了以下内容。

如果我将string.size()替换为普通数字,它的行为与预期一致吗?
我注意到string.size()返回大小类型而不是int但是如何处理




建议你阅读最近线程''时间摆脱未签名?'',于17/02/04开始




john




" Gianni Mariani" < GI ******* @ mariani.ws>在消息中写道

news:c1 ******** @ dispatch.concentric.net ...

Jason写道:

下面是示例代码,它演示了遇到
的问题。将数字传递给函数时,我将其与字符串的大小
进行比较然后采取某些操作,不幸的是在测试过程中我发现负数被视为> ; 0.
我在mingw编译器/ dev c ++ / windows xp上编译了以下内容。

如果我将string.size()替换为普通数字,它的行为与预期一致吗?
我注意到string.size()返回大小类型而不是int但是如何处理
呢?

提前感谢您提供任何帮助



GCC sez:

signed_probs.cpp:在函数`void something(int)'':
signed_probs.cpp:17:警告:有符号和无符号之间的比较
整数表达式

将第17行更改为:

if(number< static_cast< int>(str.size())){cout<< "你好" << ENDL; }

解决了这个问题。




当然这是OP应该做的,但请注意解决方案

有效地排除了字符串,其中size()> INT_MAX。那么为什么要首先设计这样的

字符串呢?为什么没有size()返回一个int?

john


Hi, below is example code which demonstrates a problem I have encountered.
When passing a number to a function I compare it with a string''s size and
then take certain actions, unfortunately during the testing of it I
discovered that negative numbers were being treated as if they were > 0. I
compiled the following on mingw compiler/dev c++/windows xp.

If I replace string.size() for a ordinary number it behaves as expected? I
notice string.size() returns size type and not an int but how do I deal with
that?

Thanks in advance for any help

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

void something(int);

int main()
{
something(-1);
system("PAUSE");
return 0;
}

void something(int number) {
string str = "sdfjksdflksdjfkjsklfjsklfj";
if(number < str.size()) { cout << " hello" << endl; }
else { cout << " eek" << endl; }
}

解决方案

Jason wrote:

Hi, below is example code which demonstrates a problem I have encountered.
When passing a number to a function I compare it with a string''s size and
then take certain actions, unfortunately during the testing of it I
discovered that negative numbers were being treated as if they were > 0. I
compiled the following on mingw compiler/dev c++/windows xp.

If I replace string.size() for a ordinary number it behaves as expected? I
notice string.size() returns size type and not an int but how do I deal with
that?

Thanks in advance for any help



GCC sez:

signed_probs.cpp: In function `void something(int)'':
signed_probs.cpp:17: warning: comparison between signed and unsigned
integer expressions

Changing line 17 to :

if(number < static_cast<int>(str.size())) { cout << " hello" << endl; }

solves the problem.



"Jason" <ja***********@btinternet.com> wrote in message
news:c1**********@titan.btinternet.com...

Hi, below is example code which demonstrates a problem I have encountered.
When passing a number to a function I compare it with a string''s size and
then take certain actions, unfortunately during the testing of it I
discovered that negative numbers were being treated as if they were > 0. I compiled the following on mingw compiler/dev c++/windows xp.

If I replace string.size() for a ordinary number it behaves as expected? I notice string.size() returns size type and not an int but how do I deal with that?



Suggest you read the recent thread ''time to get rid of unsigned?'', started
on 17/02/04.

john



"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:c1********@dispatch.concentric.net...

Jason wrote:

Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string''s size and then take certain actions, unfortunately during the testing of it I
discovered that negative numbers were being treated as if they were > 0. I compiled the following on mingw compiler/dev c++/windows xp.

If I replace string.size() for a ordinary number it behaves as expected? I notice string.size() returns size type and not an int but how do I deal with that?

Thanks in advance for any help



GCC sez:

signed_probs.cpp: In function `void something(int)'':
signed_probs.cpp:17: warning: comparison between signed and unsigned
integer expressions

Changing line 17 to :

if(number < static_cast<int>(str.size())) { cout << " hello" << endl; }

solves the problem.



Of course this is what the OP should do, but notice that the solution
effectively rules out strings where size() > INT_MAX. So why design for such
strings in the first place? Why not have size() return an int?

john


这篇关于评估大于string.size()的负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆