C/C++中求字符串数组的大小---sizeof,strlen,string,length()
来源:互联网 发布:无人机航拍系统编程 编辑:程序博客网 时间:2024/05/05 19:47
#include "iostream"//#include <string>#include <cstring>//strlen/**<string>是C++标准库头文件,包含了拟容器class std::string的声明(不过class string事实上只是basic_string<char>的typedef),用于字符串操作。*cstring>是C标准库头文件<string.h>的C++标准库版本,包含了C风格字符串(NUL即'\0'结尾字符串)相关的一些类型和函数的声明,例如strcmp、strchr、strstr等。<cstring>和<string.h>的最大区别在于,其中声明的名称都是位于std命名空间中的,而不是后者的全局命名空间。*/using namespace std;//字符串数组首地址作为参数时,数组大小不能一同传入int printnum(char** elem){if(elem = NULL)cout<<"elem = NULL"<<endl;cout<<"sizeof(elem) = "<<sizeof(elem)<<endl;//指针都是4个字节cout<<"sizeof(elem[0]) = "<<sizeof(elem[0])<<endl;int x = sizeof(elem)/sizeof(elem[0]);return x;}int printstringnum(string* elem){if(elem = NULL)cout<<"elem = NULL"<<endl;cout<<"sizeof(elem) = "<<sizeof(elem)<<endl;//指针都是4个字节cout<<"sizeof(elem[0]) = "<<sizeof(elem[0])<<endl;int x = sizeof(elem)/sizeof(elem[0]);return x;}int main(){char* color[] = {"redyellowaddfdfg","green","blue","yellow"};cout<<"sizeof(color) = "<<sizeof(color)<<endl;//4*4. 求的不是元素数组大小,而是元素的所有指针占据的大小cout<<"sizeof(color[0]) = "<<sizeof(color[0])<<endl;//4.求的不是元素大小,而是指针大小cout<<"sizeof(char*) = "<<sizeof(char*)<<endl;//4cout<<"sizeof(color[3]) = "<<sizeof(color[3])<<endl;//4cout<<"strlen(color[0]) = "<<strlen(color[0])<<endl;//16int y = sizeof(color)/sizeof(color[0]);cout<<"y = "<<y<<endl;int x = printnum(color);cout<<"x = "<<x<<endl;string col[] = {"red","green","blue","yellow"};cout<<"sizeof(col) = "<<sizeof(col)<<endl;//4*16,cout<<"sizeof(col[0]) = "<<sizeof(col[0])<<endl;//cout<<"sizeof(string) = "<<sizeof(string)<<endl;//cout<<"sizeof(col[3]) = "<<sizeof(col[3])<<endl;//cout<<"col[0].length = "<<col[0].length()<<endl;//cout<<"col[3].length = "<<col[3].length()<<endl;int y2 = sizeof(col)/sizeof(col[0]);cout<<"y2 = "<<y2<<endl;int x2 = printstringnum(col);cout<<"x2 = "<<x2<<endl;string str = "add";cout<<"str.length() = "<<str.length()<<endl;return 0;}
- C/C++中求字符串数组的大小---sizeof,strlen,string,length()
- char字符串数组与string之间的转换,以及sizeof,strlen,length的区别
- C中 字符数组和字符串以及strlen 和sizeof
- C/C++中字符串数组及strlen()和sizeof()区别
- C++ 数组与字符串长度sizeof()、strlen()、string的length()和size()
- 数组与字符串长度sizeof()、strlen()、string的length()和size()
- 数组与字符串长度sizeof()、strlen()、string的length()和size()
- 数组与字符串长度sizeof()、strlen()、string的length()和size()
- 数组与字符串长度sizeof()、strlen()、string的length()和size()
- 数组与字符串长度sizeof()、strlen()、string的length()和size()
- c/c++中sizeof()、strlen()、length()、size()详解和区别
- C 字符串中sizeof() 和 strlen()
- sizeof()、strlen()、string中length()和size()
- [C][C++]长度、大小等的详解:sizeof, strlen, size...
- [C][C++]长度、大小等的详解:sizeof, strlen, size...
- C基础 对字符串数组的sizeof和strlen的区别
- C中字符串和字符,strlen和sizeof()
- C/C++中字符串长度strlen()/sizeof()区别
- 翻转句子中单词的顺序(10)
- 大数相乘 +方法2(C语言)
- 【学习笔记】读《领导力》第四版 笔记一
- linux 线程的绑定 PTHREAD_SCOPE_SYSTEM(绑定的)和PTHREAD_SCOPE_PROCESS(非绑定的)
- Hibernate程序性能优化的考虑要点
- C/C++中求字符串数组的大小---sizeof,strlen,string,length()
- 算法整理
- SSH初步整合
- C/C++中 static 用法总结
- PHP正则表达式学习
- 线程安全
- eCos Synthetic实践(二)——简单外设
- 做网络成功的60句话 连载四
- 求1+2+…+n(12)