恋恋风辰的个人博客


  • Home

  • Archives

  • Categories

  • Tags

  • Search

C++ 函数模板和类模板

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

函数模板

当我们想要定义一个可以支持泛型的函数时,就要采用函数模板的方式了。所谓泛型就是可以支持多种类型的操作,比如我们定义一个compare操作,他可以根据传递给他的参数类型动态调用对应的函数版本,实现多种类型的比较。

1
2
3
4
5
6
7
8
9
template <typename T>
int compare(const T &v1, const T &v2)
{
if (v1 < v2)
return -1;
if (v2 < v1)
return 1;
return 0;
}

Read more »

C++ 类的继承封装和多态

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

C++ 特性

C++ 三大特性,封装继承多态。我们先实现一个Quote作为基类

Read more »

C++ lambda和function

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

lambda表达式

lambda表达式又称为匿名表达式,是C11提出的新语法。[]存储lambda表达式要捕获的值,()内的参数为形参,可供外部调用传值。lambda表达式可以直接调用

1
2
3
4
5
6
// 1  匿名调用
[](string name)
{
cout << "this is anonymous" << endl;
cout << "hello " << name << endl;
}("zack");

Read more »

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 »
12…22

恋恋风辰

重剑无锋,大巧不工

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