Skip to content

Commit

Permalink
Add C++
Browse files Browse the repository at this point in the history
  • Loading branch information
zzicarus committed Feb 20, 2024
1 parent dd6a385 commit 7b0fc5d
Show file tree
Hide file tree
Showing 20 changed files with 312 additions and 35 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@

这里是我的个人笔记仓库

主要用来存储自己学习CS的一些笔记
主要用来存储自己学习CS的一些笔记

使用mkdocs搭建,主题为material for mkdocs

Pages: zzicarus.github.io

99 changes: 99 additions & 0 deletions docs/CodingLanguage/CPP/Stadard_Library/IO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
## IO类

- IO对象无赋值无拷贝
![alt text](images/custom-image.png)

### 条件状态

![alt text](images/custom-image-2.png)

#### 流的状态

`iostate`类型
![alt text](images/custom-image-3.png)

- 可以利用`good() fail()`判断整体流的状态

### 管理缓冲

!!! note "刷新缓冲的原因"
![alt text](images/custom-image-4.png)
<span class="box box-yellow">操纵符</span>

![alt text](images/custom-image-5.png)
```Cpp
cout << unitbuf; // 所有cout操作之后都刷新buffer
cout << nounitbuf;
```

**流的关联操作**

```c++
cin.tie(&cout);
// old_tie指向的是cin的关联流;转化后,cin不再关联
ostream *old_tie = cin.tie(nullptr);
cin.tie(&cerr);
cin.tie(old_tie);
```

## File

![alt text](images/custom-image-6.png)

```cpp
ifstream in;
ofstream out;
string filename("CppLearning");
in.open(filename + ".md");
if (in)
{ //...
}
in.close();
```
### 文件模式
![alt text](images/custom-image-7.png)
"../Stadard_Library/关联容器.md"
!!! note "注意"
以`out`方式打开文件,默认会覆盖
```cpp
ofstream out("filename",mode);
ofstream out("filename",ofstream::app | ofstream::out);
```

## String

![alt text](images/custom-image-8.png)

!!! example "Example"
使用`istringstream`读取,ostringstream输入
使用`ostringstream`可以在适当的时机输出所有值
```c++ hl_lines="7 10 12" title="istringstream"
string str, line;
vector<PersonInfo> People;
while (getline(cin, line))
{
PersonInfo Person;
string name, numbers;
istringstream iStr(line);
iStr >> name;
Person.name = name;
while (iStr >> numbers)
{
Person.Phones.push_back(numbers);
}
People.push_back(Person);
}
// print
for (auto person : People)
{
cout << "name:" << person.name << endl;
cout << "phones:" << endl;
for (auto phone : person.Phones)
cout << phone << endl;
}
```


Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
132 changes: 132 additions & 0 deletions docs/CodingLanguage/CPP/Stadard_Library/顺序容器.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
!!! abstract "Abstract"
一个容器是一些特定类型对象的集合。
所有容器类都共享公共的接口,不容的容器按照不同的方式进行拓展。<br>
**一般情况下,范围 range(a,b) 指的是[a,b)**

- 类型
![alt text](images/custom-image-9.png)

## 基本操作

![alt text](images/custom-image-10.png)

### 定义 初始化

```Cpp
// 返回反向容器
reverse_iterator
const_reverse_iterator
c.rbegin() c.rend()
```

### 赋值与swap

```c++
c1 = c2;
c = {a,b,c};
swap(c1,c2);
c1.swap(c2);
seq.assign(b,e); // b,e指向的不能是seq中的
seq.assign(initialList);
seq.assign(n,val);
```
> 除了数组之外,Swap操作不对数据进行拷贝、删除、插入,可以在常数时间内完成
## 顺序容器
### 操作
```c++
// 添加元素
c.push_back(t);
c.push_front(t);
c.emplace_front(args);
c.emplace_back(args);
//返回第一个新加入元素的迭代器
c.insert(position,n,val); // position 为 迭代器
c.insert(position,begin,end);
c.insert(position,initialList);
// 位置
c.at(n);
// 删除
c.pop_back();
c.pop_front();
c.erase(position);
c.erase(b,e);
// 改变大小
c.resize(n,optional::val);
// 容量大小
c.capacity();
c.size();
c.shrink_to_fit();
c.reserve(n);
```

!!! note "添加元素"
- emplace与push、insert的不同在于,emplace是通过元素的构造函数直接构造一个新的元素;而另外两者是拷贝的方式

#### forward_list

```c++
flits.before_begin();
flits.insert_after(...);
emplace_after(iter_position,args);
flits.erase_after(position);
flits.erase_after(b,e);
```
#### String
![alt text](images/custom-image-11.png)
```c++
// 构造函数
string s1(cp,n); // cp[n]之前
string s2(str,pos); // str[pos]之后
string s3(str,pos,len);
//
s.substr(pos,n); // position n个
// 插入 char / string
s.insert(nth,"");
s.append("");
s.replace();
```

