我知道C或C++代码通常需要使用这样的包含守卫:
#ifndef __A__H__
#define __A__H__
class A{
};
#endif
Run Code Online (Sandbox Code Playgroud)
并且为了加快编译时间,在其他cpp(例如:B.cpp)中,它可以改变
#include "A.h"
Run Code Online (Sandbox Code Playgroud)
至:
#ifndef __A__H__
#include "A.h"
#endif
Run Code Online (Sandbox Code Playgroud)
但问题是为什么编译器不会自动添加或生成包含保护,因此如果通常需要包含保护,程序员为什么需要手动添加呢?
例如:0.5和0.25是2的幂但是0.3不是,我也知道检查整数是2的幂是否容易,但如果数字<1,如何找到数是2的幂?
public bool isPowerOf2(float number){
if(number>1){
//easy to write
}else if(number==1){
return true;
}else{
//hard to write
}
}
Run Code Online (Sandbox Code Playgroud) 例如,我想使用数组SQRT [i]创建一个平方根表来优化游戏,但我不知道在访问SQRT [i]的值时,以下初始化之间是否存在性能差异:
硬编码阵列
int SQRT[]={0,1,1,1,2,2,2,2,2,3,3,.......255,255,255}
Run Code Online (Sandbox Code Playgroud)在运行时生成值
int SQRT[65536];
int main(){
for(int i=0;i<65536;i++){
SQRT[i]=sqrt(i);
}
//other code
return 0;
}
Run Code Online (Sandbox Code Playgroud)访问它们的一些示例:
if(SQRT[a*a+b*b]>something)
...
Run Code Online (Sandbox Code Playgroud)
我不清楚程序是否以不同的方式存储或访问硬代码阵列,我也不知道编译器是否会优化硬代码阵列以加快访问时间,它们之间是否存在性能差异访问阵列时?
我想创建一个sin cos查找表进行优化,使用从0到0的数组索引UCHAR_MAX,这样0弧度就是索引0,pi/2radian是UCHAR_MAX/4:
sincos.h
#include <limits.h>
#include <math.h>
int sini[UCHAR_MAX];
int cosi[UCHAR_MAX];
#define MAGNIFICATION 256
#define SIN(i) sini[i]/MAGNIFICATION
#define COS(i) cosi[i]/MAGNIFICATION
void initTable(){
for(int i=0;i<UCHAR_MAX;i++){
sini[i]=sinf(i*2*M_PI/UCHAR_MAX)*MAGNIFICATION;
cosi[i]=cosf(i*2*M_PI/UCHAR_MAX)*MAGNIFICATION;
}
}
Run Code Online (Sandbox Code Playgroud)
使用UCHAR_MAXas max 的原因是我想充分利用unsigned char overflow来模拟从0到2*pi只变化的弧度:例如,如果radian的值是2*pi,则数组的索引变为UCHAR_MAX,因为它溢出,它自动变为0并且不需要mod(如果我使用0到360作为域,我可能index%360每次都需要计算).然后我用一些弧度值测试它:
float rad[]={2.0f,4.0f,6.0f,8.0f,10.0f,-2.0f,-4.0f,-6.0f,-8.0f,-10.0f};
Run Code Online (Sandbox Code Playgroud)
如下:
#include "sincos.h"
#include <stdio.h>
int main(){
initTable();
unsigned char radToIndex;
float rad[]={2.0f,4.0f,6.0f,8.0f,10.0f,-2.0f,-4.0f,-6.0f,-8.0f,-10.0f};
int scalar=123;
printf("scalar=%d\n",scalar);
for(int i=0;i<sizeof(rad)/sizeof(float);i++){
radToIndex=rad[i]*UCHAR_MAX/2/M_PI;
printf("%d*sin(%f) : %f , %d\n",scalar,rad[i],scalar*sinf(rad[i]),scalar*SIN(radToIndex));
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我测试了表 …
我想创建一个只需要一个随机数的程序,所以我尝试在main函数中使用argc的地址作为随机源,因为我认为程序在内存中的位置是随机的,也可以保存一些include语句,所以我试过了:
#include <stdio.h>
int main(int argc,const char* argv[]){
printf("%lu\n",(unsigned long int)&argv/sizeof(unsigned long int));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但我发现每次输出都不是很"随机":它们是4的倍数:
17591828907268
17591841542404
17591845040388
17591834556676
Run Code Online (Sandbox Code Playgroud)
是什么原因?并且使用argc的地址作为随机数可能吗?
然后我尝试删除一些地址:
#include <stdio.h>
int main(int argc,const char* argv[]){
printf("%lu\n",(unsigned long int)&argv >> 12);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这次看起来很随机,至少它有奇数和偶数:
34359070631
34359034616
34359078055
34359080624
Run Code Online (Sandbox Code Playgroud)
将argc的地址变成随机数的"正确"方法是什么?
我想创建一个程序来生成在将一个数组更改为另一个数组期间交换元素的步骤,(例如:从 {0,1,2} 到 {0,2,1},步骤是 1<->2,这意味着交换元素1和元素2的位置),以A={0,1,3,2}和B={2,0,3,1}为例,我最初的想法如下:
这是我尝试过的代码:
#include <stdlib.h>
#include <functional>
#include <vector>
int main(){
std::function<bool(int,int)> f=[](int a,int b){
if(a>=b)
printf("%d<->%d\n",a,b);
return a<b;
};
std::vector<int> a={0,1,3,2};
std::sort(a.begin(),a.end(),f);
printf("---\n");
std::vector<int> b={2,0,3,1};
std::sort(b.begin(),b.end(),f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
1<->0 //step to sort A
3<->1
2<->1
---
3<->0 //step to sort B
3<->2
1<->0
Run Code Online (Sandbox Code Playgroud)
因此从 0,1,3,2 更改为 2,0,3,1 的步骤应该是:
1<->0
3<->1
2<->1
1<->0
3<->2
3<->0
Run Code Online (Sandbox Code Playgroud)
但是当我按照步骤操作时:
0,1,3,2
1,0,3,2
3,0,1,2
3,0,2,1
3,1,2,0
2,1,3,0
2,1,0,3
Run Code Online (Sandbox Code Playgroud)
结果是 2,1,0,3 而不是 …
似乎文档也可以用作参数
void test(Value value);
Run Code Online (Sandbox Code Playgroud)
Document和Value都可以有子值,它们之间有什么区别?
我通过调用getInstance()创建了一个ScoreBoard类作为全局对象:
export class ScoreBoard{
protected _myNumber : number;
protected static scoreBoard : ScoreBoard;
constructor(){
this._myNumber=3;
}
get myNumber():number{
return this._myNumber;
}
set myNumber(_myNumber : number){
this._myNumber=_myNumber;
}
static getInstance() : ScoreBoard{
if(!this.scoreBoard){
this.scoreBoard=new ScoreBoard();
}
return this.scoreBoard;
}
}
Run Code Online (Sandbox Code Playgroud)
并尝试在html页面中调用它:
newscontent.ts
import { Component } from '@angular/core';
import { ScoreBoard } from '../scoreboard';
@Component({
templateUrl: 'newscontent.html'
})
export class NewsContent {
constructor() {
}
}
Run Code Online (Sandbox Code Playgroud)
newscontent.html
<ion-header>
<ion-navbar>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title></ion-title>
</ion-navbar>
</ion-header>
<ion-content fullscreen="true">
{{ScoreBoard.getInstance().myNumber}} …Run Code Online (Sandbox Code Playgroud) 我在for循环中放错了,但它似乎仍然可以编译.然后我尝试了for循环语法,它看起来像下面的代码:for循环中的一个类定义,在for循环中有一些像这样无意义的字符,可以编译.
int main(){
for(class A{} fkldsjflksdjflsj;;)
return 0;
}
Run Code Online (Sandbox Code Playgroud)
也.
for(class A;;)
for(class A{};;)
Run Code Online (Sandbox Code Playgroud)
但不是.
for(class A fkldsjflksdjflsj;;)
Run Code Online (Sandbox Code Playgroud)
为什么?
例如,要镜像pair < int , float >到pair < float , int >,我可以创建一个这样的模板函数:
template<class AB,class BA>
void mirror(const AB& ab,BA& ba){
ba.first=ab.second;
ba.second=ab.first;
}
int main(){
pair<int,float> ab;
ab.first=3;
ab.second=2.0;
pair<float,int> ba;
mirror<decltype(ab),decltype(ba)>(ab,ba);
printf("%d\n",ba.second);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是这种方法不能处理另一对中的一对,例如:
pair < bool , pair< int , float > >到pair < pair < float , int > , bool >:
pair<bool,pair<int,float> > a_bc;
pair<pair<float,int>,bool> cb_a;
mirror<decltype(a_bc),decltype(cb_a)>(a_bc,cb_a);
Run Code Online (Sandbox Code Playgroud)
pair < pair < A , B >, pair < C …