大挪耗

c++string容器基础

#include "iostream"
#include "string"

using namespace std;

int main(){
    //显示string类型
    cout<<"npos="<<string::npos<<endl;
    //1)string():创建一个长度为0的string
    string s1;
    cout<<"s1.capacity()="<<s1.capacity()<<endl;//返回当前容量,可以存放字符的总数
    cout<<"s1.size()"<<s1.size()<<endl;//返回容器中数据的大小。
    s1="xxxxxxxxxxxxxxxxxxxx";
    cout<<"s1.capacity()"<<s1.capacity()<<endl;
    cout<<"s1.size()="<<s1.size()<<endl;
    cout<<"容器动态数组的首地址="<<(void *)s1.c_str()<<endl;

    //string(const char *s): string对象初始化为s指向的NBTS(转换函数)。
    string s2("hello world");
    cout<<"s2="<<s2<<endl;
    string s3="hello world";
    cout<<"s3="<<s3<<endl;

    // 3)string(const string &str):将strin对象初始化str(拷贝构造函数)
    string s4(s3);
    cout<<"s4="<<s4<<endl;
    string s5=s3;
    cout<<"s5="<<s5<<endl;

    string s6("hello world",5);
    cout<<"s6="<<s6<<endl;
    string s7("hello world",50);
    cout<<"s7="<<s7<<endl;
//5)string(const string & str,size_t pos=0,size_t n=npos);
//将string对象初始化为str位置pos开始到结尾的字符,或从位置pos开始的n个字符。

    string s8(s3,3,5);//s3="hello world";
    cout<<"s8="<<s8<<endl;//将输出s8=lo wo
    string s9(s3,3);
    cout<<"s9="<<s9<<endl;//将输出s9=lo world
    cout<<"s9.capacity()="<<s9.capacity()<<endl;
    cout<<"s9.size()"<<s9.size()<<endl;
    string s10("hello world",3,5);
    cout<<"s10="<<s10<<endl;
    string s11("hello world",3);
    cout<<"s11="<<s11<<endl;    //将输出s11=hel
//7)string(size_t_n,char c):创建一个由n个字符c组成的string对象。
    string s12(8,'8');
    cout<<"s12="<<s12<<endl;//将输s12=xxxxxxxx
    cout<<"s12.capacity()="<<s12.capacity()<<endl;//s12。capacity()=15
    cout<<"s12.size()="<<s12.size()<<endl;//s12.size()=8
    string s13(30,0);
    cout<<"s13="<<s13<<endl;//将输出s13=
    cout<<"s13.capacity()="<<s13.capacity()<< endl;//s13.capacity()=31
    cout<<"s13.size()="<<s13.size()<<endl;//s13.size()=30



}

输出:

npos=18446744073709551615
s1.capacity()=15
s1.size()0
s1.capacity()30
s1.size()=20
容器动态数组的首地址=0x1be7a556570
s2=hello world
s3=hello world
s4=hello world
s5=hello world
s6=hello
s7=hello world s2= s3= s4= s5= s6= s7= s8= s9= s9.cap
s8=lo wo
s9=lo world
s9.capacity()=15
s9.size()8
s10=lo wo
s11=hel
s12=88888888
s12.capacity()=15
s12.size()=8
s13=
s13.capacity()=30
s13.size()=30


已发布

分类

可以收藏大挪耗一下。下载麻烦点城通网盘,站长保证下载速度,不会限速的,放心点就是了;分卷,安卓下载为txt:程序下载为url,不会下载参考不会下载。如果你想让本站活的久一点,请直接捐助

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注