c++学习记录一:Fraction类

(本文来自《c++简明教程》)

Point:一个简单的类

在讨论Fraction类之前,先来看一个简单的类,以下是c++ class关键字的常规语法:

阅读更多

dockerNote

常用命令:

查看运行中的容器:
1
docker ps
停止运行中的容器,可以使用容器名或容器ID
1
docker stop static_web
重新启动关闭的容器
1
docker start 容器ID/容器名
附着到正在运行的容器
1
docker attach 容器ID/容器名
从容器中构建镜像

查找需要导出镜像的容器

image-20210708181544821

1
docker commit -m "lrr第一次test" -a "lrr" 13f79514a41e  ufoym/deepo:pytorch-py36-cu100

运行结果:

image-20210708182252743

导出镜像:
1
docker save -o LRR/docker/my_deeplearning_01 5ee3ae62a0eb

查看结果:

image-20210708183802897

推送镜像到dockerHub

若在push过程中出现:denied: requested access to the resource is denied

则需要将镜像的用户名和dockerhub的用户名保持一致,可以用docker tag进行修改

1
docker push brooke921090271/static_web

结果:

image-20210708184918475

为镜像添加别名

第一个参数为源,第二个参数为别名

1
docker tag  lrr01/static_web:latest brooke921090271/static_web

docker复制文件到容器

第一个参数为本机路径,第二个参数为容器名/容器ID:容器中的路径

1
docker cp Linda3615_requirements.txt myexp2:/root/Linda3615_requirements.txt

为正在运行的容器启动另一个终端

若对正在运行的容器进行attach,会进入同一个终端

1
docker exec -it myexp2 /bin/bash

启动深度学习镜像

1
docker run -it --gpus all ufoym/deepo:pytorch-py36-cu100 /bin/bash

docker run 参数

Run a command in a new container

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

  • -i:Keep STDIN open even if not attached,保证容器中STDIN是开启的
  • -t:Allocate a pseudo-TTY,分配为tty终端
  • --name:为容器分配名字

Example:

1
docker run -i -t ubuntu /bin/bash

开启以上两个参数才可以提供一个交互式的Shell

  • --gpus gpu-request: GPU devices to add to the container (‘all’ to pass all GPUs),这里可以用0,1,2选择

docker stop参数

Stop one or more running containers

Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

  • -t,Seconds to wait for stop before killing it (default 10),等待一段时间后杀死容器

docker commit 参数

Create a new image from a container’s changes

Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

  • -a:–author string
  • -m:–message string Commit message
  • -p:–pause Pause container during commit (default true)

docker save参数

Save one or more images to a tar archive (streamed to STDOUT by default)

  • -o:–output string Write to a file, instead of STDOUT

Longest Substring Without Repeating Characters

题目链接: https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
using namespace std;

int length(string s) {
vector<int> dict(256,-1);
int maxLen = 0, start = -1;
for(int i = 0; i != s.length(); i++) {
if(dict[s[i]] > start)
start = dict[s[i]];
dict[s[i]] = i;
maxLen = max(maxLen, i -start);
}
return maxLen;
}

int main()
{
cout<<length("abcabcbb")<<endl;
}

Output: 3

个人理解
dict 记录当前字符出现的位置
start 记录当前字符出现的上一次位置
参考自https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/1737

sql学习记录

实验一

参照实验指导书附录,建立数据库SCHOOL,分别建立students、teachers、courses、choices四张表。

  1. 用SQL创建数据库、建表。建表时为各表建立关键字、设置外码

    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
    create dabase db_school
    on primary
    (
    name='schoolDB_data',
    filename='E:\schoolDB_data.mdf',
    size=5mb,
    maxsize=100mb,
    filegrowth=15%
    )

    log on(
    name='schoolDB_log',
    filename='E:\schollDB_log.ldf',
    size=2mb,
    filegrowth=1mb
    )

    create table students(
    sid char(10) primary key,
    sname char(30) not null,
    email char(30),
    gread int
    )

    create table teachers(
    tid char(10) primary key,
    tname char(30) not null,
    email char(30),
    salary int
    )

    create table courses(
    cid char(10) primary key,
    cname char(30) not null,
    hour int
    )

    create table choices(
    no int primary key,
    sid char(10),
    tid char(10),
    cid char(10),
    score int,
    foreign key(sid) references students(sid),
    foreign key(tid) references teachers(tid),
    foreign key(cid) references courses(cid)
    )
  2. 为students表、courses建立按主键增序排列的索引

    1
    2
    create index stuid on students(sid)
    create index couid on courses(cid)
  3. 删除course上的索引

    1
    drop index courses.couid
  4. 在students表中增加一个“出生日期”字段,类型为日期时间型

    1
    2
    alter table students
    add "出生日期" datetime
  5. 删除students表中的出生日期字段

    1
    2
    alter table students
    drop column 出生日期
  6. 删除students表

    1
    drop table students

