分类 个人 下的文章

daisyUI

https://daisyui.com daisyUI — Tailwind CSS Components WebdaisyUI is a plugin for Tailwind CSS. It works on all JS frameworks and doesn't need a JS bundle file. Install daisyUI as a dev dependency and use the class names just like any …

Chakra UI

https://chakra-ui.com Chakra UI - A simple, modular and accessible component … WebCreate accessible React apps. with speed. Chakra UI is a simple, modular and accessible component library that gives you the building blocks you need to build your React applications.

mantine (UI)

https://mantine.dev/

Global web icon

react-icons.github.io https://react-icons.github.io/react-icons React Icons - GitHub Pages WebReact Icons. Include popular icons in your React projects easily with react-icons, which utilizes ES6 imports that allows you to include only the icons that your project is using. …

tabler-icons

https://tabler-icons.io/

Over 4850 pixel-perfect icons for web design Free and open source icons designed to make your website or app attractive, visually consistent and simply beautiful.

Introduction to Immer

GitHub Pages https://immerjs.github.io › immer Immer (German for: always) is a tiny package that allows you to work with immutable state in a more convenient way. ‎React & Immer · ‎Using produce · ‎Supporting immer · ‎Installation

hooks

immer

https://zhuanlan.zhihu.com/p/639089491

  1. use-immer库的高级用法 下面我们将介绍use-immer库的高级用法,包括:

3.1 使用produce函数 produce函数是use-immer库中最重要的函数之一。它可以帮助我们更加方便地管理和更新JavaScript对象和数组。下面是一个简单的例子:

import { produce } from 'immer';
const state = {
  todos: [
    { id: 1, text: 'Learn React', completed: true },
    { id: 2, text: 'Learn use-immer', completed: false },
  ],
};
const nextState = produce(state, draftState => {
  draftState.todos.push({ id: 3, text: 'Learn Redux', completed: false });
});
console.log(nextState.todos);

在上面的例子中,我们使用了produce函数来更新state中的todos数组。我们可以看到,使用produce函数可以帮助我们更加方便地更新JavaScript对象和数组。


react snippets

a quick way to create a code template for a component or etc...

rcc

refs

ref is a reference to a element but ref maybe null

react-hook-form

transform several properties

const obj = {
    className: "none",
    size: 14,
    color: "red"
}

<Comp
    { ... obj }
/>

equals

<Comp
    className={obj.className}
    size={obj.size}
    color={obj.color}
/>

... means    loop the obj and set each key to the value

zod , joi , ... validation libs

import { z } from 'zod';

// create the limites
const schema = z.object({
    name: z.string().min(3, {"message":"at least 3 characters."}),
    age: z.number({invalid_type_error:"required"}).min(18) // the api shoud be mobile and ...
})

// create the  interface for data
type FormData = Formz.infer<typeof schema>;
@hookform/resolvers
const {
    register,
    handleSubmit,
    formState: {errors,isValid}
} = 
useForm<FormData>( { resolver: zodResolver(schema) } );
useEffect()

useEffect is similar to a "OnRendered" Callback when JSX modules is rendered then react call this function

finally()

the finally method will be called both succeed or failed when fetch on axios

?. optional link

seemly to swift syntax

obj?.type means obj && obj.type

Abort Signal

Mozilla Developer https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal AbortSignal - Web APIs | MDN WebAug 4, 2023 · English (US) AbortSignal. The AbortSignal interface represents a signal object that allows you to communicate with a DOM request (such as a fetch request) and abort it …

const controller = new AbortController();
const signal = controller.signal;

const url = "video.mp4";
const downloadBtn = document.querySelector(".download");
const abortBtn = document.querySelector(".abort");

downloadBtn.addEventListener("click", fetchVideo);

abortBtn.addEventListener("click", () => {
  controller.abort();
  console.log("Download aborted");
});

