C++ Primer Plus 学习笔记 第十六章 string类, 智能指针模板类

使用string类

string类对每个关系运算符都做了重载 并且都以三种方式被重载:

string 与string

string与c-string

c-string 与 string

用心, 赞!

size()和length()都能够返回字符串中的字符数 效果一样

find()

string类的内存分配原理

程序示例

#include <iostream>
#include <string>

int main()
{
  using namespace std;
  string empty;
  string small = "bit";
  string larger = "Elephants are a girl's best friend";
  cout << "Sizes:\n";
  cout << "\tempty: " << empty.size() << endl;
  cout << "\tsmall: " << small.size() << endl;
  cout << "\tlarger: " << larger.size() << endl;
  cout << "Capacities:\n";
// 查看string对象分配的内存大小
  cout << "\tempty: " << empty.capacity() << endl;
  cout << "\tsmall: " << small.capacity() << endl;
  cout << "\tlarger: " << larger.capacity() << endl;
// 手动设置string对象的内存大小
  empty.reserve(50);
  cout << "Capacity after empty.reserve(50): "
       << empty.capacity() << endl;

  return 0;
}

运行结果

通过c_str()函数能够将string转换成c风格字符串 返回指向该字符串的指针。

string种类

只能指针模板类

有3种

auto_ptr

unique_ptr

shared_ptr

智能指针模板包含在memory头文件中

示例:

#include <iostream>
#include <string>
#include <memory>

class Report
{
  private:
    std::string str;
  public:
    Report(const std::string s) : str(s)
      { std::cout << "Object created!\n";}
    ~Report() {std::cout << "Object deleted!\n";}
    void comment() const {std::cout << str << "\n";}
};

int main()
{
  // 我这里的编译器表示木有这货 估计被移除了
  // {
  //   std::auto_ptr<Report> ps (new Report("using auto_ptr"));
  //   ps->comment();
  // }
  {
    std::shared_ptr<Report> ps (new Report("using shared_ptr"));
    ps->comment();
  }
  {
    std::unique_ptr<Report> ps (new Report ("using unique_ptr"));
    ps->comment();
  }
  return 0;
}

不要把非堆内存丢到智能指针中去

shared_ptr:策略

示例:

#include <iostream>
#include <string>
#include <memory>

int main()
{
  using namespace std;
  shared_ptr<string> films[5] = 
  {
    shared_ptr<string> (new string("Fowl Balls")),
    shared_ptr<string> (new string("Duck Walks")),
    shared_ptr<string> (new string("Chicken Runs")),
    shared_ptr<string> (new string("Turkey Errors")),
    shared_ptr<string> (new string("Goose Eggs"))
  };
  shared_ptr<string> pwin;
  pwin = films[2];
  cout << "The nominees for best avian baseball film are\n";
  for (int i = 0; i < 5; i++)
    cout << *films[i] << endl;
  cout << "The winner is " <<*pwin << "!\n";
  cin.get();
  return 0;
}

运行结果

unique_ptr

两个智能指针指向同一个对象的时候 unique_ptr会在编译的时候就报错 而不是在运行阶段

但unique_ptr允许悬挂指针的存在

感觉unique_ptr很费劲 不用悬挂指针吧 用 move()

new[] 的话 使用unique_ptr<>

new 的话 使用 shared_ptr<>, unique_ptr<>

不是new的话 那就再见

 

如何选择智能指针

建议用shared_ptr<>的场景

建议使用uniqure_ptr<>的场景

uniqre_ptr可以转换成shared_ptr

完结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@凌晨三点半

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值