实验二 sql语句

  1. 查询年级为2001的所有学生的名称,按编号顺序排列

    1
    2
    3
    select sid,sname from students
    where grade = '2001'
    order by sid ASC
  2. 查询所有课程中含有data的课程编号

    1
    2
    select cid from courses
    where cname like 'data%'
  3. 统计所有老师的工资

    1
    select avg(salary) from teachers
  4. 查询至少选了3门课的学生编号

    1
    2
    3
    select sid from choices
    group by sid
    having count(*)>3
  5. 查询学号为80009026的学生的姓名、所选课名及成绩

    1
    2
    3
    4
    5
    select students.name, courses.cname, score
    from students, courses, choices
    where
    students.sid = choices.sid and choices.cid = courses.cid
    and choices.sid = '80009026'
  6. 查询没有学生选的课程编号

    1
    2
    3
    4
    5
    6
    select cid from courses
    where not exists
    (
    select courses.cid from courses, choices
    where choices.cid = courses.cid
    )
  7. 查询既选了C++又选了Java课程的学生编号

    1
    2
    3
    4
    5
    6
    7
    8
    9
    select sid from choices,courses
    where choices.cid=courses.cid
    and cname='C++'
    and sid in
    (
    select sid from choices,courses
    where choices.cid=courses.cid
    and cname='Java'
    )
  8. 查询选了C++但没选Java课程的学生编号

    1
    2
    3
    4
    5
    6
    7
    8
    9
    select sid from choices
    where
    cid = (select cid from courses where cname='C++')
    and
    sid not in
    (
    select sid from choices where
    cid = (select cid from courses where cname='Java')
    )
  9. 向students表中插入”LiMing”的个人信息(编号:700045678,名字:LiMing,Email:
    LX@cdemg.com,年级:1992)

    1
    2
    insert into students(sid, sname, email, grade)
    values('700045678','LiMing','LX@cdemg.com','1992')
  10. 将”LiMing”的年级改为2002

    1
    2
    update students set grade='2002'
    where sname='LiMing'
  11. 删除所有选了Java课程的学生选课记录

    1
    2
    3
    4
    5
    6
    delete from choices
    where cid in
    (
    select cid from courses
    where cname='Java'
    )
  12. 求出每门课的课程号、选课人数,结果存入数据库表T1中

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    create table T1(
    cid char(10),
    m int
    )

    insert into T1(cid, m)
    select courses.cid, count(sid)
    from courses, choices
    where courses.cod = choices.cid
    group by courses.cid
  13. 查询所有选课记录的成绩并换算为五分制

    1
    2
    select sid, cid, score/20 as score
    from choices
  14. 查询成绩小于80的选课记录,统计总数、平均分、最大值和最小值

    1
    2
    3
    select count(*) m, avg(score) avg_score, max(score) max_score,
    min(score) min_score from choices
    where score<80
  15. 按成绩顺序排序显示choices表中所有记录

    1
    2
    select * from choices
    order by score
  16. 创建视图V1,显示学生姓名、所选课名称、任课教师名

    1
    2
    3
    4
    5
    6
    7
    create view V1
    as
    select sname, cname, tname
    from students, courses, teachers, choices
    where students.sid = choices.sid
    and courses.cid = choices.cid
    and teachers.tid = choices.tid
  17. 取消V1视图

    1
    drop view V1

