恋恋风辰的个人博客


  • Home

  • Archives

  • Categories

  • Tags

  • Search

hexoblog 搭建遇到的问题

Posted on 2022-03-01 | In 资源共享

需要js版本

js版本过高会导致调用hexo g 命令时出现问题。建议使用node.js 12以下版本,我用的是v12.18.3
历史版本下载可以参考如下链接
https://nodejs.org/en/download/releases/
如果想要管理多个版本的node,可以通过nvm管理,nvm下载和安装可以自行百度。

external_link 错误

我们可能会遇到如下问题,当我们执行hexo g 时

hexo版本更新报错:INFO Validating config WARN Deprecated config detected: “external_link“ with a Boolean
解决方案是
将原有的

1
2
external_link:
enable: true|false

修改为

1
2
3
4
external_link:
enable: true # Open external links in new tab
field: site # Apply to the whole site
exclude: ''

执行hexo clean ,然后再次hexo g 生成文章。

Read more »

C++ 右值引用与移动构造函数

Posted on 2022-02-10 | In C++

右值与右值引用

不能修改的值就是右值,右值一般为临时变量。常见的右值有字面常量值,返回右值的表达式。
所谓右值引用就是必须绑定到右值的引用。我们通过&&来获得右值引用。
右值引用有一个重要的性质——只能绑定到一个将要销毁的对象。
因此,我们可以自由地将一个右值引用的资源“移动”到另一个对象中。

Read more »

C++ 动态内存管理示例

Posted on 2022-02-09 | In C++

动态内存管理

之前我们讲述过动态内存的开辟,可以通过new, malloc,以及alloc等方式,本文通过介绍alloc方式,构造一个StrVec类,这个类的功能类似于一个vector,实现字符串的管理,其中包含push一个字符串,动态扩容,析构,回收内存等操作。

StrVec类实现细节

StrVec类实现如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class StrVec
{
public:
//无参构造函数
StrVec() : elements(nullptr), first_free(nullptr),
cap(nullptr) {}
//拷贝构造函数
StrVec(const StrVec &);
//拷贝赋值运算符
StrVec &operator=(const StrVec &);
//析构函数
~StrVec();
//拷贝元素
void push_back(const std::string &);
//返回元素个数
size_t size() const { return first_free - elements; }
//返回总容量
size_t capacity() const { return cap - elements; }
//返回首元素地址
std::string *begin() const
{
return elements;
}
//返回第一个空闲元素地址
//也是最后一个有效元素的下一个位置
std::string *end() const
{
return first_free;
}

private:
//判断容量不足开辟新空间
void chk_n_alloc()
{
if (size() == capacity())
reallocate();
}
//重新开辟空间
void reallocate();
// copy指定范围的元素到新的内存中
std::pair<std::string *, std::string *> alloc_n_copy(
const std::string *, const std::string *);
//释放空间
void free();
//数组首元素的指针
std::string *elements;
//指向数组第一个空闲元素的指针
std::string *first_free;
//指向数组尾后位置的指针
std::string *cap;
//构造string类型allocator静态成员
static std::allocator<std::string> alloc;
};

Read more »

C++类的拷贝控制demo

Posted on 2022-02-07 | In C++

拷贝控制

有时候我们需要两个类对象互相关联,当其中一个对象修改后也要关联修改另一个,用这个例子说明拷贝控制的案例。我们有两个类,Message类表示信息类,Folder类表示文件夹类,Message类里有成员folders表示其所属于哪些文件夹。Folder类有成员messages表示其包含哪些messages,所以Folder和Message之间是互相包含,多对多的关系。
同时我们要考虑Message类的拷贝,赋值,销毁等操作,如何同步处理其关联的Folder类。

Read more »

swap操作

Posted on 2022-01-28 | In C++

swap操作

我们常用的交换两个数据的操作是这样

1
2
3
4
5
6
void swap_int(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}

主函数调用是这样的

1
2
3
4
int a = 100, b = 200;
swap_int(a, b);
cout << "a is " << a << endl;
cout << "b is " << b << endl;

