继有关链接 *= += 运算符的问题和Tom Wojcik 的好评(“你为什么假设aaa *= 200比 快aaa = aaa * 200? ”)之后,我在 Jupyter notebook 中对其进行了测试:
%%timeit aaa = np.arange(1,101,1)
aaa*=100
%%timeit aaa = np.arange(1,101,1)
aaa=aaa*100
Run Code Online (Sandbox Code Playgroud)
我很惊讶,因为第一个测试比第二个测试长:分别为 1530ns 和 952ns。为什么这些价值观如此不同?
这是我的代码的骨架:
var myArray: (Array<any> | null);
if (cnd) {
myArray = [];
myArray?.push(elt); // Question 1
myArray[0].key = value; //Question 2
} else {
myArray = null;
}
Run Code Online (Sandbox Code Playgroud)
问题1:为什么?需要?myArray 已被分配给一个空数组。
问题 2:避免错误的语法是什么:对象可能为空。
感谢您的回答。
如何仅选择另一个元素(例如 h1)之后的第一个元素(例如 h2)但不一定紧跟在 之后?
因此,元素+元素(例如 h1 + h2)不起作用,因为它选择了紧跟在元素之后的元素
<h1> Title1 </h1>
..... <--! many tags but h2 -->
<h2> h2 Title2 selected </h2>
..... <--! many tags but h1 and h2-->
<h2> h2 Title3 not selected </h2>
Run Code Online (Sandbox Code Playgroud) I read on many posts on Stack Overflow that an allocatable array is deallocated when it is passed in a subroutine where the dummy argument is intent(out).
If I consider the following code :
program main
real, dimension(:), allocatable :: myArray
integer :: L=8
allocate(myArray(1:L))
call initArray(myArray)
print *, myArray
contains
subroutine initArray(myArray)
real, dimension(:), intent(out) :: myArray
myArray(:) = 10.0
end subroutine initArray
end program main
Run Code Online (Sandbox Code Playgroud)
输出是正确的。因此,当发生释放时,内存被释放,但数组形状保持不变。准确吗?任何详细的解释将不胜感激。
我阅读了有关该主题的不同帖子(我可以在 Fortran 中使用可分配数组作为意图(输出)矩阵吗?,将可分配变量传递到具有不可分配参数的子例程的效果是什么?,...)。所以我知道数组已被释放,但我想了解这是什么意思,因为在我的代码中,大小被保留,而且我也对这段代码的工作原理感到惊讶。
我发现这个宏#define TIMES(x) for(int i1=0;i1<x;i1++)非常实用,可以缩短代码文本.但是当我有嵌套循环时,我不知道如何编写这样的宏,甚至我不知道是否可能.这个想法如下.是否可以编写此代码
for(int i1=0;i1<5;i1++)
for(int i2=0;i2<3;i2++)
for (int i3=0;i3<7;i3++)
/* many nested `for` loops */
{
/* some code, for example to print an array printf("%d \n",a[i1][i2][i3]) */
}
Run Code Online (Sandbox Code Playgroud)
如
TIMES(5) TIMES(3) TIMES(7) ....
{
/* some code, for example to print an array printf("%d \n",a[i1][i2][i3]) */
}
Run Code Online (Sandbox Code Playgroud)
用一种"递归"宏来检测所有TIMES并通过for循环替换它们与i1,i2,i3,... i'n'循环计数器?
考虑以下接口:
interface MyType1 {
field1: string;
options: {
basicOption1: string;
basicOption2: string;
};
}
interface MoreOptions {
moreOptions1: string;
moreOptions2: string;
}
Run Code Online (Sandbox Code Playgroud)
我扩展了接口 MyType1 的字段选项:
interface MyType2 extends MyType1{
options: {
basicOption1: string; // How to remove it, yet declared in MyType1
basicOption2: string; // How to remove it, yet declared in MyType1
moreOptions1: string;
moreOptions2: string;
};
}
Run Code Online (Sandbox Code Playgroud)
如果我省略行 :basicOptions1:string;和basicOptions2:string;,编译器会抱怨。所以,我添加它们。但是,是否可以删除它们并仅保留行 :moreOptions1:string;和moreOptions2:string;其他选项。
接下来,如何Mytype2从两个类型为MyType1和的对象分配一个类型的对象MoreOptions?
例如(Rq:我测试了其他东西但也不成功):
let myobj1: …Run Code Online (Sandbox Code Playgroud) 我关注了这篇文章如何在我的 React 组件中使用 SCSS 变量或这个React 和 SCSS 从 scss 导出一个变量以做出反应以在我的 React 应用程序中获取 scss 变量,但它不起作用。
myvar.scss文件:
$color: red;
:export {
color: $color;
}
.myclass {
background-color: $color;
}
Run Code Online (Sandbox Code Playgroud)
App.js文件:
import variables from './myvar.scss';
const App = () => {
console.log(variables);
return <div className="myclass">Hello</div>
}
export default App;
Run Code Online (Sandbox Code Playgroud)
div 背景是红色的,所以 myvar.scss 正在工作。但是variables是一个空对象。
(反应版本:17.0.1)
node_modules\react-scripts\config\webpack.config.js
module: {
strictExportPresence: true,
rules: [
{ parser: { requireEnsure: false } },
{
oneOf: [
...
{
test: sassRegex, …Run Code Online (Sandbox Code Playgroud) 让我们考虑这两个函数:
void my_foo1(char ** my_par, int size) {
for (int i=0; i<size; i++) printf("%s \n",my_par[i]);
}
void my_foo2(int * my_par, int size) {
for (int i=0; i<size; i++) printf("%d \n",my_par[i]);
}
Run Code Online (Sandbox Code Playgroud)
为了调用它们,需要声明并初始化变量。之后,使用这些变量在第二行调用函数。
char * (my_strs[3])={"hello","world","!!!!"};
my_foo1(my_strs,3);
int my_ints[3]={1,2,3};
my_foo2(my_ints,3);
Run Code Online (Sandbox Code Playgroud)
是否可以写这样的东西:
my_foox(????,3)
Run Code Online (Sandbox Code Playgroud)
并避免变量声明?
考虑以下类:
class MyClass():
def __init__(self,attr):
self.attr=attr
Run Code Online (Sandbox Code Playgroud)
和 MyClass 对象列表:
myList=[MyClass(1),MyClass(2),MyClass(3)]
Run Code Online (Sandbox Code Playgroud)
属性值是唯一的。要获取具有特定值的对象的引用(例如:2),我使用了
[i for i in myList if i.attr==2][0]
Run Code Online (Sandbox Code Playgroud)
或者
myList[[i.attr for i in myList].index(2)]
Run Code Online (Sandbox Code Playgroud)
做到这一点的最佳方法是什么,是否有更好的解决方案?(如果可以,因为我是 Python 初学者,请回答不同解决方案的优缺点,将不胜感激)。(Rq:这个例子很简单,列表可以更大,对象更复杂)
谢谢你的回答。
使用灵活数组成员(FAM)或指针成员有什么区别?在这两种情况下,必须按元素执行malloc和effectation元素.但是对于FAM,内存分配是针对整个结构进行的,并且对于ptr成员,仅对ptr成员进行内存分配(参见代码).这两种方法的优点和缺点是什么?
#include <stdio.h>
#include <stdlib.h>
typedef struct farr_mb {
int lg;
int arr[];
} Farr_mb;
typedef struct ptr_mb {
int lg;
int * ptr;
} Ptr_mb;
int main() {
int lg=5;
Farr_mb *a=malloc(sizeof(Farr_mb)+lg*sizeof(int));
Ptr_mb b; b.ptr=malloc(lg*sizeof(int));
for (int i=0;i<lg;i++) (a->arr)[i]=i;
for (int i=0;i<lg;i++) (b.ptr)[i]=i;
for (int i=0;i<lg;i++) printf("%d \t",(a->arr)[i]=i);
printf("\n");
for (int i=0;i<lg;i++) printf("%d \t",(b.ptr)[i]=i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)