!!! note "搜索操作"
注意,**find()函数的返回值是 `unsigned` 类型**,string::size_type npos = -1;
```c++
// 全匹配
s.find();
s.rfind(); // 最后一次出现
// 部分匹配
s.find_first_of();
s.find_first_not_of();
s.find_last_of();
s.find_last_not_of();
// example
cout << a.find(); // Not Found: 18446744073709551615
string str = "he is not here";
cout << str.find("he") << endl;
cout << str.substr(str.find("he"), sizeof("he"));
// example 选择匹配
string str = "11h23e55 i4456s88 n77o97t here!";
string numbers("0123456789");
string result("");
string::size_type pos = 0;
while ((pos = str.find_first_not_of(numbers, pos)) != string::npos)
{
result.push_back(str[pos]);
pos++;
}
cout << result;
```

### 适配器 | adaptor

> 适配器可以将一个类的接口转化为另外一个类的接口。
> 通过包装一个底层的类,对其接口进行二次处理,呈现出一种新的形式
> 一般情况下,`stack` `queue`基于`dequeue`实现,`priority_queue`基于vector实现(**也可以通过重载改变**,但需要满足适配器的特性)
> `stack<string, vector<string>> str_stk;`
38 changes: 14 additions & 24 deletions docs/CodingLanguage/CPP/test.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
#include <iostream>

#include <array>
#include <vector>
#include <fstream>
#include <list>
#include <forward_list>
#include <sstream>
#include <string>
#include <stack>
using namespace std;

std::string to_camel_case(std::string text)
struct PersonInfo
{
int flag_count = 0;
string result;
for (auto single : text)
{
if (single == '_' || single == '-')
flag_count = 1;
if (flag_count == 1)
{
if (isalpha(single))
{
result.push_back(toupper(single));
flag_count = 0;
}
}
else
{
result.push_back(single);
}
}
return result;
}
string name;
vector<string> Phones;
};

int main()
{
cout << to_camel_case("The_Formal_language");
int aaSSD_dawj = 5;
string asd;
}
Binary file modified docs/CodingLanguage/CPP/test.exe
Binary file not shown.
64 changes: 56 additions & 8 deletions docs/CodingLanguage/CPP/基础.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ int i = 0;
const int ci = i,&cr = ci;
auto b = ci; // b is int
auto c = cr;

```

#### decltype
Expand Down Expand Up @@ -273,16 +272,18 @@ vector<string> process()
## 迭代器

!!! note
begin end

返回值类型可能是 iterator或者

const_iterator

cbegin cend
begin end<br>
返回值类型可能是 iterator或者const_iterator

cbegin cend<br>
一定是const_iterator

- 迭代器类型

- 迭代器运算<br>
<span class="box box-green">string vector支持</span>
获取任意位置的iterator

## 数组

```CPP
Expand Down Expand Up @@ -730,3 +731,50 @@ inline Screen &Screen::set(char c)
可以使用`explicit`(只允许在类内出现 `explicit Sales_data(std::istream &)`)来限制隐式转换(但是仍然可以进行显式的转换)
![alt text](images/custom-image-14.png)
### 静态成员
> 与类相关,而不是与类的成员相关;一旦发生改变,所有的成员对象都能使用改变后的值
!!! note "note"
- 通常情况下,静态成员在类的外部进行初始化;但常量表达式情况是一个例外,`static constexpr int period = 30; double daily[period];`,一般情况下,我们还需要在类的外部对常量静态数据成员进行定义,`constexpr int Account::period;`
- 与非静态成员的区别
```c++ hl_lines="3 6"
class Bar{
public:
1. 可以作为默认实参
Screen &clear(char = bkground)
private:
2. 可以使用不完全类型定义
static Bar mem1;
Bar Wrong; // ×
static const char bkground;
}
```
```c++
// 声明
class Account {
public:
static double rate() {return interestRate;}
static void rate(double);
private:
std::string owner;
double amount;
static double interestRate;
static double initRate() { return .0225; }
static const std::string accountType;
}
// 使用
double r = Account::rate();
Account ac1;
Account &ac2;
ac1.rate();
ac2->rate();
// 定义
void Account::rate(double newRate) # (1)
{
interestRate = newRate;
}
```
7 changes: 5 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,12 @@ nav:
- ⌨️ CPP:
- CodingLanguage/CPP/index.md
- 基础: CodingLanguage/CPP/基础.md

- Stadard Library:
- IO库: CodingLanguage/CPP/Stadard_Library/IO.md
- 顺序容器: CodingLanguage/CPP/Stadard_Library/顺序容器.md
- 关联容器: CodingLanguage/CPP/Stadard_Library/关联容器.md
- ⌨️ Python:
- Python: CodingLanguage/Python/index.md
- CodingLanguage/Python/index.md
- 💡CS61A: CodingLanguage/Python/CS61A.md
- 杂谈:
- 杂谈/index.md
Expand Down

0 comments on commit 7b0fc5d

Please sign in to comment.