function fetchVideo() {
  fetch(url, { signal })
    .then((response) => {
      console.log("Download complete", response);
    })
    .catch((err) => {
      console.error(`Download error: ${err.message}`);
    });
}

Array.reduce()

Mozilla Developer https://developer.mozilla.org/.../Array/Reduce Array.prototype.reduce() - JavaScript | MDN - MDN Web … WebSep 14, 2023 · Array.prototype.reduce () Try it. The reducer walks through the array element-by-element, at each step adding the current array value to the... Syntax.

JavaScript Array filter()

WebThe filter () method creates a new array filled with elements that pass a test provided by a function. The filter () method does not execute the function for empty elements.

随着智能化在生活的方方面面深入,家电设备上的智能化需求也是在不断增长。 同时伴随着芯片行业的飞速发展、芯片的国产化进程不断推进,在家用电器上内嵌一块显示屏、并且提供智能化的设备服务已经成为一个行业趋势。

本文从需求、行业特性、技术架构几个角度试图探索、分析和预测一下家电行业的智能化趋势。


需求:智能家电的本质是什么?

科技的世俗目的是扩展人类的能力边界,而计算机则是大大降低了人脑在记忆、数据处理方面的心智成本,“智能”则是站在体验的角度,基于一个高性能设备,通过各类交互和流程设计,令计算机(设备)的使用更符合人类的直觉感观体验,能够让用户更加“随心所欲”操控计算机的概括。

a. 其智能化的程度主要体现在用户通过直觉体验能够对于设备操作的精细度以及完成复杂的组合式目标的便捷和直观程度上。

比起智能手机、汽车与家电的相似性更高,都是在可以完全通过机械操作完成设备的工作。其智能化的程度主要体现在用户通过直觉体验能够对于设备操作的精细度以及完成复杂的组合式目标的便捷和直观程度上。(这里的智能实际上是设备本身替代和减少人类思考决策和操作而形成的机器智能的感观体验,而并不是设备本身一定具有切实的AI能力)

- 阅读剩余部分 -

C++中的基本数据类型定义没有最终的规定,由编译系统自行确定。

但是一些关系已经确定

长整形 不小于整形

短整形 不大于整形

一般16位机C++系统中,short int,int 2个字节,long int 4个字节 VC++中,short 2个字节,int,long int 4个字节

一个字节是计算机中的8个bit位 一个比特位就是硬件中的一个逻辑单元 可以表示0 或者1 所以一个字节就是 00000000 一个字节最大值就是 11111111 换算成10进制就是 1+2+4+8+16+32+64+128 = 255

两个字节就是 00000000 00000000 最大值是 11111111 11111111 => 1+2+... 2^15 = 65535

这里另外需要考虑一个问题就是符号,如果将刚才的范围的第一个比特位用作符号表示的话,那么一个字节的范围就是 1 0000000 - 1 11111110 0000000 - 0 1111111-128 -> -1,0 -> 127

这里的负数比正数多一个原因在于 补码机制

无符号,有符号 位数一致,无符号 绝对值大一倍(但没有负数)

基本关系: boolean = char < short <= int <= long <= float < double

Bool实际上需要的是最少的,只需要0,1但是最低的位数也是1字节 char也是1字节 255的范围用于表示基本英文字母和基础符号足够了

浮点数在计算机的表示方法

loat规格float共计32位,4字节由最高到最低位分别是第31、30、29、……、0位,则:31位是符号位,1表示该数为负,0表示为正。30-23位,一共8位是指数位。22-0位,一共23位是尾数位。3、转换例子按照IEEE浮点数表示法,将float型浮点数123456.0f转换为二进制(注:这里的f表示浮点数,为十进制数,不是表示16十六进制)。处理不带小数的浮点数时,直接将整数部转化为二进制表示:11110001001000000也可以这样表示:11110001001000000.0然后将小数点向左移,一直移到离最高位只有1位:1.11100010010000000共左移了16位,所以原数就等于:1.11100010010000000*(2^16)。