实验三 数据库完整性与安全性

  1. 创建一张新表class,包括Class_id(varchar(4)), Name(varchar(10)),Department(varchar(20))三个属性列, 其中Class_id为主码

    1
    2
    3
    create TABLE CLASS( Class_id varchar(4) primary key,
    Class_name varchar(10),
    Department varchar(20))
  2. 使用SQL命令创建一张学生互助表,要求:包括学生编号、学生姓名、学生帮助对象的编号,每个学生有且仅有一个帮助对象,帮助的对象必须是已存在的学生

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    create table help
    (
    sid char(10) primary key,
    sname char(10),
    help_sid char(10)
    );

    alter table help
    add constraint fk_help
    foreign key (help_sid)
    references help(sid);
  3. 使用STC数据库,修改设置,完成以下2种不同的要求,请写出对应的语句或简述操作过程:

    1. 当更新、删除STU表中记录时,若SC表中有此学生的记录,则拒绝
      1
      2
      3
      4
      5
      6
      alter table sc
      add constraint fuck
      foreign key (sno)
      references stu(sno)
      on update no action
      on delete no action
    2. 当更新、删除STU表中记录时,若SC表中有此学生的记录,则自动更新或删除
      1
      2
      3
      4
      5
      6
      alter table sc
      add constraint fuck2
      foreign key (sno)
      references stu(sno)
      on update cascade
      on delete cascade
  4. 使用sql命令完成以下任务

    1. 创建Worker表,定义约束U1、U2,其中U1规定Name字段取值唯一,U2规定sage字段上限是28

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      create table worker(
      number char(5),
      name char(8),
      sex char(1),
      sage int,
      deapartment char(20)
      );

      alter table worker
      add constraint U1 unique(name);
      alter table worker
      add constraint U2 check(sage <= 28);
    2. 去除U1约束,修改约束U2,令sage的值大于等于0

      1
      2
      3
      4
      5
      6
      7
      alter table worker
      drop constraint U1;
      alter table worker
      drop constraint U2;

      alter table worker
      add constraint U2 check(sage >= 0)
    3. 创建规则rule_sex,规定更新或插入的值只能是M或F,并绑定到Worker的sex字段

      1
      2
      3
      create rule rule_sex
      as @value in('M','F');
      sp_bindrule rule_sex, 'worker.[sex]';
  5. 创建触发器并测试

    1. 为worker表创建触发器T1,当插入或更新表中数据时,保证所操作记录的sage大于0

      1
      2
      3
      4
      5
      6
      create trigger T1
      on worker
      for update,insert
      as
      if (select sage from inserted) <= '0'
      rollback transaction
    2. 为worker表创建触发器T2,禁止删除编号为00001的记录

      1
      2
      3
      4
      5
      6
      create trigger T2
      on worker
      for delete
      as
      if (select number from deleted) = '00001'
      rollback transaction
    3. 为worker表创建触发器T3,要求更新一个巨鹿时,表中记录的sage要比老记录的sage值大

      1
      2
      3
      4
      5
      6
      create trigger T3
      on worker
      for update
      as
      if (select sage from inserted) < (select sage from deleted)
      rollback transaction

vim配置文件

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
set nocompatible              " required
filetype off " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
"call vundle#begin()

" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
"Plugin 'gmarik/Vundle.vim'

" Add all your plugins here (note older versions of Vundle used Bundle instead of Plugin)
"Plugin 'Valloric/YouCompleteMe'


" All of your Plugins must be added before the following line
"call vundle#end() " required
filetype plugin indent on " required

syntax on "自动语法高亮
set number " 显示行号
set cursorline " 突出显示当前行
set ruler " 打开状态栏标尺
set shiftwidth=4 " 设定 << 和 >> 命令移动时的宽度为 4
set tabstop=4 " 设定 tab 长度为 4
filetype plugin indent on " 开启插件
set backupcopy=yes " 设置备份时的行为为覆盖
set nowrapscan " 禁止在搜索到文件两端时重新搜索
set incsearch " 输入搜索内容时就显示搜索结果
set hlsearch " 搜索时高亮显示被找到的文本
set cmdheight=1 " 设定命令行的行数为 1
set laststatus=2 " 显示状态栏(默认值为 1, 无法显示状态栏)
set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ %c:%l/%L%)\
" 设置在状态行显示的信息
set foldenable " 开始折叠
set foldmethod=syntax " 设置语法折叠
set foldcolumn=0 " 设置折叠区域的宽度
setlocal foldlevel=1 " 设置折叠层数为
" set foldclose=all " 设置为自动关闭折叠
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>
" 用空格键来开关折叠
" return OS type, eg: windows, or linux, mac, et.st..
function! MySys()
if has("win16") || has("win32") || has("win64") || has("win95")
return "windows"
elseif has("unix")
return "linux"
endif
endfunction

"highlight Functions
syn match cFunctions "\<[a-zA-Z_][a-zA-Z_0-9]*\>[^()]*)("me=e-2
syn match cFunctions "\<[a-zA-Z_][a-zA-Z_0-9]*\>\s*("me=e-1
hi cFunctions gui=NONE cterm=bold ctermfg=blue

