一个C++声明语句求解释

keloli 2014-10-02 02:57:38
声明语句如下,请这句话是什么意思?


typedef std::string::size_type str_size;
...全文
359 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
凡人_浪子 2014-10-08
  • 打赏
  • 举报
回复
这个应该叫做“特征”技术,读者去看一看《C++编程思想》第二卷中“深入理解模板”一章中“模板编程中的习语”一节,看不懂的话,还是从基本看起吧! size_type其实就是int类型
强脚的花 2014-10-08
  • 打赏
  • 举报
回复
定义一个 str_size 是std::string::size_type种类型 哈哈
keloli 2014-10-06
  • 打赏
  • 举报
回复
引用 12 楼 zhao4zhong1 的回复:
msdn•••
这。。。好吧
赵4老师 2014-10-06
  • 打赏
  • 举报
回复
msdn•••
keloli 2014-10-05
  • 打赏
  • 举报
回复
引用 9 楼 zhao4zhong1 的回复:
typedef typedef type-declaration synonym; The typedef keyword defines a synonym for the specified type-declaration. The identifier in the type-declaration becomes another name for the type, instead of naming an instance of the type. You cannot use the typedef specifier inside a function definition. A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the decl-specifiers portion of the declaration. In contrast to the class, struct, union, and enum declarations, typedef declarations do not introduce new types — they introduce new names for existing types. Example // Example of the typedef keyword typedef unsigned long ulong; ulong ul; // Equivalent to "unsigned long ul;" typedef struct mystructtag { int i; float f; char c; } mystruct; mystruct ms; // Equivalent to "struct mystructtag ms;" typedef int (*funcptr)(); // funcptr is synonym for "pointer // to function returning int" funcptr table[10]; // Equivalent to "int (*table[10])();"
请问赵老师,这么标准的解释可以在哪里查询到呢?
keloli 2014-10-03
  • 打赏
  • 举报
回复
引用 6 楼 u013163178 的回复:
就是给标准库里面的string类的size成员取一个别名叫做str_size
不是给size_type取个别名么?
keloli 2014-10-03
  • 打赏
  • 举报
回复
引用 3 楼 FrankHB1989 的回复:
std::string是类模板std::basic_string的实例,是一个类,size_type是类string的成员。还有疑问么。
清晰易懂,大叔叔谢啦~~
赵4老师 2014-10-03
  • 打赏
  • 举报
回复
typedef typedef type-declaration synonym; The typedef keyword defines a synonym for the specified type-declaration. The identifier in the type-declaration becomes another name for the type, instead of naming an instance of the type. You cannot use the typedef specifier inside a function definition. A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the decl-specifiers portion of the declaration. In contrast to the class, struct, union, and enum declarations, typedef declarations do not introduce new types — they introduce new names for existing types. Example // Example of the typedef keyword typedef unsigned long ulong; ulong ul; // Equivalent to "unsigned long ul;" typedef struct mystructtag { int i; float f; char c; } mystruct; mystruct ms; // Equivalent to "struct mystructtag ms;" typedef int (*funcptr)(); // funcptr is synonym for "pointer // to function returning int" funcptr table[10]; // Equivalent to "int (*table[10])();"
li4c 2014-10-02
  • 打赏
  • 举报
回复
就是给标准库里面的string类的size成员取一个别名叫做str_size
aa5566f4 2014-10-02
  • 打赏
  • 举报
回复
typedef 作用是為一種資料類型定義一個新名字。這裡的資料類型包括內部數據類型(int,char等)和自定義的資料類型(struct等)。最簡單的一種用法就是用來替型別取另一個名字。它的語法如下 typedef 資料型別 自訂名稱; 舉個實例來說如果我們要訂一個叫 height 的型別,我們就可以這樣寫: typedef float height; 然後我們就可以在程式中,加上自已的型別,如: height *y = 3.88; NSLog(@"height=%.2f",y); 程式就會輸出【height=3.88】。 這樣作的目的,主要是為了讓程式容易思考與閱讀。如果只是單純的一個取代,似乎還看不出來,但如果較複雜的結構就很明顯了。 舉個實例來說,我們可以設計一個結構叫距型 theRect , 內有兩個參數,第一個參數是位置,第二個參數是大小。位置以 x,y 座標表示,大小以 width, height 表示,那我們就可以設計這樣一個結構: typedef struct { // 訂義位置 thePoint,其表示方法為 x,y 兩個浮點數來指示座標 float x; float y; }thePoint; typedef struct { // 訂義大小 theSize,其表示方法為 width,height 兩個浮點數 float width; float height; } theSize; typedef struct { // 訂義大小 theRect, 內有起點座標位置 origin(thePoint類型) 與大小 size(theSize類型) thePoint origin; theSize size; } theRect; 定義好型別之後,我們就可以建立一個型別,然後對它做設定,如 theRect myRect; myRect.origin.x = 0; // myRect 的起始位置 y 座標為 0 myRect.origin.y = 0; // myRect 的起始位置 y 座標為 0 myRect.size.width = 50; // myRect 的大小,寬為 50 myRect.size.height = 60; // myRect 的大小,高為 60 NSLog(@"x:%.1f y:%.1f width:%.1f height:%.1f",myRect.origin.x,myRect.origin.y,myRect.size.width,myRect.size.height); 輸出:x:0.0 y:0.0 width:50.0 height:60.0 這樣子用 myRect.size.width 讀起來就像是 距形 . 大小 . 寬度 是不是很好閱讀呢? 在 iOS 程式設計中,有非常大量的使用自訂的型別訂義,像是 UIColor, CGPoint,CGSize 等資料型別,多半是這種概念建立的。
The_Third_Wave 2014-10-02
  • 打赏
  • 举报
回复
你该查::是干什么的
FrankHB1989 2014-10-02
  • 打赏
  • 举报
回复
std::string是类模板std::basic_string的实例,是一个类,size_type是类string的成员。还有疑问么。
keloli 2014-10-02
  • 打赏
  • 举报
回复
引用 1 楼 u013171165 的回复:
std::string::size_type起个名字叫str_size,楼主该翻翻书,查下typedef是干什么的,什么都问不如不学啊!
我是不太理解“std::string::size_type”的作用域符号怎么起作用的。。。刚自学C++还望包涵
The_Third_Wave 2014-10-02
  • 打赏
  • 举报
回复
std::string::size_type起个名字叫str_size,楼主该翻翻书,查下typedef是干什么的,什么都问不如不学啊!

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