array

C++中的异常处理(下)

倾然丶 夕夏残阳落幕 提交于 2019-12-09 23:31:37
array.h #ifndef _ARRAY_H_ #define _ARRAY_H_ #include <stdexcept> using namespace std; template < typename T, int N > class Array { T m_array[N]; public: int length() const; bool set(int index, T value); bool get(int index, T& value); T& operator[] (int index); T operator[] (int index) const; virtual ~Array(); }; template < typename T, int N > int Array<T, N>::length() const { return N; } template < typename T, int N > bool Array<T, N>::set(int index, T value) { bool ret = (0 <= index) && (index < N); if( ret ) { m_array[index] = value; } return ret; } template < typename T, int N > bool Array

Numpy学习之——数组创建

谁说我不能喝 提交于 2019-12-09 22:51:11
Numpy学习之——数组创建 过程展示 import numpy as np a = np.array([2,3,9]) a array([2, 3, 9]) a.dtype dtype('int32') b = np.array([1.2,2.3,3]) b array([1.2, 2.3, 3. ]) b.dtype dtype('float64') 常见的错误是:直接将多个数值当做参数传递,正确的做法是将他们以列表或数组的方式传递 # a = np.array(1,2,3)#错误 b = np.array([1,2,3])#正确 b = np.array([(1.5,2.2),(4.5,3.9)]) b array([[1.5, 2.2], [4.5, 3.9]]) c = np.array([(1.5,2.2),(4.5,3.9)],dtype=complex) c array([[1.5+0.j, 2.2+0.j], [4.5+0.j, 3.9+0.j]]) s = "Hello! Mr.shi" np.array(s) array('Hello! Mr.shi', dtype='<U13') 创建带有初占位符内容的数组 np.zeros() np.ones() np.empty()初始内容为0或者垃圾值,取决于当前内存的状态 np.full(,x

矩阵分解