nmap <C-down> :resize +3<CR>
nmap <C-up> :resize -3<CR>
nmap <C-left> :vertical resize -3<CR>
nmap <C-right> :vertical resize +3<CR>
nmap <F5> :call RunTest()<CR>
nmap <F2> :call Save()<CR>
nmap <F3> :call Esc()<CR>
nnoremap <C-s> :call Save()<CR>
map <F9> :call Run()<CR>
func! RunTest()
exec "w"
exec "!./test.sh"
endfunc

func! Save()
exec"w"
endfunc

func! Esc()
exec "w"
exec "!clear"
exec "q"
endfunc

func! Run()
exec "w"
exec "!g++ -Wall -g % -o %<"
exec "! ./%<"
endfunc

" 当新建 .h .c .hpp .cpp .mk .sh等文件时自动调用SetTitle 函数
autocmd BufNewFile *.[ch],*.hpp,*.cpp,Makefile,*.mk,*.sh exec ":call SetTitle()"

" 加入注释
func SetComment()
call setline(1,"/*================================================================")
call append(line("."), "* Copyright (C) ".strftime("%Y")." brooke. All rights reserved.")
call append(line(".")+1, "* ")
call append(line(".")+2, "* 文件名称:".expand("%:t"))
call append(line(".")+3, "* 创 建 者:Brooke")
call append(line(".")+4, "* 创建日期:".strftime("%Y年%m月%d日"))
call append(line(".")+5, "* 描 述:")
call append(line(".")+6, "*")
call append(line(".")+7, "================================================================*/")
call append(line(".")+8, "")
call append(line(".")+9, "")
endfunc

" 加入shell,Makefile注释
func SetComment_sh()
call setline(3, "#================================================================")
call setline(4, "# Copyright (C) ".strftime("%Y")." brooke. All rights reserved.")
call setline(5, "# ")
call setline(6, "# 文件名称:".expand("%:t"))
call setline(7, "# 创 建 者:Brooke")
call setline(8, "# 创建日期:".strftime("%Y年%m月%d日"))
call setline(9, "# 描 述:")
call setline(10, "#")
call setline(11, "#================================================================")
call setline(12, "")
call setline(13, "")
endfunc


" 定义函数SetTitle,自动插入文件头
func SetTitle()

if &filetype == 'make'
call setline(1,"")
call setline(2,"")
call SetComment_sh()

elseif &filetype == 'sh'
call setline(1,"#!/system/bin/sh")
call setline(2,"")
call SetComment_sh()

else
call SetComment()
if expand("%:e") == 'hpp'
call append(line(".")+10, "#ifndef _".toupper(expand("%:t:r"))."_H")
call append(line(".")+11, "#define _".toupper(expand("%:t:r"))."_H")
call append(line(".")+12, "#ifdef __cplusplus")
call append(line(".")+13, "extern \"C\"")
call append(line(".")+14, "{")
call append(line(".")+15, "#endif")
call append(line(".")+16, "")
call append(line(".")+17, "#ifdef __cplusplus")
call append(line(".")+18, "}")
call append(line(".")+19, "#endif")
call append(line(".")+20, "#endif //".toupper(expand("%:t:r"))."_H")

elseif expand("%:e") == 'h'
call append(line(".")+10, "#pragma once")

elseif &filetype == 'c'
call append(line(".")+10,"#include \"".expand("%:t:r").".h\"")

elseif &filetype == 'cpp'
call append(line(".")+10, "using namespace std;")
call append(line(".")+10, "#include \""."iostream\"")

endif

endif
endfunc

简单的条件编译

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#define DEBUG 0
using namespace std;
int main()
{
#if DEBUG
cout<<"OK!"<<endl;
#endif
#if !DEBUG
cout<<"nice!"<<endl;
#endif
return 0;
}

当DEBUG为1时输出”OK!”,为0时输出”nice!”
即DEBUG为1时,6-8行参与编译,9-11行不参与,反之亦然。

python2.7安装mysqldb

sudo pip install mysql-python
安装成功后,就可以中python中导入MySQLdb

const限定符

有时我们希望定义这样一种变量,它的值不能被改变。例如,用一个变量来表示缓冲区的大小。使用变量的好处是当我们觉得缓冲区大小不再合适时,很容易对其进行调整。另一方面也应随时警惕防止程序一不小心改变了这个值。为了满足这一要求,可以用关键字const对变量的类型加以限定:

阅读更多

C++函数之指针形参

当执行指针拷贝操作时,拷贝的是指针的指。拷贝之后,两个指针是不同的指针。因为指针可以间接地访问它所指的对象,所以通过指针可以修改它所指的对象。

阅读更多