社区
C语言
帖子详情
关于String::size_type pos=//...的疑问!
broadarms
2003-11-17 08:37:42
1)“::”------是什么意思?
2)size_type------又是什么意思?
3)这句话是什么意思?
//初学c++,见笑!
...全文
120
7
打赏
收藏
关于String::size_type pos=//...的疑问!
1)“::”------是什么意思? 2)size_type------又是什么意思? 3)这句话是什么意思? //初学c++,见笑!
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
7 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
Meyer
2003-11-17
打赏
举报
回复
String::size_type pos
^^^
应该是string::size_type
对string的字符索引 所使用的类型不是我们用的最多的int
而是一个 typedef 的声明, 这样做可以是标准库在各平台调整合适的类型
jyc_nj
2003-11-17
打赏
举报
回复
typedef basic_string<char> string;
class basic_string {
...
typedef A::size_type size_type;
...
}
::是作用域符号, 表示后面是String类里的(这里是个类中自定义的变量类型.)
typedef A::size_type size_type;同样, 在allocator<T>中已经定义了这个类型, 但在不同域使用要加上作用域符号, 即每次都要用上A::size_type, 故,这里定义个本类的中同意义的size_type.
class allocator {
typedef size_t size_type; //所以到底这还是常见的size_t(在win32里,就是unsigned int)
}
象string中at(size_type pos)这类函数都需要这种类型的参数, 转换是隐式完成的.
你知道是这么回事就可以了,不用太在意.
broadarms
2003-11-17
打赏
举报
回复
其实是c++ primer上的,讲文本查询系统的那章!
我贴出一段完整的程序,如下:
//下面的代码找到字符串中的第一个数字.
#include <string>
#include <iostream>
int main(){
string num1("0123456789");
string num2("c2j2");
string::size_type pos=num2.find_first_of_(num1);
cout<<"found num1 at index:"
<<pos <<"\telement is:"
<<num1[pos]<<endl;
}
schollc
2003-11-17
打赏
举报
回复
pos是不是string的一个属性,但是看上去不像,关注吧。
yuantao
2003-11-17
打赏
举报
回复
估计是string 的嵌套类把
::是表示size_type归String 所有
zhouqingyuan
2003-11-17
打赏
举报
回复
把程序前后多贴点出来先
broadarms
2003-11-17
打赏
举报
回复
楼上的意思是不是说 size_type是将数据类型转换成整型?
【c++】
string
::
size
_
type
string
::
size
_
type
在C++标准库类型
string
,在调用
size
函数求解
string
对象时,返回值为
size
_
type
类型,一种类似于unsigned类型的int 数据。可以理解为一个与unsigned含义相同,且能足够大能存储任意
string
的类型。在C++ primer 中 提到 库类型一般定义了一些配套类型,通过配套类型,库类型就能与机器无关。我理解为 机器可以分为16位 32位64位等,如果利用int等内置类型,容易影响最后的结果,过大的数据可能丢失。
string
::
string
的find函数返回值和
string
::n
pos
先看一种很多人日常使用错误方式
string
str(“hello world”); int
pos
=str.find_first_of(“h”); if(
pos
!=
string
::n
pos
) // 或者 if(
pos
!=-1) {… …// 查找到了 } //不存在是find返回-1 这样的使用,结果是正常的,但并没有很好的理解
string
find函数的返回类型和
string
::n
pos
真正的值。
string
::find这类型的函数,返回值类型都是
string
::
size
_
type
, 而
string
使用std::
string
::find() 和 std::
string
::n
pos
需要注意的地方
int
pos
= str.find("\t"); if (
pos
==
string
::n
pos
) 上述代码中,
pos
的类型被定义为 int,这是错误的,即使定义为 unsigned int 也是错的,它必须定义为
string
::
size
_
type
。 c++中 n
pos
是这样定义的: static const
size
_
type
n
pos
= -1;
std::
string
::n
pos
的使用
std::
string
::n
pos
std::
string
::n
pos
是一个常数,它等于
size
_
type
类型可以表示的最大值,用来表示一个不存在的位置,类型一般是std::container_
type
::
size
_
type
。 定义 static const
size
_
type
n
pos
= -1; #include <iostream> int main(int argc, ...
stl中的
size
_
type
,difference_
type
和value_
type
,reference
size
_
type
无符号整数类型,足够保存两个迭代器之间的距离
size
_
type
是unsigned类型,表示容器中元素长度或者下标,vector::
size
_
type
i = 0; 标准库
string
中的成员类型(member
type
),以实现标准库类型和机器的无关性,能够存下任意
string
对象的大小,是一种无符号类型。 标准库
string
的成员函数(
size
(),查找函数)的返回值类型均为
string
::
size
_
type
。 在用下标访问元素时,
string
使用
string
::
size
_ty
C语言
70,017
社区成员
243,265
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章