mirror of
https://github.com/Estom/notes.git
synced 2026-04-13 18:00:27 +08:00
C++补充
This commit is contained in:
@@ -27,7 +27,7 @@ mingw
|
||||
|
||||
## 1 C++程序构成
|
||||
|
||||
### 构成
|
||||
### 函数构成
|
||||
* 返回类型
|
||||
* 函数名
|
||||
* 形参列表
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
- 联合体`union`
|
||||
|
||||
|
||||
> **关于size_t的说明**:size_t是标准C库中定义的,在64位系统中为long long unsigned int,非64位系统中为long unsigned int。使用size_t可能会提高代码的可移植性、有效性或者可读性,或许同时提高这三者。因为其是基于平台变化的最大支持的无符号整型。
|
||||
|
||||
## 2 字面值常量
|
||||
|
||||
@@ -143,4 +144,4 @@ int f{3};//花括号列表初始化
|
||||
* 左值(lvalue):指向内存位置的表达式被称为左值(lvalue)表达式。左值可以出现在赋值号的左边或右边。
|
||||
* 右值(rvalue):术语右值(rvalue)指的是存储在内存中某些地址的数值。右值是不能对其进行赋值的表达式,也就是说,右值可以出现在赋值号的右边,但不能出现在赋值号的左边。
|
||||
|
||||
* 变量是左值,因此可以出现在赋值号的左边。数值型的字面值是右值,因此不能被赋值,不能出现在赋值号的左边。下面是一个有效的语句:
|
||||
* 变量是左值,因此可以出现在赋值号的左边。数值型的字面值是右值,因此不能被赋值,不能出现在赋值号的左边。
|
||||
@@ -63,7 +63,7 @@ int **p2 = &p1
|
||||
```
|
||||
int arr[10];//含有10个整型的数组
|
||||
int *arr[20];//含有20个整形指针的数组。指针数组
|
||||
int (*ptr)[10];指向含有是个整数的数组的指针。数组的指针(常量的指针和指针类型的常量类似)
|
||||
int (*ptr)[10];指向含有10个整数的数组的指针。数组的指针(常量的指针和指针类型的常量类似)
|
||||
|
||||
string arr[10];//含有10个string对象的数组C++11允许数组为对象类型。
|
||||
```
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class B{
|
||||
public:
|
||||
int a = 1;
|
||||
B():a(2){
|
||||
// a=3;
|
||||
}
|
||||
};
|
||||
int main()
|
||||
{
|
||||
// unsigned int a =1;
|
||||
@@ -21,11 +28,34 @@ int main()
|
||||
// cout<<*i<<endl;
|
||||
// cout<<*j<<endl;
|
||||
|
||||
extern int fff;
|
||||
fff=11;
|
||||
cout << fff << endl;
|
||||
// extern int fff;
|
||||
// fff=11;
|
||||
// cout << fff << endl;
|
||||
|
||||
// fff = 11;
|
||||
// cout << fff << endl;
|
||||
// int a = 10;
|
||||
// long long b = 10;
|
||||
// cout<<sizeof(a)<<"\t"<<sizeof(b)<<endl;
|
||||
|
||||
// B *b =new B();
|
||||
// cout<<b->a<<endl;
|
||||
// int m = 10;
|
||||
// int *a[3]={&m,&m,&m};
|
||||
// int b[]={1,2,3};
|
||||
// int (*ptr)[3]=&b;
|
||||
|
||||
// cout<<(*a)[7]<<endl;
|
||||
// cout<<(*ptr)[2]<<endl;
|
||||
|
||||
const char* m = "123";
|
||||
const void* n = "123";
|
||||
|
||||
char a[] = "123";
|
||||
char b[3] = "13";
|
||||
|
||||
cout<<sizeof(m)<<"\t"<<sizeof(n)<<endl;
|
||||
cout<<sizeof(a)<<"\t"<<sizeof(b)<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(){
|
||||
int i=0;
|
||||
while(i<10){
|
||||
i++;
|
||||
if(i>100){
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -202,8 +202,23 @@ public
|
||||
* 没有基类。
|
||||
|
||||
* 使用花括号括起来的成员初始值列表。并用它初始化聚合类的数据成员。
|
||||
```C++
|
||||
struct Person
|
||||
{
|
||||
std::string name;
|
||||
int height;
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
Person person={"xiaohong",10};
|
||||
std::cout << person.name<<" "<<person.height << std::endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
### 字面值常量类
|
||||
* 数据成员都是字面值类型的聚合类。是字面值常量类。
|
||||
* 数据成员必须都是字面值类型。
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
|
||||
### 申请内存
|
||||
|
||||
* new无法围棋分配的对象命名,而是返回一个指向该对象的指针。
|
||||
* new无法为其分配的对象命名,而是返回一个指向该对象的指针。
|
||||
```
|
||||
int * p = new int;
|
||||
```
|
||||
|
||||
@@ -96,6 +96,7 @@ resize(n,t) | 调整容器的大小为n个元素。任何添加的新元素初
|
||||
|
||||
### 特殊构造方法
|
||||
* 是静态的连续数组,只有默认初始化。
|
||||
* 元素类型和容量。是类型的一部分。
|
||||
```
|
||||
array<Type, Size> a;
|
||||
array<int,5> arr= {1, 2, 3, 4, 5};
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
## 0 简介
|
||||
|
||||
### 概念
|
||||
* 适配器 (adaptor) 是标准库巾的一个通用概念。容器、类和函数都有适配器。 本质上, 一个适配器是一种机制, 能使某种事物的行为看起来像另外一种事物一样。。一个容器适配器接受一种己有的容器类型, 使其行为看起来像一利1不同的类型。
|
||||
* 适配器 (adaptor) 是标准库的一个通用概念。容器、类和函数都有适配器。 本质上, 一个适配器是一种机制, 能使某种事物的行为看起来像另外一种事物一样。。一个容器适配器接受一种己有的容器类型, 使其行为看起来像一利1不同的类型。
|
||||
* 添加额外操作,实现某种特殊的数据结构。
|
||||
|
||||
### 容器适配器的操作
|
||||
|
||||
@@ -21,6 +21,11 @@ int main(){
|
||||
|
||||
// vector<int> vec;
|
||||
// 构造边的对象
|
||||
|
||||
vector<int> a{1,2,3};
|
||||
queue<int> b;
|
||||
cout<<b.front()<<endl;
|
||||
|
||||
struct Edge
|
||||
{
|
||||
int start;
|
||||
|
||||
@@ -100,7 +100,7 @@ int main(){
|
||||
|
||||

|
||||
|
||||
```
|
||||
```C++
|
||||
/*
|
||||
* 读取数据的三种方法
|
||||
* 直接使用输入输出流iostream
|
||||
|
||||
@@ -13,9 +13,35 @@ void unique_sort(vector<int> &words){
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(){
|
||||
vector<int> n{4,2,5,2,5,6,7};
|
||||
// 所以这万一是怎么记住上一个的状态的呢?
|
||||
// 查阅资料,发现这个函数是根据函数大小顺序生成下一个
|
||||
// 例如5,6,7的下一个是5,7,6
|
||||
// 但是7,6,5就没有下一个了,因为它已经是最大的排列了。
|
||||
// vector<int> n{4,2,5,2,5,6,7};
|
||||
vector<int> n {7,6,5};
|
||||
for(auto a :n){
|
||||
cout<<a<<" ";
|
||||
}
|
||||
cout<<endl;
|
||||
next_permutation(n.begin(),n.end());
|
||||
for(auto a :n){
|
||||
cout<<a<<" ";
|
||||
}
|
||||
cout<<endl;
|
||||
next_permutation(n.begin(),n.end());
|
||||
for(auto a :n){
|
||||
cout<<a<<" ";
|
||||
}
|
||||
cout<<endl;
|
||||
next_permutation(n.begin(),n.end());
|
||||
|
||||
// next_permutation(n.begin(),n.end());
|
||||
// next_permutation(n.begin(),n.end());
|
||||
for(auto a :n){
|
||||
cout<<a<<" ";
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
// for(int m :n){
|
||||
// cout<<m<<" ";
|
||||
@@ -27,12 +53,12 @@ int main(){
|
||||
// cout<<m<<" ";
|
||||
// }
|
||||
// cout<<endl;
|
||||
for_each(n.begin(),n.end(),[](int &a){
|
||||
a=1;
|
||||
return;
|
||||
});
|
||||
for_each(n.begin(),n.end(),[](int&a){
|
||||
cout<<a<<endl;
|
||||
});
|
||||
return 0;
|
||||
// for_each(n.begin(),n.end(),[](int &a){
|
||||
// a=1;
|
||||
// return;
|
||||
// });
|
||||
// for_each(n.begin(),n.end(),[](int&a){
|
||||
// cout<<a<<endl;
|
||||
// });
|
||||
// return 0;
|
||||
}
|
||||
@@ -5,6 +5,22 @@
|
||||
|
||||
> 参考顺序容器部分
|
||||
|
||||
* 访问元素
|
||||
* 也可以使用**迭代器**访问元素。
|
||||
* **at**会进行安全检查抛出异常。
|
||||
* **[]下标运算符**不会进行检查。
|
||||
* **back(),front()**
|
||||
* 添加元素
|
||||
* 总共有6+3=9种插入方法。insert有额外的两种范围插入方法。
|
||||
* 在尾部添加元素**push_back(),emplace_back()**
|
||||
* 在头部添加元素**push_front(),emplace_front()**
|
||||
* 在中间添加元素insert(),emplace()
|
||||
* insert方法提供了范围插入的方法。中间插入一个元素。在给定的一个迭代器之前插入一个值。中间插入多个元素。在给定的迭代器之前插入范围迭代器内的元素。
|
||||
* 删除元素
|
||||
* **back、front、push_back、push_front、pop_back、pop_front、emplace_front、emplace_back**。是一组首尾相关的插入操作。
|
||||
* **insert、emplace、at、erase**。是一组随机的操作。
|
||||
* 重构容器
|
||||
* 并非该表容器内存的大小。而是改变容器范围的大小。
|
||||
### 1.2 STL泛型算法
|
||||
* string 对象也可以看作一个顺序容器,它支持随机访问迭代器,也有 begin 和 end 等成员函数。STL 中的许多算法也适用于 string 对象。下面是用 STL 算法操作 string 对象的程序示例。
|
||||
|
||||
@@ -30,6 +46,11 @@ int main()
|
||||
|
||||
## 2.1 字符串创建
|
||||
* string 类有多个构造函数,用法示例如下:
|
||||
* 拷贝初始化
|
||||
* 序列初始化
|
||||
* 子串初始化
|
||||
|
||||
> 这三种初始化的恶魔是,在assign、find、append、replace、insert函数中,也在使用。就如同顺序容器中的初始化方法,在顺序容器的其他的操作中也是通用的。
|
||||
```C++
|
||||
string s1(); // si = ""
|
||||
string s2("Hello"); // s2 = "Hello"
|
||||
|
||||
@@ -4,23 +4,23 @@
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
// istringstream s("145,5343,42,44");
|
||||
// int a;
|
||||
// s>>a;
|
||||
// cout<<a<<endl;
|
||||
void test_seq(){
|
||||
cout<<"test_seq"<<endl;
|
||||
string s = "1234";
|
||||
s.push_back('5');
|
||||
cout<<s<<endl;
|
||||
s.insert(s.begin()+2,'0');
|
||||
cout<<s<<endl;
|
||||
cout<<s.size()<<endl;
|
||||
cout<<sizeof(s)<<endl;
|
||||
cout<<s.capacity()<<endl;
|
||||
s.pop_back();
|
||||
for(auto beg = s.begin(); beg!=s.end(); beg++){
|
||||
cout<<*beg<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
// string str="12345678";
|
||||
// cout<<str.erase(3,1)<<endl;
|
||||
// cout<<str<<endl;
|
||||
|
||||
// char* helo;
|
||||
// s.getline(helo,10,',');
|
||||
// size_t m;
|
||||
// string s="123avicfee";
|
||||
// int a = stoi(s,&m,10);
|
||||
// cout<<a<<endl;
|
||||
// cout<<m<<endl;
|
||||
void test_find(){
|
||||
|
||||
string strs =" adb dai fei af";
|
||||
|
||||
@@ -29,8 +29,8 @@ int main(){
|
||||
size_t end_pos = strs.find(pattern,start_pos);
|
||||
vector<string> vec;
|
||||
string temp;
|
||||
while (end_pos != string::npos)
|
||||
{ cout<<start_pos<<"--"<<end_pos<<endl;
|
||||
while (end_pos != string::npos){
|
||||
cout<<start_pos<<"--"<<end_pos<<endl;
|
||||
if(start_pos!=end_pos){
|
||||
temp = strs.substr(start_pos,end_pos-start_pos);
|
||||
vec.push_back(temp);
|
||||
@@ -43,5 +43,35 @@ int main(){
|
||||
for(auto a : vec){
|
||||
cout<<"name:"<<a<<":end"<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
void test_substr(){
|
||||
istringstream s("145,5343,42,44");
|
||||
int a;
|
||||
s>>a;
|
||||
cout<<a<<endl;
|
||||
|
||||
string str="12345678";
|
||||
cout<<str.erase(3,1)<<endl;
|
||||
cout<<str<<endl;
|
||||
|
||||
char* helo;
|
||||
s.getline(helo,10,',');
|
||||
size_t m;
|
||||
// string s="123avicfee";
|
||||
// int a = stoi(s,&m,10);
|
||||
cout<<a<<endl;
|
||||
cout<<m<<endl;
|
||||
|
||||
}
|
||||
void test_contruct(){
|
||||
string a("12345",2,2);
|
||||
cout<<a<<endl;
|
||||
}
|
||||
int main(){
|
||||
// test_contruct();
|
||||
// test_seq();
|
||||
test_find();
|
||||
// test_substr();
|
||||
return 0;
|
||||
}
|
||||
@@ -13,10 +13,10 @@ void doSomething(promise<string>& p){
|
||||
cout<<"read char x for exception"<<endl;
|
||||
char c = cin.get();
|
||||
if(c=='x'){
|
||||
throw runtime_error(string("char")+c+"fault");
|
||||
throw runtime_error(string("char ")+c+" fault");
|
||||
}
|
||||
else{
|
||||
string s = string("char")+c+"correct";
|
||||
string s = string("char ")+c+" correct";
|
||||
p.set_value(move(s));//移动赋值函数。防止退出局部变量后销毁。
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,19 @@
|
||||
|
||||
### 知识复习——语言
|
||||
* [ ] C++
|
||||
* [ ] 基础知识
|
||||
* [x] 基础知识
|
||||
* [ ] 标准库
|
||||
* [ ] 面向对象
|
||||
* [ ] 设计模式
|
||||
* [ ] 并行编程
|
||||
* [ ] 并行编程(并发和多线程)
|
||||
* [ ] 网络编程
|
||||
* [ ] web开发
|
||||
* [ ] GO
|
||||
* [ ] 基础知识。
|
||||
* [ ] go语言的优势。
|
||||
* [ ] 协程并发编程等,相关内容的了解。
|
||||
* [ ] 网络编程
|
||||
* [ ] web开发
|
||||
* [ ] 数据结构
|
||||
* [ ] 算法
|
||||
### 知识复习——基础
|
||||
|
||||
Reference in New Issue
Block a user