程序输出

1
2
a is 200
b is 100

Read more »

拷贝控制和资源管理

Posted on 2022-01-27 | In C++

拷贝控制

前文我们介绍了HasPtr类的拷贝控制,实现了行为像值的类,所谓行为像值的类就是我们所说的深拷贝,将一个类对象拷贝给另一个类对象时,其所有的成员都作为副本在新的类对象创建一遍,如果是指针类型的成员,则将指针指向的空间的数据复制一份给新对象,这样两个对象所有的成员都不关联,实现了深拷贝,不会受彼此影响。比如之前的HasPtr的拷贝构造

1
2
3
4
5
6
7
8
9
10
11
12
13
HasPtr::HasPtr(const HasPtr &hp)
{
cout << "this is copy construtor" << endl;
if (&hp != this)
{
this->m_str = new string(string(*hp.m_str));
int seconds = time((time_t *)NULL);
_curnum++;
this->m_index = _curnum;
}

return;
}

Read more »

C++ 拷贝构造 赋值 和析构

Posted on 2022-01-24 | In C++

拷贝构造函数

一个类可以不定义拷贝构造函数,系统会默认提供一个拷贝构造函数,叫做合成拷贝构造函数。与默认构造函数不同的是,即使我们定义了其他构造函数,系统也会为我们生成合成拷贝构造函数。合成的拷贝构造函数会将其参数的成员逐个拷贝到正在创建的对象中。编译器从给定对象中依次将每个非static成员拷贝到正在创建的对象中。对类类型的成员,会使用其拷贝构造函数来拷贝;内置类型的成员则直接拷贝。
为了方便举例,我们手动实现一个mystring类

1
2
3
4
5
6
7
8
9
10
11
12
13
class mystring_
{
private:
/* data */
public:
mystring_(/* args */);
mystring_(const mystring_ &mstr);
mystring_(char *m_str);
~mystring_();

private:
char *m_str;
};

Read more »

文本查询程序

Posted on 2022-01-21 | In C++

简介

本篇利用之前介绍的智能指针,关联容器,顺序容器等知识,做一个文本查询程序。该程序读取文本中的内容,然后根据输入的单词,判断该单词出现多少次,出现在哪些行,每行的句子是什么。比如我们输入elment,输出如下

1
2
3
4
5
6
element occurs 112 times
(line 36) A set element contains only a key;
(line 158) operator creates a new element
(line 160) Regardless of whether the element
(line 168) When we fetch an element from a map,we
(line 214) If the element is not found,find returns

Read more »

C++ 动态数组

Posted on 2022-01-20 | In C++

C++语言和标准库提供了两种一次分配一个对象数组的方法。C++语言定义了另一种new表达式语法,可以分配并初始化一个对象数组。标准库中包含一个名为allocator的类,允许我们将分配和初始化分离。使用allocator通常会提供更好的性能和更灵活的内存管理能力。

Read more »

智能指针 unique_ptr

Posted on 2022-01-19 | In C++

unique_ptr

unique_ptr和shared_ptr不同,unique_ptr不允许所指向的内容被其他指针共享,所以unique_ptr是不允许拷贝构造和赋值的。

1
2
3
4
5
6
7
8
9
10
11
void use_uniqueptr()
{
//指向double类型的unique指针
unique_ptr<double> udptr;
//一个指向int类型的unique指针
unique_ptr<int> uiptr(new int(42));
// unique不支持copy
// unique_ptr<int> uiptr2(uiptr);
// unique不支持赋值
// unique_ptr<int> uiptr3 = uiptr;
}

虽然我们不能拷贝或赋值unique_ptr,但可以通过调用release或reset将指针的所有权从一个(非const)unique_ptr转移给另一个unique:

Read more »

123…22

恋恋风辰

重剑无锋,大巧不工

216 posts
12 categories
16 tags
RSS
GitHub ZhiHu
© 2022 恋恋风辰 本站总访问量次 | 本站访客数人
Powered by Hexo
|
Theme — NexT.Muse v5.1.3