其实简单来说浮点数就是三个部分,位数0、小数点位置(二进制) 1-8 、整体数值二进制表示 9-31


前言 Why

macOS在某个版本改版之后,对于文件的权限系统做了升级,同时开启了一个SIP保护功能,导致了基于之前一直的习惯(macos自带的Apache,php)在使用的时候会有诸多阻碍。譬如说,安装一个php扩展的时候,就会遇到各种各样的问题,安装过程不能顺利进行。 类似于: PHP 安装扩展报错 grep: /usr/include/php/main/php.h: No such file or directory 包括我们要在www目录下做修改,也不是那么方便。

基于brew,可以傻瓜式的安装和配置好nginx+php开发环境,之所以选择nginx环境,因为生产环境中也是使用的nginx,保持统一比较方便。


安装酒桶 install homebrew

https://brew.sh https://github.com/homebrew/install#uninstall-homebrew

install

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

uninstall

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh)"

不建议换镜像/源,换了之后可能会出现无法正常使用的问题,我自己就遇到了,重新安装折腾了我一夜。

最好是使用高速稳定的VPN下载官方源。

下载时总是出现 fetch failed , early EOF 这样的错误。

安装core的时候比较容易出现这个问题,因为仓库整体很大,所以经常会因为网络波动而中断,我参考了网上很多尝试解决的方式都无效,比如说设置postBUFFER, packalimit之类的。

我的解决办法是,使用git clone命令,先将仓库克隆到用户文件夹下,之后删除(替换)brew目录下面的 homebrew-core目录。

git proxy

对git使用代理

vi ~/.gitconfig

添加代理配置 一般代理配置的地址和端口号在代理的说明中会有。

[http "https://github.com"]
    proxy = 127.0.0.1:19180

安装NGINX install nginx

brew install nginx

Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:

brew services start nginx

Or, if you don't want/need a background service you can just run: nginx

补充

#测试配置是否有语法错误
nginx -t

#打开 nginx
brew services start nginx

#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit

多个网站的配置

/usr/local/etc/nginx/servers/ 在这个目录下,新建多个需要的 xxx.conf nginx会加载所有的配置文件。


安装PHP install php

通过brew安装php,如7.4

brew install php@74

安装完成后会提示:

To enable PHP in Apache add the following to httpd.conf and restart Apache: LoadModule php7_module /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Finally, check DirectoryIndex includes index.php DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in: /usr/local/etc/php/7.4/

php@7.4 is keg-only, which means it was not symlinked into /usr/local, because this is an alternate version of another formula.

复制提示的代码,将新安装的php设为环境变量:

If you need to have php@7.4 first in your PATH, run:

echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc

补充 (source才能生效)

source ~/.zshrc
php -v

For compilers to find php@7.4 you may need to set: export LDFLAGS="-L/usr/local/opt/php@7.4/lib" export CPPFLAGS="-I/usr/local/opt/php@7.4/include"

To have launchd start php@7.4 now and restart at login: brew services start php@7.4 Or, if you don't want/need a background service you can just run: php-fpm

nginx - php

安装完php后,需要修改nginx的配置来启用php

如果不需要单独配置多个服务器,直接修改nginx.conf即可,如果需要多个,则在servers文件夹下,新建单独的xx.conf文件。

conf文件模板为

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        root /Users/mac/dev/web_server/qiyouyun;
        location / {
            index  index.html index.htm index.php;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
}

如果打开测试的php时看到,file not found。很大可能是root目录配置错误。 特别是nginx.conf中,分别需要对 .php和默认的 root设置。

忽略其中一个可能就造成找不到文件。 权限问题可能性不大,不过如果确认目录没问题,可以考虑检查一下权限。

配置完成后,重新加载nginx的配置

nginx -s reload

安装php库管理工具 composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
composer -v
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.0.11 2021-02-24 14:57:23

安装扩展 extensions for php

php-zip

下载,或使用wget

wget http://pecl.php.net/get/zip
cd zip-1.19.2
ls

