size_t和int比较 [英] size_t and int comparison

查看:555
本文介绍了size_t和int比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for(int i = 0; i< strlen(pathcmd); i ++){//此行引起警告


警告C4018:''<'':签名/未签名不匹配


strlen返回一些类型''size_t''。 size_t是一个无符号类型,

你将它与int,一个签名类型进行比较。


两个删除警告的解决方案:


1.将变量''i'的类型更改为''size_t''。

2. staic_cast i tounsigned类型。


哪种方式在C ++中更好?

解决方案



"吊环" < TI ****** @ hotmail.com>在消息中写道

新闻:%R ***************** @ bgtnsc04-news.ops.worldnet.att.net ...

for(int i = 0; i< strlen(pathcmd); i ++){//此行引发警告

警告C4018:''<'':signed / unsigned mismatch

strlen返回一些类型''size_t''。 size_t是一个无符号类型,并且你将它与int,一个签名类型进行比较。

删除警告的两个解决方案:

1.更改类型变量''i''''size_t''。
2. staic_cast i tounsigned类型。

哪种方式在C ++中更好?


更改''我''键入''size_t''。


或者更好的是,替换你的char数组''pathcmd' '

带有''std :: string''对象,并写道:


std :: string pathcmd(" whatever");

for(std :: string :: size_type i = 0; i< pathcmd.size(); ++ i)

/ * etc * /


(如果你的循环中的代码没有修改字符串,你可以通过在循环之前存储

大小来获得性能略有提升并使用它:


std :: vector :: size_type sz(pathcmd.size());

for(std :: vector :: size_type i = 0; i< sz; ++ i)

/ * etc * /


-Mike


" tings"< ti ****** @ hotmail.com>写了...

for(int i = 0; i < strlen(pathcmd); i ++){//此行引发警告

警告C4018:''<'':签名/未签名错误h

strlen返回一些类型''size_t''。 size_t是一个无符号类型,并且你将它与int,一个签名类型进行比较。

删除警告的两个解决方案:

1.更改类型变量''i''''size_t''。
2. staic_cast i tounsigned类型。


3.忽略警告。

哪种方式在C ++中更好?




它取决于以后如何使用我。我自己更喜欢#3。


Victor


" Mike Wahler" < MK ****** @ mkwahler.net>写了...

[..]
(如果你的循环中的代码没有修改字符串,你可以通过存储
循环前的大小并使用它:

std :: vector :: size_type sz(pathcmd.size());
for(std :: vector :: size_type i = 0;我< sz; ++ i)
/ * etc * /




我个人不喜欢用不必要的名字来污染范围,

所以我会为(std :: string :: size_type sz = pathcmd.size(),i = 0; i< sz; i ++){写'




...


但它通常无关紧要。


V


for (int i=0;i < strlen(pathcmd);i++){//this line cause a warning

warning C4018: ''<'' : signed/unsigned mismatch

strlen returns a number of type ''size_t''. size_t is an unsigned type and
you are comparing it to an int, a signed type.

Two solutions to remove the warning:

1. Change the type of the variable ''i'' to ''size_t''.
2. staic_cast i to "unsigned" type.

Which way is better in C++?

解决方案


"tings" <ti******@hotmail.com> wrote in message
news:%R*****************@bgtnsc04-news.ops.worldnet.att.net...

for (int i=0;i < strlen(pathcmd);i++){//this line cause a warning

warning C4018: ''<'' : signed/unsigned mismatch

strlen returns a number of type ''size_t''. size_t is an unsigned type and
you are comparing it to an int, a signed type.

Two solutions to remove the warning:

1. Change the type of the variable ''i'' to ''size_t''.
2. staic_cast i to "unsigned" type.

Which way is better in C++?
Change ''i'' to type ''size_t''.

Or better yet, replace your char array ''pathcmd''
with a ''std::string'' object, and write:

std::string pathcmd("whatever");
for(std::string::size_type i = 0; i < pathcmd.size(); ++i)
/* etc */

(if the code in your loop does not modify the string, you
might get a slight performance improvement by storing the
size before the loop and using that:

std::vector::size_type sz(pathcmd.size());
for(std::vector::size_type i = 0; i < sz; ++i)
/* etc */

-Mike



"tings" <ti******@hotmail.com> wrote...

for (int i=0;i < strlen(pathcmd);i++){//this line cause a warning

warning C4018: ''<'' : signed/unsigned mismatch

strlen returns a number of type ''size_t''. size_t is an unsigned type and
you are comparing it to an int, a signed type.

Two solutions to remove the warning:

1. Change the type of the variable ''i'' to ''size_t''.
2. staic_cast i to "unsigned" type.
3. Ignore the warning.
Which way is better in C++?



It depends on how ''i'' is used later. I prefer #3 myself.

Victor


"Mike Wahler" <mk******@mkwahler.net> wrote...

[..]
(if the code in your loop does not modify the string, you
might get a slight performance improvement by storing the
size before the loop and using that:

std::vector::size_type sz(pathcmd.size());
for(std::vector::size_type i = 0; i < sz; ++i)
/* etc */



I personally prefer not to pollute scopes with unnecessary names,
so I''d write

for (std::string::size_type sz = pathcmd.size(), i = 0; i < sz; i++) {
...

But it often doesn''t matter, probably.

V


这篇关于size_t和int比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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