C ++ std :: make_shared

时间:2018-03-25 04:13:41

标签: c++ c++11 make-shared

以下代码片段来自C ++ Concurrency In Action Practical Multithreading第152页,这是一个线程安全的堆栈类。我的问题是为什么以下pop函数(线程安全堆栈类)只能return std::make_shared<T>(std::move(data.top()),而数据类型为std::stack<T>,因为make_shared<T>返回shared_ptr 1}?提前谢谢!

std::shared_ptr<T> pop()
{
std::lock_guard<std::mutex> lock(m);
if(data.empty()) throw empty_stack();
std::shared_ptr<T> const res(std::make_shared<T>(std::move(data.top())));
data.pop();
return res;
}

1 个答案:

答案 0 :(得分:0)

这是因为你需要在临时变量(res)中存储值,然后才能通过pop()调用来破坏它。

from bs4 import BeautifulSoup
import urllib.request


url="http://classes.usc.edu/term-20181/classes/itp/"
page=urllib.request.urlopen(url)
soup=BeautifulSoup(page.read(),"html.parser")

# ask for user input
choiceUnits = float(input("Enter number of units:"))
choiceUnits = "(" + str(choiceUnits) + " units)"

#trying to find the tag that contain all the courses information
coursesTable = soup.find("div",class_="course-table")

#trying to find each course table under the course-table tag
courses = coursesTable.find_all("div",class_="course-info expandable") 
for course in courses:
    # trying to find the course units
    units = course.find("span",class_="units").text

    #compare the course units with the user input.
    #If they are the same, find out the course title,time,students registered and instruction
    if units == choiceUnits:
        title = course.find("a",class_="courselink").text 
        print("Course - {}:".format(title))

        for row in course.find("table", class_="sections responsive").find_all("tr")[1:]:
            time = row.find("td",class_="time").text
            register = row.find("td",class_="registered").text
            instructor = row.find("td",class_="instructor").text

            print(time,register,instructor)