查看一下包是否已经解压,能否ls的时候看到里面的文件结构,如果还是一个目录文件,则再进入,知道可以看到含有install.sh这样的文件。

phpize

Configuring for:
PHP Api Version:         20190902
Zend Module Api No:      20190902
Zend Extension Api No:   320190902

查看一下本地php-config的所在目录

which php-config

/usr/local/opt/php@7.4/bin/php-config

对于当前正在使用的版本进行配置

./configure --with-php-config=/usr/local/opt/php@7.4/bin/php-config
make
............
make install
............


cp ./.libs/zip.so /Users/mac/env/zip-1.19.2/zip-1.19.2/modules/zip.so
cp ./.libs/zip.lai /Users/mac/env/zip-1.19.2/zip-1.19.2/modules/zip.la
----------------------------------------------------------------------
Libraries have been installed in:
   /Users/mac/env/zip-1.19.2/zip-1.19.2/modules

这里的 zip.so是很重要的,开启扩展的时候需要用到。

编辑php.ini

在php.ini中,添加一行扩展信息。(建议添加在 extensions部分)

;extension=pdo_odbc
;extension=pdo_pgsql
;extension=pdo_sqlite
;extension=pgsql
;extension=shmop
extension=/Users/mac/env/zip-1.19.2/zip-1.19.2/modules/zip.so

php-redis

因为一些学习和研究目的,最近在写一些数据抓取的组件,在网页上很常见的是相对链接,有时候因为所在网页和相对链接的关系不太确定,所以就需要转换一下,本来这个功能实在太简单,直接在网上搜索了一下,但是发现绝大部分代码都是错的,或者说不严谨,随便改一个目录深度就会发生错误。 这里贴一下我的解决方案:

<?php


