• STL string的构造函数


         前几天在网上,一位网友问我几个问题如下:

     1     string S0(6, 'A');
     2 
     3     string S1 = "abcdefg";
     4 
     5     string S2(S1, 0, 3);
     6     string S3 (S1, 3);
     7 
     8     cout << "s0 = " << S0.c_str() <<endl;
     9     cout << "s2 = " << S2.c_str() <<endl;
    10     cout << "s3 = " << S3.c_str() <<endl;

    就这么简单的几句语句,发现如果不去运行下,还真不知道结果是什么。

    常用的 string赋值都是这样的:

    1 string s = "aaa";
    2 
    3 string s1("bbbb");

    查了下资料,才知道string的构造函数居然有那么多。

    1 a) string s; //生成一个空字符串s 
    2 b) string s(str) //拷贝构造函数 生成str的复制品 
    3 c) string s(str, stridx) //将字符串str内"始于位置stridx"的部分当作字符串的初值 
    4 d) string s(str, stridx, strlen) //将字符串str内"始于stridx且长度顶多strlen"的部分作为字符串的初值 
    5 e) string s(cstr) //将cstr字符串作为s的初值 
    6 f) string s(chars, chars_len) //将C字符串前chars_len个字符作为字符串s的初值。 
    7 g) string s(num, c) //生成一个字符串,包含num个c字符 
    8 h) string s(beg, end) //以区间beg;end(不包含end)内的字符作为字符串s的初值 
    高山流水,海纳百川!
  • 相关阅读:
    Swift
    Swift
    Swift
    Swift
    Swift
    Swift
    Swift
    C++生产和使用的临时对象
    RecyclerView0基于使用
    Docker创建MySQL集装箱
  • 原文地址:https://www.cnblogs.com/ahcc08/p/3691072.html
Copyright © 2020-2023  润新知