string的size()和length()
2009-11-16 10:42
323 查看
string的size()和length()
标签:
string算法语言c
2009-11-16 10:42
42303人阅读 评论(8)
收藏
举报
本文章已收录于:
分类:
C++(36)
作者同类文章X
版权声明:本文为博主原创文章,未经博主允许不得转载。
C++标准库中的string中两者的源代码如下:
size_type __CLR_OR_THIS_CALL length() const
{ // return length of sequence
return (_Mysize);
}
size_type __CLR_OR_THIS_CALL size() const
{ // return length of sequence
return (_Mysize);
}
所以两者没有区别。
length是因为沿用C语言的习惯而保留下来的,string类最初只有length,引入STL之后,为了兼容又加入了size,它是作为STL容器的属性存在的,便于符合STL的接口规则,以便用于STL的算法。
string类的size()/length()方法返回的是字节数,不管是否有汉字。
顶0踩0
上一篇对重复包含的初步认识
下一篇对预编译的认识
// sample2.cpp : Defines the entry point for the console application.
#include "stdafx.h"
int main(int argc, char* argv[])
//i) ==,!=,<,<=,>,>=,compare() //比较字符串
//j) size(),length() //返回字符数量
Hello World!
======== ======== =========
-1
1
0
======== ======== =========
16
3
3
======== ======== =========
Press any key to continue
标签:
string算法语言c
2009-11-16 10:42
42303人阅读 评论(8)
收藏
举报
本文章已收录于:
分类:
C++(36)
作者同类文章X
版权声明:本文为博主原创文章,未经博主允许不得转载。
C++标准库中的string中两者的源代码如下:
size_type __CLR_OR_THIS_CALL length() const
{ // return length of sequence
return (_Mysize);
}
size_type __CLR_OR_THIS_CALL size() const
{ // return length of sequence
return (_Mysize);
}
所以两者没有区别。
length是因为沿用C语言的习惯而保留下来的,string类最初只有length,引入STL之后,为了兼容又加入了size,它是作为STL容器的属性存在的,便于符合STL的接口规则,以便用于STL的算法。
string类的size()/length()方法返回的是字节数,不管是否有汉字。
顶0踩0
上一篇对重复包含的初步认识
下一篇对预编译的认识
// sample2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
//h) + //串联字符串
string hello("Hello ");
string world("World!");
cout<<hello<<world<<endl;
cout<<"======== ======== ========="<<endl;
//i) ==,!=,<,<=,>,>=,compare() //比较字符串
{
string abc("abc");
string abd("abd");
cout<<abc.compare(abd)<<endl;
string abb("abb");
cout<<abc.compare(abb)<<endl;
string abc2("abc");
cout<<abc.compare(abc2)<<endl;
cout<<"======== ======== ========="<<endl;
}
//j) size(),length() //返回字符数量
{
string abc("abc");
cout<<sizeof(abc)<<endl;
cout<<abc.size()<<endl;
cout<<abc.length()<<endl;
cout<<"======== ======== ========="<<endl;
}
return 0;
}
======== ======== =========
-1
1
0
======== ======== =========
16
3
3
======== ======== =========
Press any key to continue
相关文章推荐
- string的size()和length()
- Parameter \' \': No size set for variable length data type: String
- C++中string的size,length,capacit…
- 前不久出现的WCF一个错误 —— MaxStringContentLength & MaxReceivedMessageSize
- 数组与字符串长度sizeof()、strlen()、string的length()和size()
- WCF 配置文件中的MaxStringContentLength & MaxReceivedMessageSize
- length、length()、size()区别 List与String相互转换
- Parameter 'p_REMARK': No size set for variable length data type: String.
- C++中string的size,length,capacity三者到底有何区别求解啦?
- string的size()和length()
- 数组与字符串长度sizeof()、strlen()、string的length()和size()
- string的size、length、capacity
- Java for循环每次都通过list.size()和 string.length()获取大小是否消耗性能?
- std::string 的length()与size()方法没有区别
- string的size()和length()
- 用C++的string::size()和string::length()返回值做比较
- string的length()与size()方法有什么区别
- 2. std::string 的 size() length() capacity()
- sizeof()、strlen()、string中length()和size()
- string的size()和length()