class spider{

/*
$rel            string      相对链接
$baseURL    string      当前所在页面完整地址
*/
    public function absoluteURL( $rel, $baseURL ): string
    {
        // 忽略绝对地址
        if ( strstr( $rel, 'https:/') || strstr($rel,'http:/') || strstr($rel,':/') ){
            return $rel;
        }

        // 结构化当前URL
        $url = parse_url($baseURL);

        $rel = trim($rel);

        $depthPath = [];
        foreach ( explode('/',$url['path']) as $i => $p ){
            if( $p != '' ){
                $depthPath[] = $p;
            }
        }
        $pathDeep = count($depthPath);

        $relDepth = [];
        $rootPath = false;
        $backPathDepth = 0;

        if( strstr($rel,'/') ){

            foreach ( explode("/",$rel) as $i => $r){
                if( $i==0 && $r == '' ){ // 直接根目录
                    $rootPath = true;
                }
                if( $r != '' ){
                    $relDepth[] = $r;
                }
                if( $r === '..' ){
                    $backPathDepth++;
                }
            }
        }else{
            $relDepth = [$rel];
        }

        $new_url = $url['scheme'] . '://' . $url['host'];

        if( !$rootPath ){
            for( $i = 0; $i < $pathDeep - $backPathDepth; $i++ ){

                $new_url .= ('/' . $depthPath[$i]);
            }
        }

        for ( $i = 0; $i < count($relDepth); $i ++ ){

            if( $relDepth[$i] !== '..' && $relDepth[$i] !== '.' ){
                $new_url .= ('/' . $relDepth[$i]);
            }
        }

        return $new_url;
}

版本管理在编程中的重要程度不言而喻,其中git工作流也是最主流的方式,接下来总结一下git工作流中的一些比较实用的概念和具体方法。

在实际使用中,我还是用图形软件 sourcetree为主,不过图形软件只是为了方便,并且有很多用法还是要实用命令行来解决,所以要先理解概念,再熟悉命令,最后使用工具。

最常规的几个命令 init, add, rm, status, diff, commit 分别用来 新建仓库、添加、删除、查看概览、比较更改,提交更改。 基本上有这几个命令就可以顺利进行本地仓库的“备份”了。 clone, pull, push 是基于网络管理仓库比较常用的命令,用于 复制仓库,拉取更新,推送更新到服务器。

在git工作流中,协作的重要性是很高的,随着项目规模的升级,以及更多的人使用项目(fork),基于协作的共同维护就很有意义了。

这里主要有两个协作方式 1. 成为维护开发者 2. 创建分支、提交推送

第二种方式,不仅可以用于为源仓库贡献代码,也可以作为“定制化”开发的一种可行途径。这时候如果觉得自己开发的某些代码对于源仓库也有价值,可以再考虑贡献回去。

在github中,成为协作者主要是使用invitation功能,成为维护开发者之后,就可以和创建人一起管理仓库了。

当没有足够认可成为维护开发者,或者只是希望做一些定制化开发留为己用的时候呢,可以使用GitHub的fork功能。

这里我设计了一张图来诠释fork时,repo之间的关系。

在fork之后,实际上我们不必把自己的仓库当成是树枝,当我们创建完分支后,两个仓库已经是对等的了。我们可以向源仓库推送更新,也可以把源仓库的更新当做推送方,合并到自己的仓库中。

在github中,两个仓库之间的拉取是很简单的,无论是希望推送,还是希望从源仓库更新都适用这个拉取。 如果是希望更新就将两个仓库的顺序对转然后进行对比。

之后就根据需要进行合并操作就可以了。

如果是贡献代码,那么需要源仓库开发者通过并且选择再合并。我们更新则是自己来通过。


移除所有记录中的文件

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch THE_FILE_PATH' --prune-empty --tag-name-filter cat -- --all
git push --force

Swift泛型

swift 泛型使用 <Type> 来声明

func plus<Number>( _ a:Number, _ b:Number ) -> Number{
    return a+b
}

可以同时声明多个无关的泛型,使用,分割 <TypeA, TypeB>

Swift 函数定义

(Int,Int) -> Bool
(Double) -> Void // (Double)
() -> Array<String>
() -> Void

var foo: (Double) -> Void

func doSometing( what: () -> Bool )

Swift 中 struct 与 class的区别

struct class
Value Type 值类型 Reference Type 引用类型
在swift中是 copy on write 任何时候都是传递指针
Funcional-programing 函数式编程 Object-programing 面向对象编程
不能继承 可以继承
可变性需要被精确描述 默认可变

private(set)

private(set) 表达只在set时处于private 而可以正常读取 这样就避免了大量写 set,get

Identifiable

在swift 使用 ForEach 迭代的时候,需要迭代体内的元素是可以迭代的(每个元素要有可唯一区别性)这时候,可以让元素继承(接受) Identifiable接口

struct student: Identifiable{
    id:Int,
    name:String
}

ForEach( students ){ index in
    // ...
}

Php8在性能上有了一定的提升,接下来看一下对于7.x的版本迁移有那些需要注意的,新版本带来的新特性有哪些适用性。

新特性的介绍源于 php官方文档: Php8

named arguments

命名属性

推荐 好处不用多说了,语法能力提升,自然编程的自由度,便捷度也更好

这一项在面向对象语言中比较常见,类似于C++中的重载就允许实现类似的作用,但是C++的重载实现的能力更强一些,在swift中也是有类似的语法实现。 这个更新总体来说是预言特性上的补足,在7X版本中虽然IDE可以补充参数名显示,但是参数本身是有强制顺序的(如果写了最后一个参数,那么中间所有参数都必须补全),对于有写面向对象语言习惯的人来说这一点应该是比较实用,方便的。


function testArguments( $name, $age = 18, $gender = 1, $isStudent = false ){
}

// php7
// 如果中间两项都是默认,也必须提供参数(NULL)
testArguments( "jack", null, null, true );

// php8
// 可以直接跳过使用默认值的参数
testArguments( "jack", isStudent: true );

Attributes

属性?

也就是说以php官方提供的形式来进行文档注释

不推荐 phpDoc注释显然比官方提供的注释要臃肿,但可读性也更好,一味的简洁不太明智

Constructor property promotion

构造函数中定义属性?

从描述来看,其实是给了一个默认属性和构造函数简便的写法。

对比C++和swift来说,这个增强只能说聊胜于无,因为他并没有直接解决类属性的默认值问题。 而是把默认值的定义放在构造函数中,也就真的和官方说明一样,少写几个字而已。

- 阅读剩余部分 -

在做习题的时候出现了一个小纰漏,原因是想当然的把 ƒ²(x) 的导数当成了 x²的导数。 从原理上来说 ƒ²(x) 应该当作 ƒ(x) 的复合函数来求导,也可以当作是 ƒ(x) * ƒ(x) 来计算。

ƒ(x),g(x)可导,ƒ²(x)+g²(x) ≠ 0,求

$$ y= \sqrt {f^2(x)+ g^2(x)} $$

的导数。

另外就是 e2t 的导数求法了,这也是很容易就疏忽写错的。 每次求导一定要注意,前一层复合函数中作为主变量在后一层中,是否是一层函数。

(e2t)' = e2t * (2t)' = 2e2t

课程出处

P16 常用求导公式 -《高等数学》同济版 全程教学视频(宋浩老师)

视频中 22分钟处有一个关于 x的u次幂求导公式,其中证明步骤跳过了后面的步骤,当时看的时候有些疑问,经过一些尝试总算得出有效递推过程。

当时的疑问主要是 第二行的部分 h/x 既然趋向于0 那么 (1+0)的u次方 - 1 也趋向于零,所以结果应该是0

幂函数求导

谭浩强 C++程序设计(第三版)P189 第16题

输入一个字符串,内有数字和非数字字符,如

a123x456_17960?302tab5876

将其中连续的数字作为一个整数,依次存放到一个数组a中。统计总共有多少个整数,并输出这些数。

这个问题是比较好解决的,主要是三步

