| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1087 人关注过本帖
标题:求指教,求鞭笞,关于string::size_type;
只看楼主 加入收藏
信箱有效
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1102
专家分:4268
注 册:2012-6-19
收藏
得分:3 
以下是引用yaobao在2013-1-14 13:48:57的发言:

unsigned  int i;
string st("abcderty");


i=st.size();
//第一种,赋值
i=5;
if(i>st.size())
\\第二种,比较
i+=st.size();
//第三种,运算
那上面这三种都是错误的喽
注释应该在上面或者右边吧。。。。。。
2013-01-14 13:59
yaobao
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:4
帖 子:1854
专家分:4121
注 册:2012-10-25
收藏
得分:0 
明白了,谢谢T版,谢谢兄弟们。
我也想试啊,可以我现在C++的水平来说,即使测出来个结果我自己也不敢相信啊,测错的几率绝对大于对的

认认真真的学习,踏踏实实的走路:戒骄戒躁!!!
2013-01-14 14:04
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
好的编译器,会有足够的提醒信息,写的时候马上就看到指示的。

授人以渔,不授人以鱼。
2013-01-14 14:07
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:0 
以下是引用yaobao在2013-1-14 13:48:57的发言:

unsigned  int i;
string st("abcderty");


i=st.size();
//第一种,赋值
i=5;
if(i>st.size())
\\第二种,比较
i+=st.size();
//第三种,运算
那上面这三种都是错误的喽

第一种,会带有自动类型转换,如果i是int还会有warning说无符号转向有符号
第二种无可厚非
第三种,同第一种,自动类型转换

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-01-14 19:56
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:0 
以面向对象的观点来看,任何事物都是对象...
比如说java或者c#里面,int,double都是对象
int n其实是定义了一个类的对象
那么,再来看size_t
只要这个size_type类重载了大于小于,等于操作符,那么它就能进行相应的操作...

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-01-14 19:59
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:0 
好吧,今晚我特别的蛋疼...
看介绍
size_t. A basic unsigned integer C/C++ type. It is the type of the result returned by sizeof operator. The type's size is chosen so that it could store the maximum size of a theoretically possible array of any type. On a 32-bit system size_t will take 32 bits and on a 64-bit one - 64 bits. In other words, a pointer can be safely put inside size_t type (an exception is class-function-pointers but this is a special case). size_t type is usually used for loop, array indexing, size storage and address arithmetic. Although size_t can store a pointer, it is better to use another unsinged integer type uintptr_t for that purpose (its name reflects its capability). In some cases using size_t type is more effective and safe than using a more habitual for the programmer unsigned type.

size_t is a base unsigned integer memsize-type defined in the standard library of C/C++ languages. This type is described in the header file stddef.h for C and in the file cstddef for C++. Types defined by the header file stddef.h are located in the global namespace while cstddef places the size_t type in the namespace std. Since the standard header file stddef.h of the C language is included into C++ programs for the purpose of compatibility, in these programs you may address the type both in the global namespace (::size_t, size_t) and namespace std (std::size_t).

In the terms of static analyzer PVS-Studio, type size_t refers to memsize-types. The analyzer includes Viva64 system for detailed error detection in 64-bit programs and for code optimization. Many diagnostic messages shown by Viva64 analyzer relate to recommendations on using memsize-types. Using memsize-types (such as size_t, ptrdiff_t, INT_PTR) instead of 32-bit types in 64-bit programs allows you to:

    enable the compiler to build a simpler and consequently faster code which will have no unnecessary conversions of 32-bit and 64-bit data. It is especially useful when operating with address arithmetic and array indexing;
    avoid some errors when processing a large size of input data when the number of the elements being processed excesses the number UINT_MAX;
    avoid some other more specific errors;
    make the code more portable among 64-bit versions of Windows and Linux systems which use different data models. Thus, for example, for indexing large arrays in Linux systems you can use unsigned long type while in Windows it is impossible.

To learn more about the errors you can avoid when using size_t type and also how this type allows improving and optimizing your 64-bit programs, see the articles given in the references.

If you are planning to start developing 64-bit projects or porting the existing 32-bit projects on 64-bit systems, we would like to offer you purchasing PVS-Studio analyzer which will simplify this task greatly and allow you to avoid the long period of searching hidden errors.

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-01-14 20:31
yaobao
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:4
帖 子:1854
专家分:4121
注 册:2012-10-25
收藏
得分:0 
我和这些字母不熟啊,虽然经常见面但也只是点头之交..........

认认真真的学习,踏踏实实的走路:戒骄戒躁!!!
2013-01-15 08:06
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:0 
大概意思就是说,size_t是stl标准的一部分,主要是为了做代码跨平台移植用的...
可以安全的存放一个指针...
基本上就是个uint

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-01-15 09:55
ly1002
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-1-15
收藏
得分:0 
学习学习
2013-01-15 13:36
快速回复:求指教,求鞭笞,关于string::size_type;
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016594 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved