恋恋风辰的个人博客


  • Home

  • Archives

  • Categories

  • Tags

  • Search

模拟实现vector

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

模拟vector

我们可以通过模板实现类似vector的类。我们实现一个StrVecTemp类,其内部通过allocator开辟空间,存储的类型用T来表示,T是模板类型。

Read more »

模板类型的原样转发和可变参数

Posted on 2022-06-16 | In C++

原样转发的意义

前文我们实现了一个my_move函数,用来模拟stl的move操作,实现去引用的功能。其内部的原理就是通过remove_reference实现去引用操作。
有时我们也需要保留原类型的左值或者右值属性,进行原样转发,此时就要用forward实现转发功能。
我们先定义一个模板函数

Read more »

C++ 模板类的友元和折叠规则

Posted on 2022-06-15 | In C++

为模板类声明友元类

有时我们需要A类访问模板类B的私有成员,但是不想其他类访问,就要在模板类B里为A类声明友元。比如我们想要实现一个BlobPtr类,让BlobPtr类成为Blob类的友元,这样BlobPtr类就可以访问Blob类了。对于Blob类的声明和定义在前文已经阐述https://llfc.club/articlepage?id=28Vv7hro3VVMPDepLTlLRLqYJhJ。
我们省略Blob类的详细声明,只为它添加友元类BlobPtr类,并且为他添加友元函数operator==

Read more »

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 »

12…22

恋恋风辰

重剑无锋,大巧不工

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