恋恋风辰的个人博客


  • Home

  • Archives

  • Categories

  • Tags

  • Search

C++ 运算符重载

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

本文介绍了C++ 运算符重载的用法,以我们构造的string类为例子,说明重载的用法。

构造我们自己的string类

声明如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class mystring_
{
public:
mystring_(/* args */);
mystring_(const mystring_ &mstr);
mystring_(const char *m_str);
mystring_(const string);
~mystring_();

friend ostream &operator<<(ostream &os, const mystring_ &mystr1);
mystring_ &operator=(const mystring_ &mystr);
mystring_ &operator=(string str);
mystring_ &operator=(const char *cstr);
friend mystring_ operator+(const mystring_ &str1, const mystring_ &str2);
char operator[](unsigned int index);
friend class mystringOpr_;

private:
char *m_str;
};
Read more »

C++单例模式的几种实现

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

本文介绍C++单例模式的集中实现方式,以及利弊

Read more »

二分查找升序序列

Posted on 2022-03-02 | In 数据结构和算法

问题描述

有一个连续的int数组,数组中的数据升序排序,数组中的数据不唯一,有重复数据,要求在数组中查找指定值为target的数据,返回target最小的下标,如果找到返回其最小的下标,如果没有找到,返回-1, 要求用 用二分查找的方式解决上述问题, 要求时间复杂度为Olog(n),空间复杂度为O(1)

举例 数组nums = [0,1,1,3,3,4], 查找target为1的数据最小的下标则返回1。如果查找target为100,则返回-1

解决思路

用二分查找解决上述问题,用一个数组中间的数据和target比较,如果中间值比target小,则说明target在中间值右侧。
如果中间值比target大,则说明target在中间值左侧。
如果中间值和target相等,则找到该target的位置,但是由于target不唯一,所以要向左移动,找到target最小的下标。

Read more »

C++ 面试常问问题(一)

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

这篇文章讲解C++ 面试常问的几个问题。本文通过demo讲解初始化列表,继承,字符串等常问问题。看下边这个例子

初始化列表

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
//基类
class Base
{
public:
Base() : m_nbase(0), m_nbase2(m_nbase + 100) {}
Base(int n) : m_nbase(n), m_nbase2(m_nbase + 100)
{
cout << "this is Base construct " << endl;
}
~Base()
{
cout << "this is Base destruct " << endl;
}

virtual void printData()
{
cout << "this is Base printData " << endl;
cout << "data is " << m_nbase << endl;
// cout << "base2 data is " << m_nbase2 << endl;
}

void printData2()
{
cout << "base2 data is " << m_nbase2 << endl;
}

int SizeOf(char p[])
{
return sizeof(p);
}

int SizeOf2(char *p)
{
return sizeof(p);
}

private:
int m_nbase;
int m_nbase2;
};

实现了一个类Base,类的构造函数采用了初始化列表,初始化列表按顺序初始化初始类成员。
接下来在main函数里调用如下

Read more »

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 »
<1…161718…37>

370 posts
17 categories
21 tags
RSS
GitHub ZhiHu
© 2025 恋恋风辰 本站总访问量次 | 本站访客数人
Powered by Hexo
|
Theme — NexT.Muse v5.1.3