  1. 开辟一个 int a[(n+1)/2]; 大小的整数数组a,(n+1)/2 是字符串中能够包含的至多个整数了。 初始化一个数字统计 int total = 0;,用来累计出现过的数字总数。

  2. 遍历字符串,比对是否是数字,如果是 压入栈中,如果不是,将栈逐步清空并将取出的若干个数字计算为十进制数,其中每次出栈,将进制+1,则可以顺利求出。 每次得出一个新整数,total++

  3. 以total为终,遍历a并输出。

#include <iostream>
#include <stack>

using namespacing std;

int getNumberFromStack( stack &s ){ // 传递引用

    int level = 1;
    int number= 0;

    while( !stack.empty() ){

        number += stack.top() * level;

        stack.pop();
        level *= 10;
    }
    return number;
}

int main(){

    string s;
    cout << "请输入一个字符串,如a123x456_17960?302tab5876。\n";
    cin >> s;

    int a[ (s.size() + 1)/2 ];
    int total = 0;
    stack<int> numberStack;

    for( unsigned int i =0; i< s.size(); i++ ){

        if( s[i]<='9' && s[i]>='0' ){

            numberStack.push( s[i]-0 );
        }else if( !numberStack.empty() ){

            a[total++] = getNumberFromStack( numberStack );
        }
    }

    // 输出全部数字
    for( int k=0; k < total; k++ ){

        cout << a[k] << "\n";
    }

    return 0;
}



本页的练习在无特殊说明时一律按照 单右向循环 链表为准。

1. L=(a,b,c,d,e) 作图 pass

2. setSize

复杂度O(n)
template <class T>
void chain<T>::theSize( int n ){
    chainNode<T> current = firstNode();
    int i=0, max = _size < n ? _size : n;

    // 由于不是双向链表,所以需要先到达n的极点
    while( i < max ){
        current = current->next();
        i++;
    }

    chainNode<T> tmp;
    // 如果有多余的
    while( _size > n ){
        tmp = current->next();
        delete current; // 系统会执行element析构
        current = tmp;
    }

    _size = n;
}

3. set()

复杂度O(n) 在实际使用的时候,链表一般不用index表示法来获取或设置元素。因为每次都相当于O(n)的复杂度。

template <class T>
void chain<T>::checkIndex( int theIndex ){

    if( theIndex <0 || theIndex >= _size ){
        throw illegalIndex("Out of range");
    }
}

template <class T>
void chain<T>::set( int theIndex, T& theElement ){
    checkIndex(theIndex);
    chainNode<T>* current = firstNode();
    int i = 0;
    while( i < _size ){
        current = current->next();
    }
    current->element.~T(); // 析构
    current->element = theElement;
}

4. removeRange

复杂度O(n) 和上面的setSize类似,做一次遍历,之后将切割掉的部分接起来即可。

template <class T>
void chain<T>::setSize( int fromIndex, int toIndex ){
    checkIndex(fromIndex);
    checkIndex(toIndex);

    chainNode<T>* current = firstNode();
    int i=0;

    // fromIndex的极点
    while( i < fromIndex ){
        current = current->next();
        i++;
    }

    chainNode<T>* tmp;

    while( i < toIndex ){
        tmp = current->next();
        delete current; // 系统会执行element析构
        current = tmp;
    }

    _size -= toIndex-fromIndex +1; // 考虑左右闭区间
}

5. lastIndex()

复杂度O(n) 遍历元素并比对,不即时返回。

template <class T>
int chain<T>::lastIndex( T& theElement ) const{

    if( empty() ) return -1;

    chainNode<T>* current = firstNode();
    int i = 0, lastIndex = -1;

    while( i< _size ){
        if( current->element == theElement ){
            lastIndex = i;
        }
        current = current->next();
        i++;
    }
    return lastIndex;
}

6. 重载[]

复杂度O(n) 实际上重载应该包含两种,分别是提供左值,右值返回。

template <class T>
const T& chain<T>::operator[](int n) const{  } // 右值
T& chain<T>::operator[]( int n) const{ // 左值
    int i = 0;
    chainNode<T>* current = firstNode();

    while( i < n ){
        current = current->next();
    }
    return current->element;
}

7. 重载==

复杂度O(n) 同步遍历两个链表元素,一旦发现不同元素返回false。

template <class T>
bool chain<T>::operator==( chain<T>& c) const{

    if( size() != c.size() ) return false;
    chainNode<T>* current = firstNode();
    chainNode<T>* target  = c.firstNode();

    int i = 0;
    while( i < size() ){
        if( current->element !== target->element ) return false;
        current = current->next();
        target  = target->next();
    }
    return true;
}

8. 重载!=

复杂度O(n)

template <class T>
bool chain<T>::operator!=( chain<T>& c) const{
    return !*this == c;
}

9. 重载<

复杂度O(n) 略微需要注意的是,除了最后一项的值完全相等以外,其他的相等 都不能判断非<。 所以在遍历的靠前阶段,== 都应该算成功,只有最后一项相等判断非小于。

template <class T>
bool chain<T>::operator<( chain<T>& c) const{

    if( size() > c.size() ) return false;
    chainNode<T>* current = firstNode();
    chainNode<T>* target  = c.firstNode();

    int i = 0;
    while( i < size() ){
        if( current->element > target->element ){
            return false;
        }else{
            current = current->next();
            target  = target->next();
        }
    }
    if( current->element == target->element ) return false;
    return true;
}

- 阅读剩余部分 -

1### 2. L = (a,b,c,d,e) ... 做图

初始状态:

0 1 2 3 4 5 6 7 8 9
a b c d e  

insert(0,f)

0 1 2 3 4 5 6 7 8 9
f a b c d e  

insert(3,g)

0 1 2 3 4 5 6 7 8 9
f a b g c d e  

insert(7,h)

0 1 2 3 4 5 6 7 8 9
f a b g c d e h  

earse(0)

0 1 2 3 4 5 6 7 8 9
a b g c d e h  

erase(4)

0 1 2 3 4 5 6 7 8 9
a b g c e h  

3. changeLength2D

二维数组的话要基于Array设计一个矩阵类,其中的每一行都是一个Array对象。

template <class T>
class ArrayMatrix: public Array{
    void changeLength2D( int x, int len );
}

template <class T>
void ArrayMatrix::changeLength2D( int rowIndex, int len ){
    checkIndex(x);
    get(rowIndex).changeLength(len);
}

3. 构造函数

好像题目描述有问题。 我自己写的是提供一个是否自动扩容的参数。如果配置为不自动扩容则在超出的时候抛出异常。

enum class ARRAY_AUTO_CAPACITY
{
    DISABLED = 0,ENABLED = 1
};

template <class T>
class Array{
public:
    Array(int initCapacity = ARRAY_DEFAULT_CAPACITY, ARRAY_AUTO_CAPACITY autoCapacity = ARRAY_AUTO_CAPACITY::DISABLED);
    void checkMax();
private:
    ARRAY_AUTO_CAPACITY _autoCapacity =  ARRAY_AUTO_CAPACITY::DISABLED;
}


template<class T>
inline void Array<T>::checkMax()
{
    if (_size >= _capacity) {

        switch (_autoCapcity)
        {
        case ARRAY_AUTO_CAPACITY::ENABLED:
            changeCapacityTo(_capacity << 1);
            break;
        case ARRAY_AUTO_CAPACITY::DISABLED:
            throw illegalInputData("Array is full.");
            break;
        default:
            break;
        }
    }
}

5. trimToSize()

实际上就是改变数组长度, 长度为size或1。 这里我基于我改写的 changeCapacityTo()来实现,实际上是一回事。

复杂度是O(n)

void trimToSize(){
    changeCapacityTo( _size>0 ? _size : 1 );
}

template<class T>
inline void Array<T>::changeCapacityTo(int newCapacity)
{
    T* newElements = new T[newCapacity];
    _capacity = newCapacity;
    _size = newCapacity < _size ? newCapacity : _size;

    for (int i = 0; i < _size; i++)
    {
        newElements[i] = _elements[i];
    }
    delete[] _elements;

    _elements = newElements;
}

6. setSize

这一题,同样题目描述是有问题的。 见上一题changeCapacityTo()

7. 重载[]

T& operator[]( int n){
    checkIndex(n);
    return _elements[n];
}

8. 重载==

template<class T>
bool operator==( const Array<T>& targetArray ) const{
    if( size()!= targetArray.size() ){
        return false;
    }
    for( int i=0; i<size();i++ ){
        if( get(i) != target.get(i) ){
            return false;
        }
    }
    return true;
}

9. 重载!=

template<class T>
bool operator!=( const Array<T>& targetArray ){
    return !*this == targetArray;
}

10. 重载<

见8 . 其中!= 替换为>

- 阅读剩余部分 -

C++在使用的时候莫名的会出一些编译错误,有时候只是语法的特定写法不一致,所以记录一下。

1. 不允许使用默认参数

默认参数需要写在定义部分,不能写在实现部分。

const ARRAY_DEFAULT_CAPACITY = 8;

template <class T>
class Array{
    Array( int capacity ); // 有效
    Array( int capacity = ARRAY_DEFAULT_CAPACITY ); // 有效
}

// 错误, 不允许使用默认参数
template<class T>
Array<T>::Array( int initCapacity = ARRAY_DEFAULT_CAPACITY ){
    ...
}

2. 空参数实例化错误

使用空参数实例化的时候,不能使用(),会被编译器识别为函数定义

// 使用上述定义

Array<float> arr(10) // 正确 容量为10
Array<float> arr();  // 错误 容量为默认长度 无法实例化
Array<float> arr;    // 正确 容量为默认长度 可以实例化