C++
weixin_43489941
这个作者很懒,什么都没留下…
展开
-
给定一个数组arr,返回arr的最长无的重复子串的长度(无重复指的是所有数字都不相同)。
#include "iostream"#include <vector>#include <map>using namespace std;int maxLength(vector<int>& arr){ int maxlen = 0; vector<int> v; for (int i = 0; i < arr.size(); i++) { if (v.size() == 0) { v.push_back(a原创 2021-04-05 13:03:57 · 926 阅读 · 0 评论 -
给定一个double类型的数组arr,其中的元素可正可负可0,返回子数组累乘的最大乘积。
#include "iostream"#include <vector>using namespace std;double maxproduct(vector<double> arr){ double max = arr[0]; for (vector<double> ::iterator it = arr.begin(); it != arr.end(); it++) { double product = *it; max = max >原创 2021-04-02 12:13:16 · 487 阅读 · 0 评论 -
智能指针什么时候释放?什么时候引用计数为0
智能指针内部有一个计数器,当赋值给别的智能指针或者函数传参拷贝到另一个shared_ptr,计数器就会加1,当函数执行完毕,智能指针对象 就被析构了,此时计数器就会减一,知道计数器变为0 ,说明没人在用这个对象了,就执行delete把它释放掉。#include <iostream>#include <memory>using namespace std;class Person{public: Person(int age,int height) { this-原创 2021-03-18 09:20:59 · 2805 阅读 · 0 评论 -
智能指针调用成员函数
#include <iostream>#include <memory>using namespace std;class Person{public: Person() { cout<<"执行无参构造函数"<<endl; } void setAge(int age) { this->m_Age = age; cout<<"执行setAge函数"<<endl; } int getAge() {原创 2021-03-17 11:38:37 · 624 阅读 · 0 评论 -
智能指针本本质是模板类
#include <iostream>#include <string>#include <unordered_map>using namespace std;#include <memory>int main(){ std::unordered_map<int, std::shared_ptr<int>> g_serviceSetMaps; g_serviceSetMaps.insert(std::make_pair(原创 2021-03-16 11:37:41 · 79 阅读 · 0 评论 -
为什么要用智能指针?
由于浅拷贝会导致多次释放内存,这时候需要借助深拷贝。但是有了智能指针后,可以根据实际的引用数量来释放内存,而不需要自己设计拷贝函数。#include <iostream>#include <memory>using namespace std;class Person{public: Person(int age,int height) { this->m_Age = age; m_Height = new int(height); //thi原创 2021-03-12 16:02:19 · 112 阅读 · 0 评论