repost: auto_ptr与unique_ptr的关系:

from: https://blog.csdn.net/weixin_40081916/article/details/79377564
简言之: auto_ptr 用复制语法表示移动语义,和 C++约定和人的直觉不匹配.

auto_ptr与unique_ptr
auto_ptr是用于C++11之前的智能指针。由于 auto_ptr 基于排他所有权模式:两个指针不能指向同一个资源,复制或赋值都会改变资源的所有权。auto_ptr 主要有两大问题:
o 复制和赋值会改变资源的所有权,不符合人的直觉。
o 在 STL 容器中无法使用auto_ptr ,因为容器内的元素必需支持可复制(copy constructable)和可赋值(assignable)。

unique_ptr特性
o 拥有它所指向的对象
o 无法进行复制构造,也无法进行复制赋值操作
o 保存指向某个对象的指针,当它本身离开作用域时会自动释放它指向的对象。

unique_ptr可以:
o 为动态申请的内存提供异常安全
o 将动态申请内存的所有权传递给某个函数
o 从某个函数返回动态申请内存的所有权
o 在容器中保存指针
unique_ptr十分依赖于右值引用和移动语义。

在C++11中已经放弃auto_ptr转而推荐使用unique_ptr和shared_ptr。unique跟auto_ptr类似同样只能有一个智能指针对象指向某块内存.但它还有些其他特性。unique_ptr对auto_ptr的改进如下:

1, auto_ptr支持拷贝构造与赋值操作,但unique_ptr不直接支持
auto_ptr通过拷贝构造或者operator=赋值后,对象所有权转移到新的auto_ptr中去了,原来的auto_ptr对象就不再有效,这点不符合人的直觉。unique_ptr则直接禁止了拷贝构造与赋值操作。
auto_ptr ap(new int(10));
auto_ptr one (ap) ; // ok
auto_ptr two = one; //ok

unique_ptr ap(new int(10));
unique_ptr one (ap) ; // 会出错
unique_ptr two = one; //会出错

2,unique_ptr可以用在函数返回值中
unique_ptr像上面这样一般意义上的复制构造和赋值或出错,但在函数中作为返回值却可以用.
unique_ptr GetVal( ){
unique_ptr up(new int(10 );
return up;
}
unique_ptr uPtr = GetVal(); //ok

实际上上面的的操作有点类似于如下操作
unique_ptr up(new int(10);
unique_ptr uPtr2 = std:move( up) ; //这里是显式的所有权转移. 把up所指的内存转给uPtr2了,而up不再拥有该内存

3,unique_ptr可做为容器元素
我们知道auto_ptr不可做为容器元素,会导致编译错误。虽然unique_ptr同样不能直接做为容器元素,但可以通过move语意实现。
unique_ptr sp(new int(88) );
vector<unique_ptr > vec;
vec.push_back(std::move(sp));
// vec.push_back( sp ); error:
// cout << *sp << endl;
std::move让调用者明确知道拷贝构造、赋值后会导致之前的unique_ptr失效。

#include
#include
#include
using namespace std;

unique_ptr GetVal( ){
unique_ptr up(new int(10));
return up;
}

int main()
{
// auto_ptr支持拷贝构造与赋值,拷贝构造或赋值后对象所有权转移
auto_ptr aPtr(new int(10));
auto_ptr a1 (aPtr); // call copy constuctor, OK
assert(aPtr.get() == nullptr && a1.get() != nullptr);
auto_ptr a2(NULL);
a2 = a1; // call operator=, OK
assert(a1.get() == nullptr && a2.get() != nullptr);

// unique_ptr不支持直接拷贝构造与赋值
unique_ptr<int> uPtr(new int(10));
// unique_ptr<int> u1 (uPtr);       // call copy constructor, error 
// Error: Call to implicitly-deleted copy constructor of 'unique_ptr<int>'

// unique_ptr<int> u2 = nullptr;
// u2 = uPtr;               // call operator=, error
// Error: Overload resolution selected implicitly-deleted copy assignment operator

// unique_ptr支持move语意的拷贝构造
unique_ptr<int> u1(std::move(uPtr));
assert(uPtr == nullptr && u1 != nullptr);

// unique_ptr支持move语意的赋值
unique_ptr<int> u2 = std::move(u1) ; // 把u1所指的内存转给u2了,而u1不再拥有该内存
assert(u1 == nullptr && u2 != nullptr);

// unique_ptr通过move语意可放入STL容器
unique_ptr<int> sp(new int(100));
vector<unique_ptr<int>> vec;
vec.push_back(std::move(sp));

// vec.push_back(sp);
// Error: Call to implicitly-deleted copy constructor of 'std::__1::unique_ptr<int, std::__1::default_delete<int> >'

assert(sp == nullptr);
// cout << *sp << endl;  // crash at this point, sp is nullptr now

unique_ptr<int> g_uPtr = GetVal();   //ok

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import re import json import time import requests import datetime import pymysql import selenium from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from sqlalchemy import create_engine, Column, Integer, String, Text, DateTime from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base from selenium.webdriver import Edge, EdgeOptions # 创建浏览器对象 options = EdgeOptions() options.use_chromium = True options.binary_location = r'C:\Users\邓枫林\PycharmProjects\pythonProject\edgedriver_win64\msedgedriver.exe' browser = Edge(options=options) wait = WebDriverWait(browser, 10) # 打开微博话题页面 url = 'https://weibo.com/n/%E4%B8%AD%E5%9B%BD%E9%A3%9F%E5%93%81%E5%8D%AB%E7%94%9F?from=feed&loc=at&nick=%E4%B8%AD%E5%9B%BD%E9%A3%9F%E5%93%81%E5%8D%AB%E7%94%9F&order=hot' browser.get(url) # 等待页面加载完成 wait.until(lambda driver: driver.execute_script("return document.readyState") == "complete") browser = selenium.webdriver.Edge(executable_path='C:/Users/邓枫林/PycharmProjects/pythonProject/edgedriver_win64/msedgedriver.exe') wait = selenium.webdriver.support.ui.WebDriverWait(browser, 10) # 监测页面是否包含“高校类”敏感词汇 if '高校类' in browser.page_source: # 获取原始微博 weibo = browser.find_element_by_css_selector('.WB_feed_detail .WB_text.W_f14').text # 获取转发该微博的用户昵称和转发内容 reposts = [] repost_items = browser.find_elements_by_css_selector('.list_ul .list_li') for item in repost_items: nickname = item.find_element_by_css_selector('.WB_text.W_f14').text content = item.find_element_by_css_selector('.WB_text.W_f14 + .comment_txt').text reposts.append({'nickname': nickname, 'content': content}) # 关闭浏览器 browser.quit() # 将微博和转发内容存入MySQL数据库中 Base = declarative_base() class Weibo(Base): __tablename__ = 'weibo_user' id = Column(Integer, primary_key=True) content = Column(Text) create_time = Column(DateTime) class Repost(Base): __tablename__ = 'weibo_repost' id = Column(Integer, primary_key=True) weibo_id = Column(Integer) nickname = Column(String(50)) content = Column(Text) engine = create_engine('mysql+pymysql://root:root@hostname:port/weibo?charset=utf8mb4') Session = sessionmaker(bind=engine) session = Session() now = datetime.datetime.now() weibo_obj = Weibo(content=weibo, create_time=now) session.add(weibo_obj) session.commit() for repost in reposts: repost_obj = Repost(weibo_id=weibo_obj.id, nickname=repost['nickname'], content=repost['content']) session.add(repost_obj) session.commit() session.close() else: # 关闭浏览器 browser.quit()
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值