string基本的类成员函数的使用(at、length、size、begin、empty、resize)
2012-12-30 15:22
851 查看
本文详细介绍string类的较简单也比较基本的成员函数的使用样式:
at/length/clear/begin/empty/resize
1 at 返回字符串中某个位置的处的字符,类似数组的操作
2 length、size计算字符串的长度
3 clear 删除全部字符
4 begin 返回string类变量中第一个元素的迭代器
5 empty 判断字符串是否为空
6 resize 重新分配空间,将多分配出的空间用给出的字符参数填充
下面通过一个程序来展示上面的函数应用及格式:
at/length/clear/begin/empty/resize
1 at 返回字符串中某个位置的处的字符,类似数组的操作
2 length、size计算字符串的长度
3 clear 删除全部字符
4 begin 返回string类变量中第一个元素的迭代器
5 empty 判断字符串是否为空
6 resize 重新分配空间,将多分配出的空间用给出的字符参数填充
下面通过一个程序来展示上面的函数应用及格式:
// the functions of string #include<iostream> #include<string> using namespace std; int main() { string str("Bill Gates"); //the usage of length/size int len1=str.length(); int len2=str.size(); cout<<"len1="<<len2<<" "<<"len2="<<len2<<endl; //str.begin() cout<<"str.begin()="<<*str.begin()<<endl; //the usage of str.at() cout<<"the usage of .at():"<<endl; for(int i=0;i<len1;i++) { if(i%2) cout<<str.at(i)<<" "; } cout<<endl; //the usage of resize() cout<<"the usage of .resize()"<<endl; str.resize(len1+2,'B'); cout<<str<<endl; cout<<"use the .empty() judge the string"<<endl; if(!str.empty()) cout<<"I am not empty!"<<endl; return 0; }
相关文章推荐
- STL中容器的几个容易混淆的成员函数-size, capacity, resize, reserve
- 编写一个类,实现简单的栈操作。数据的操作按先进后出的顺序。成员函数为pop; push; size; full; empty; print等等
- STL中容器的几个容易混淆的成员函数-size, capacity, resize, reserve
- STL中容器的几个容易混淆的成员函数-size, capacity, resize, reserve
- string的size/length、resize/reverse和capacity
- matlab中的size(),length(),ndims()函数的使用方法
- string的empty,size,length等比较
- C++基础---string类的capacity/max_size/size/length/empty/reserve/resize
- 在类中使用_beginthreadex函数创建线程并访问该类中的数据成员
- 数组与字符串长度sizeof()、strlen()、string的length()和size()
- C++中string的成员函数
- ExtJs的基本函数使用
- 类成员函数作为线程函数使用
- 慎重使用Get/Set成员函数(二)Set函数和其他修改函数的替代办法
- 创建一个三角形类并且使用成员函数计算三角形的周长和面积《2》
- 怎样在代码中使用Debug的调试?另外附带完整的使用流程------OutputDebugString函数
- 成员函数后面加const,没有const,以及使用的区别
- C++使用线程函数pthread_create时,调用的成员函数要定义为静态成员函数
- NSString (NSStringPathExtensions) 中 stringByDeletingPathExtension 等函数使用的误区
- c++中使用string类型 推广到其他非基本类型的使用前提