Deadly 提交于 2019-12-09 22:31:27
目录 矩阵的奇异值分解(Singular Value Decomposition): 分解步骤(SVD): 几何意义(SVD): 矩阵的奇异值分解(Singular Value Decomposition): 定义:设矩阵 A ∈ C r m × n A \in C_r^{m\times n} A ∈ C r m × n ​ , λ i \lambda _i λ i ​ 是 A A H ( A H A ) AA^H(A^HA) A A H ( A H A ) 的非零特征值,则称 σ i = λ i \sigma _i=\sqrt{\lambda _i} σ i ​ = λ i ​ ​ 为 A A A 的奇异值, i = 1 , 2 , ⋯   , r i=1,2,\cdots,r i = 1 , 2 , ⋯ , r 定理:设矩阵 A ∈ C r m × n A \in C_r^{m\times n} A ∈ C r m × n ​ ,则存在 U ∈ U m × m U \in U^{m\times m} U ∈ U m × m , V ∈ U n × n V \in U^{n\times n} V ∈ U n × n ,使得 A = U [ Δ 0 0 0 ] V H A=U \left [ \begin{matrix} \Delta &0\\ 0&0\end{matrix}

LeetCode:125.验证回文串

天大地大妈咪最大 提交于 2019-12-09 20:40:18
题目: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。 说明:本题中,我们将空字符串定义为有效的回文串。 示例 1 : 输入 : "A man, a plan, a canal: Panama" 输出 : true 示例 2 : 输入 : "race a car" 输出 : false 源码: class Solution { public boolean isPalindrome ( String s ) { if ( s . length ( ) <= 1 ) { return true ; } char [ ] array = s . toCharArray ( ) ; int left = 0 ; int right = array . length - 1 ; // 对字符串中的字符进行筛选 // 只有数字和字母参与比较 // 要将字母的大小写统一起来,便于进行比较 // 空格为有效回文串 while ( left < right ) { // 调整空格 if ( ( int ) array [ left ] == 32 ) { left ++ ; } if ( ( int ) array [ right ] == 32 ) { right -- ; } //调整大小写 if ( array [ left ] - 'A' >= 0 &&

PERL删除数组元素的多种方法

坚强是说给别人听的谎言 提交于 2019-12-09 17:32:25
比如说: @array = ('ray', 'loca', 'simon', 'ray'); 这里,我们想删除‘ray’这个元素。 列出几种方法: 1. 用grep函数。 函数名 grep 调用语法 @foundlist = grep (pattern, @searchlist); 解说 与同名的UNIX查找工具类似,grep函数在列表中抽取与指定模式匹配的元素,参数pattern为欲查找的模式,返回值是匹配元素的列表。 例子 @list = ("This", "is", "a", "test"); @foundlist = grep(/^[tT]/, @list); 结果 @foundlist = ("This", "test"); 2. 用map函数 函数名 map 调用语法 @resultlist = map (expr, @list); 解说 此函数在Perl5中定义,可以把列表中的各个元素作为表达式expr的操作数进行运算,其本身不改变,结果作为返回值。在表达式expr中,系统变量$_代表各个元素。 例子 1、 @list = (100, 200, 300); @results = map ($_+1, @list); 2、@results = map (&mysub($_), @list); 结果 1、(101, 201, 301) 2、无 3.

在JavaScript中删除数组元素-Delete与Splice

…衆ロ難τιáo~ 提交于 2019-12-09 16:48:06
在数组元素上使用 delete 运算符 与使用 Array.splice 方法有 什么 Array.splice ? 例如: myArray = ['a', 'b', 'c', 'd']; delete myArray[1]; // or myArray.splice (1, 1); 如果可以像删除对象一样删除数组元素,为什么还要使用splice方法? #1楼 在尝试了解如何从数组中删除元素的每次出现时,我偶然发现了这个问题。 这里有一个比较 的 splice 和 delete 去除每一个 'c' 从 items 阵列。 var items = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']; while (items.indexOf('c') !== -1) { items.splice(items.indexOf('c'), 1); } console.log(items); // ["a", "b", "d", "a", "b", "d"] items = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']; while (items.indexOf('c') !== -1) { delete items[items.indexOf('c')]; } console.log(items); // ["a", "b

pytorch中torch.unsqueeze()函数与np.expand_dims()

两盒软妹~` 提交于 2019-12-09 12:11:36
numpy. expand_dims ( a , axis ) Expand the shape of an array. Insert a new axis that will appear at the axis position in the expanded array shape. Parameters: a : array_like Input array. axis : int Position in the expanded axes where the new axis is placed. Returns: res : ndarray Output array. The number of dimensions is one greater than that of the input array. Examples >>> x = np.array([1,2]) >>> x.shape (2,) The following is equivalent to x[np.newaxis,:] or x[np.newaxis] : >>> y = np.expand_dims(x, axis=0) >>> y array([[1, 2]]) >>> y.shape (1, 2) >>> y = np.expand_dims(x, axis=1) #

js各种for循环的效率问题

微笑、不失礼 提交于 2019-12-09 05:59:44
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> </style> </head> <body> <script> let newarr = new Array(100000) let myfor = [] let myforin = [] let myforeach = [] let myforof = [] let mymap = [] let myfind = [] let myfilter = [] let mywhile = [] let mysome = [] let myevery = [] console.time("forin"); for(x in newarr){ myforin.push(x) } console.timeEnd("forin") console.time("for"); for(let i=0;i<newarr.length;i++){ myfor.push

foreach()注意事项

做~自己de王妃 提交于 2019-12-09 04:56:52
1. $value为传值赋值,而不是引用赋值。如需引用赋值,需要添加&符号。如下例所示: /////Example 1: $array=array(1,2,3); foreach($array as $key => $value){ $value *= 2; } print_r($array); Result: Array ( [0] => 1 [1] => 2 [2] => 3 ) /////Example 2: $array=array(1,2,3); foreach($array as $key => $value){ $array[$key] *= 2; } print_r($array); Result: Array ( [0] => 2 [1] => 4 [2] => 6 ) 原因:因为foreach()方法中的对$key和$value的赋值为传值赋值,所以Example 1中直接赋值给$value并没有真正地改变$array中的值;Example 2中$array[$key]是引用赋值,直接对$array中的数据进行操作,比较容易理解。 如果想要用$value完成对$array的更改,可以用&$value将赋值方式改为引用赋值,这时变量$value的指针就直接指向数组中的单元了。如下: $array=array(1,2,3); foreach($array as

python 实现十大排序算法

时光总嘲笑我的痴心妄想 提交于 2019-12-09 00:24:17
排序算法实现 冒泡排序 选择排序 插入排序 希尔排序 归并排序 快速排序 堆排序 计数排序 桶排序 基数排序 时间空间复杂度 冒泡排序 这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。 算法过程: 进行N-1趟操作 每一趟,都是不断的比较相邻的元素,那么一趟下来,就会将最大的移到排好顺序的最后面的位置。 代码实现: def bubbleSort ( array ) : ''' 冒泡排序 ''' for i in range ( len ( array ) - 1 , - 1 , - 1 ) : for j in range ( i ) : if array [ j ] > array [ j + 1 ] : array [ j ] , array [ j + 1 ] = array [ j + 1 ] , array [ j ] return array 最佳情况:T(n) = O(n) 最坏情况:T(n) = O(n2) 平均情况:T(n) = O(n2) 选择排序 选择排序是最稳定的排序了,不管什么情况都是O(n^2),唯一的优点可能是in-place,想法非常简单,进行n-1趟排序,每次从没排好序的部分选一个最小的元素放到已经排好序的序列的末尾。 代码实现: def selectSort ( array ) : ''' 选择排序 ''' for i in