注册 登录
编程论坛 C++教室

求指教,求鞭笞,关于string::size_type;

yaobao 发布于 2013-01-14 10:34, 1087 次点击
int main(){
    string st("abcdefghijklmn");
      if(st.size()==0)
       .......
}
这是我在书上看到的一段,我很疑惑size()成员函数的返回值是string::size_type,这个应该是类类型,怎么和int的0进行比较呢。
求指教啊。。。。。
28 回复
#2
rjsp2013-01-14 10:44
这个应该是类类型
--- 未必吧,一般而言应该是size_t类型。
即使是自定义类型,也可以重载其与size_t类型的比较

例外话,if(st.size()==0) 不如用 if( st.empty() ) 好
#3
wp2319572013-01-14 10:46
你这个string是自定义类吧  
#4
yaobao2013-01-14 10:53
[attach]68046[/attach][attach]68047[/attach]
只有本站会员才能查看附件,请 登录

string是库类型啊,书上明确说了size()返回的是size_type怎么还和0进行比较

[ 本帖最后由 yaobao 于 2013-1-14 10:57 编辑 ]
#5
yaobao2013-01-14 10:57
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
#6
wp2319572013-01-14 11:11
看源码吧
光从字面看不出啥来着 比如:
程序代码:
#include "afx.h"
typedef unsigned int wp231957;
int _tmain(int argc, _TCHAR* argv[])
{
    wp231957 test;
    test=123456;
    printf("%d",test);
    return 0;
}

#7
yaobao2013-01-14 11:20
以下是引用wp231957在2013-1-14 11:11:32的发言:

看源码吧
光从字面看不出啥来着 比如:#include "afx.h"
typedef unsigned int wp231957;
int _tmain(int argc, _TCHAR* argv[])
{
    wp231957 test;
    test=123456;
    printf("%d",test);
    return 0;
}
万版,没明白你的意思啊
#8
wp2319572013-01-14 11:26
wp231957 test;
我的意思是 光看这一句代码  你能知道test是一个啥类型吗
#9
rjsp2013-01-14 12:17
string是库类型啊,书上明确说了size()返回的是size_type怎么还和0进行比较
------ 为什么不能呀,不理解
#10
锋了2013-01-14 12:41
学习学习
#11
peach54602013-01-14 12:41
size_type可以简单理解成uint
#12
yaobao2013-01-14 12:50
在,在网上又找了半天,也没明白啊
#13
TonyDeng2013-01-14 12:57
回复 楼主 yaobao
string::size_type确实是类类型,但它与unsigned int兼容,可以比较。
#14
yaobao2013-01-14 13:05
以下是引用TonyDeng在2013-1-14 12:57:12的发言:

string::size_type确实是类类型,但它与unsigned int兼容,可以比较。
困扰我3个小时的问题竟然是因为兼容问题,苦啊!!
可是T版,这个兼容是谁兼容谁啊,是string::size_type兼容unsigned int  还是unsigned int 兼容string::size_type,或者是相互兼容;再有就是这个兼容是因为string::size_type是unsigned int的define还是别的原因啊
#15
TonyDeng2013-01-14 13:16
size_type是在类中定义的类型,可以视为define,但其实没那么简单,不过可以简单地这样看。之所以要在类中定义,是因为这个东西需要随着类的实际情况变化而变化,这个计数器可能是极小的数,也可能是极大的数,简单地一个unsigned int可能容纳不下,所以它会随着定义的类的可能性而变化,不过不管它是unsigned int还是unsigned long甚至更大,都必然与unsigned类整数兼容,与零比较是没有问题。
#16
TonyDeng2013-01-14 13:21
你自己4楼贴的东西就叙述得很清楚了
#17
yaobao2013-01-14 13:31
以下是引用TonyDeng在2013-1-14 13:16:28的发言:

size_type是在类中定义的类型,可以视为define,但其实没那么简单,不过可以简单地这样看。之所以要在类中定义,是因为这个东西需要随着类的实际情况变化而变化,这个计数器可能是极小的数,也可能是极大的数,简单地一个unsigned int可能容纳不下,所以它会随着定义的类的可能性而变化,不过不管它是unsigned int还是unsigned long甚至更大,都必然与unsigned类整数兼容,与零比较是没有问题。
我这样理解不知道对不对:之所以if(s.size()==0)这样的操作正确是因为无符号数字0的特殊位模式造成的,因为不管是考虑大小端还是字节数,无符号的0都是所有位上都是0,所以可以不考虑s.size()的返回值size_type到底是unsigned in 还是unsigned long 或是其他别的什么类型而直接比较,如果将0换成别的数是不可以的
说来惭愧,C++刚学,想自己写段代码来验证这样理解对不对都很难办到,即使有了结果自己也不敢相信是对的。
求指教
#18
TonyDeng2013-01-14 13:37
对的。类定义之所以把size_type定义为static,让外部可以使用string::size_type类型,就是让你直接使用,故不要把这个值赋值给int类型,那样编译器可能会报类型不兼容(熟悉C的人可能会通过强制转换操作,但C++很反对这种做法)。在编程时,外部定义要接收这个数据的变量,直接使用相应类的::size_type是最好的。
#19
yaobao2013-01-14 13:48
unsigned  int i;
string st("abcderty");


i=st.size();
//第一种,赋值
i=5;
if(i>st.size())
\\第二种,比较
i+=st.size();
//第三种,运算
那上面这三种都是错误的喽
#20
TonyDeng2013-01-14 13:52
是的,你上机测试一下就知道了么。
#21
信箱有效2013-01-14 13:59
以下是引用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();
//第三种,运算
那上面这三种都是错误的喽
注释应该在上面或者右边吧。。。。。。
#22
yaobao2013-01-14 14:04
明白了,谢谢T版,谢谢兄弟们。
我也想试啊,可以我现在C++的水平来说,即使测出来个结果我自己也不敢相信啊,测错的几率绝对大于对的
#23
TonyDeng2013-01-14 14:07
好的编译器,会有足够的提醒信息,写的时候马上就看到指示的。
#24
peach54602013-01-14 19:56
以下是引用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说无符号转向有符号
第二种无可厚非
第三种,同第一种,自动类型转换
#25
peach54602013-01-14 19:59
以面向对象的观点来看,任何事物都是对象...
比如说java或者c#里面,int,double都是对象
int n其实是定义了一个类的对象
那么,再来看size_t
只要这个size_type类重载了大于小于,等于操作符,那么它就能进行相应的操作...
#26
peach54602013-01-14 20:31
好吧,今晚我特别的蛋疼...
看介绍
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.
#27
yaobao2013-01-15 08:06
我和这些字母不熟啊,虽然经常见面但也只是点头之交..........
#28
peach54602013-01-15 09:55
大概意思就是说,size_t是stl标准的一部分,主要是为了做代码跨平台移植用的...
可以安全的存放一个指针...
基本上就是个uint
#29
ly10022013-01-15 13:36
学习学习
1