Maxbad`Blog

std::string 构造函数

2021-01-11 · 1 min read

C++中的String的常用函数用法总结: https://blog.csdn.net/qq_37941471/article/details/82107077

  • 1,将构造的对象初始化为s的前n个字符,s不够n个时将相邻的内存单元的内容当作s的一部分继续复制:
    string(const char *s, size_type n, const Allocator &a = Allocator());

      std::string testStr("Telephone home.", 4);
      OutputDebugStringA(testStr.c_str());
      // 输出:Tele
    
  • 2,将构造的对象初始化为s的后n个字符,也就是从s指向的内存的第n个字节开始复制:
    string(const string &str, size_type pos,size_type n = npos, const Allocator &a = Allocator());

    std::string text("Telephone home.");
    std::string testStr(text, 4);
    OutputDebugStringA(testStr.c_str());
    // 输出:phone home.
  • 3,创建一个n个字符的string对象:
    string(size_type n, char c, const Allocator &a = Allocator())
      std::string testStr(10, 'a');
      OutputDebugStringA(testStr.c_str());
      // 输出:aaaaaaaaaa
    

其他文档:http://c.biancheng.net/view/1443.html