- 博客(209)
- 资源 (1)
- 收藏
- 关注
原创 14.3.1
14.16friend bool operator==(const StrBlob&, const StrBlob&);friend bool operator!=(const StrBlob&, const StrBlob&);bool operator==(const StrBlob &lhs, const StrBlob &rhs){ return *lhs.data == *rh
2017-02-24 16:39:17
577
原创 14.3
14.13Sales_data& Sales_data::operator+=(const Sales_data &rhs){ units_sold += rhs.units_sold; revenue += rhs.revenue; return *this;}Sales_data& Sales_data::operator-=(const Sales_data &r
2017-02-24 16:28:08
498
原创 14.2.2
14.9std::istream& operator>>(std::istream &is, Sales_data &item){ double price = 0.0; is >> item.bookNo >> item.units_sold >> price; if (is) item.revenue = price * item.units_sold;
2017-02-24 16:08:37
410
原创 14.2.1
14.6friend std::ostream& operator<<(std::ostream&, const Sales_data&);std::ostream& operator<<(std::ostream &os, const Sales_data &item){ os << item.isbn() << " " << item.units_sold << " " << item
2017-02-24 15:51:15
411
原创 14.1
14.1 内置运算符有明确的定义 而重载的运算符的功能有你重载的版本决定当你重载的运算符和内置的含义和用法是一样的,那么可以认为它们是一样的14.2Sales_data& Sales_data::operator+=(const Sales_data &rhs){ units_sold += rhs.units_sold; revenue += rhs.revenue;
2017-02-24 15:28:20
543
原创 List(double linked)
简易版链表 ,目前还有些问题, 1.由于构造函数中会new一个Node,所以链表中会对出一个由默认T()构造的Node 2.打印链表中的元素非常不方便 等/*LIST-SEARCH(L,k)x=L.headwhile x!=NIL and key !=k x=x.nextreturn xLIST-INSERT(L,x)x.next= L.headif L.head!=NIL
2017-02-21 18:23:13
1084
原创 Queue
Queue.h#pragma oncetemplate<class T>class queue {public: queue(); ~queue(); bool enqueue(const T); T dequeue(); bool empty() const; bool full() const;private: T* data;
2017-02-16 20:18:39
334
原创 Stack
简单版 Stack Stack.h#pragma oncetemplate<class T>class stack {public: stack(); ~stack(); bool empty(); T pop(); bool push(T);private: T* data; int top;};Stack.cpp#include"St
2017-02-16 17:08:43
356
原创 CountingSort
/*for i←1 to k do C[i] ←0for j←1ton do C[A[j]] ←C[A[j]] + 1 ⊳C[i] = |{key = i}|for i←2 to k doC[i] ←C[i] + C[i–1] ⊳C[i] = |{key ≤i}|for j←n downto 1 do B[C[A[j
2017-02-13 16:21:37
406
原创 13.6.3
13.55void push_back(string &&s) { data->push_back(std::move(s)); }13.56Foo Foo::sorted() const & { Foo ret(*this); return ret.sorted();}将会无限递归 因为ret是一个左值,ret.sorted() 调用的不是成员函数Foo Foo::sorted
2017-02-10 21:35:00
287
原创 13.6.2
13.49StrVec(StrVec&&) NOEXCEPT;StrVec& operator=(StrVec&&) noexcept;StrVec::StrVec(StrVec &&s) noexcept : elements(s.elements), first_free(s.first_free), cap(s.cap){ s.elements = s.first_free = s.
2017-02-10 21:23:19
358
原创 13.6.1
13.45 左值引用一般绑定在左值上 右值引用引用对象将被销毁,对象没有其他用户13.46int f(); vector<int> vi(100);int&& r1 = f();int& r2 = vi[0];int& r3 = r1;int&& r4 = vi[0] * f();13.47#include <memory>class String{public: Str
2017-02-10 16:50:25
378
原创 13.5
13.39#include<string>#include<memory>using std::string;class StrVec {public: StrVec():elements(nullptr),first_free(nullptr),cap(nullptr){} StrVec(const StrVec&); StrVec& operator= (const
2017-02-09 22:27:53
349
转载 13.4
13.33 因为我们需要改变Folder本身,所以必须是Folder&13.34 @pezy#include <string>#include <set>class Folder;class Message { friend void swap(Message &, Message &); friend class Folder;public: explicit Me
2017-02-09 12:51:27
309
原创 13.3
13.29swap(lhs.ps, rhs.ps);swap(lhs.i, rhs.i);他们调用的是另一个版本的swap而不是 swap(HasPtr&, HasPtr&) 所以并不会递归13.30#include <string>#include <iostream>class HasPtr {public: friend void swap(HasPtr&, HasPtr&)
2017-02-06 21:50:49
308
原创 13.2.2
13.27#include<string>using std::string;class HasPtr {public: HasPtr(const string& s = string()):ps(new string(s)),i(0),use(new size_t(1)) {} HasPtr(const HasPtr& p) :ps(p.ps), i(p.i), use(p.use
2017-02-05 20:15:05
400
原创 13.2.1
13.23 有一定差异,我使用if判断是否自我拷贝 本节使用先拷贝右侧对象的方式来使自我拷贝正常运行13.24 如果没有定义析构函数,肯定会发生内存泄露 如果没有定义拷贝构造函数,当使用了拷贝构造函数时,删除一个对象会使和其他对象共用的内存被释放,其他对象则出现错误13.25 拷贝构造和拷贝赋值必须新建内存,而不是和右侧共用内存当shared_ptr计时器为0时,会自动的释放,所以不需要析
2017-02-04 23:09:35
307
原创 13.2
13.22#include <string>class HasPtr {public: HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) { } HasPtr(const HasPtr& hp) : ps(new std::string(*hp.ps)), i(hp.i) { }
2017-02-04 22:35:41
321
原创 13.1.6
13.18#include<string>using std::string;class Employee { Employee(const string& aname):name(aname){ id = increment++; } Employee() { id = increment++; }private: strin
2017-02-04 21:10:16
340
原创 13.1.4
13.14 输出3个相同的数据13.15 改变,会输出3个不同的数据 但是这个数据和我们传入f的数据并不同。13.16 改变,会输出3个不同的数据,但是数据和我们传入f的数据是一致的13.17 正确 https://github.com/PYPARA/Cpp-Primer/tree/master/ch13#include <iostream>class numbered {publ
2017-02-04 19:55:55
346
原创 13.13
13.9 类里前面带~的函数,析构函数用来销毁对象释放资源,当没有定义自己的析构函数的时候编译器会自己定义合成析构函数。13.10 智能指针的计时器会变为0,然后会释放智能指针指向的内存,然后销毁对象。 销毁对象,但是指针指向的内存不会被释放,因为是weak_ptr13.11#include <string>class HasPtr {public: HasPtr(const std
2017-02-04 19:18:58
431
原创 13.1.2
13.6 重载了operator=,对象是相同类型的运算符 当用一个对象对另一个对象赋值的时候使用 类似于基础类型的赋值,把符号右边的赋值给左边 当类内没有定义的时候,编译器会合成自己的版本13.7 会发生浅拷贝,这种情况是不允许的13.8#include <string>class HasPtr {public: HasPtr(const std::string &s = st
2017-02-04 18:35:36
272
原创 13.1.1
13.1 拷贝构造函数是一种特殊的构造函数,函数的名称必须和类名称一致,它必须的一个参数是本类型的一个引用变量 1. 对象以值传递的方式传入函数参数 2. 对象以值传递的方式从函数返回 3. 对象需要通过另外一个对象进行初始化 等13.2 要使用引用 如果不是引用的话,拷贝构造函数将调用本身,将是无限循环13.3 拷贝一个StrBlob会使shared_ptr计时器增1 拷贝一个S
2017-02-04 14:48:31
298
原创 12.3.2
12.30 http://blog.csdn.net/pypara/article/details/54847399 12.31 一个单词在行中出现多次,只列出一次 如果是vector则不满足12.32 … 12.33 ..
2017-02-03 15:56:55
322
原创 12.3.1
QueryResult.h#pragma once#include<memory>#include<set>#include<string>#include<vector>class QueryResult { using line_no = std::vector<std::string>::size_type;public: QueryResult(std::stri
2017-02-03 15:46:10
424
原创 12.2.2
12.26#include <iostream>#include <string>#include <memory>void input_reverse_output_string(int n){ std::allocator<std::string> alloc; auto const p = alloc.allocate(n); std::string s;
2017-02-02 20:16:42
236
原创 12.2.1
12.23 char *concatenate_string = new char[strlen("hello " "world") + 1](); strcat(concatenate_string, "hello "); strcat(concatenate_string, "world"); std::cout << concatenate_string << std::endl; d
2017-02-02 19:58:26
357
转载 12.1.6
12.19 @pezy//// ex12_19.h// Exercise 12.19//// Created by pezy on 12/26/14.//// Define your own version of StrBlobPtr and// update your StrBlob class with the appropriate friend declaratio
2017-02-02 16:32:37
308
原创 12.1.5
12.16 https://github.com/PYPARA/Cpp-Primer/blob/master/ch12/ex12_16.cpp12.17int ix = 1024, *pi = &ix, *pi2 = new int(2048);typedef std::unique_ptr<int> IntP;IntP p0(ix);// int 不能转换成std::unique_ptr
2017-02-02 16:19:28
402
原创 12.1.4
12.14 @pezy#include <iostream>#include <string>#include <memory>struct connection { std::string ip; int port; connection(std::string ip_, int port_):ip(ip_), port(port_){ }};struct des
2017-02-02 15:43:42
291
原创 12.1.3
12.10 我认为没有问题12.11process(std::shared_ptr<int>(p.get()));会出错,传递给process的是一个临时的智能指针(使用一个内置指针初始化),在函数结束,会被系统释放,从而使p指向的内存被释放了。12.12auto p = new int();auto sp = std::make_shared<int>();process(sp);//合法,
2017-02-02 14:53:07
319
原创 12.1.2
12.6#include<vector>#include<iostream>using std::vector;vector<int> * func() { auto p = new vector<int>; return p;}vector<int> * func2(vector<int> * p) { int in; while (std::cin >>
2017-02-02 13:58:09
274
原创 12.1.1
12.1b1有4个元素 b2已经不存在了12.2#include<vector>#include<string>#include<memory>class StrBlob {public: using size_type = std::vector<std::string>::size_type; StrBlob():data(std::make_shared<std::ve
2017-02-01 19:56:49
278
原创 HeapSort
MAX-HEAPIFY (A,i)1. l=LEFT(i)2. r=RIGHT(i)3. if l <= A.heap-size and A[l]>A[i]4. largest=l5. else largest=i6. if r <= A.heap-size and A[r]>A[largest]7. largest=r8. if largest!=i9.
2017-02-01 14:52:12
275
原创 11.4
11.37 http://www.cs.fsu.edu/~lacher/courses/COP4531/fall13/lectures/containers2/slide04.html11.38 @pezy #include <unordered_map>#include <set>#include <string>#include <iostream>#include <fstre
2017-01-31 15:13:24
297
原创 11.3.6
11.33#include<iostream>#include<fstream>#include<string>#include<sstream>#include<map>using std::ifstream;using std::getline;using std::string;using std::istringstream;using std::cout;using s
2017-01-31 14:28:17
283
原创 11.3.5
11.27 当我想知道知道,对应关键字的元素有几个的时候,我会使用count 当我只想知道它在不在的时候,我会使用find11.28std::map<std::string ,std::vector<int>> hh{ {"p",{0,1,2,3}} }auto iter= hh.find("p");11.29 lower_bound和upper_bound 会返回一样的迭代器,指向关键
2017-01-30 21:36:16
249
原创 11.3.4
11.24map<int, int> m;m[0] = 1;map中加入一个pair{ 0 , 1}11.25 vector<int> v; v[0] = 1;试图给v[0] 赋值,但是v[0] 根本不存在,所以行为未定义11.26std::map<int, std::string> m = { {0,"a" },{ 1,"b" } };可以用int来进行下标操作 下标操作返回一个strin
2017-01-30 20:37:29
311
原创 11.3.2
11.20#include <iostream>#include <map>#include <string>using std::string;using std::map;using std::cin;using std::cout;int main(){ map<string, size_t> counts; for(string word; cin >> word
2017-01-30 20:01:21
210
原创 11.3.1
11.15mapped_type : vector< int >key_type : intvalue_type : std::pair< int, vector<int> >11.16std::map<int, std::string> map;map[10010] = "132";std::map<int, std::string>::iterator it = map.begin();
2017-01-30 18:57:35
328
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人