我怎么叫clock()
的C++
?
例如,我想测试线性搜索在数组中查找给定元素所花费的时间.
引用用于计算整数绝对值(abs)的代码而不分支来自http://graphics.stanford.edu/~seander/bithacks.html:
int v; // we want to find the absolute value of v
unsigned int r; // the result goes here
int const mask = v >> sizeof(int) * CHAR_BIT - 1;
r = (v + mask) ^ mask;
Run Code Online (Sandbox Code Playgroud)
专利变化:
r = (v ^ mask) - mask;
Run Code Online (Sandbox Code Playgroud)
CHAR_BIT
它是什么以及如何使用它?
我们知道,例如两个幂的模数可以这样表达:
x % 2 inpower n == x & (2 inpower n - 1).
Run Code Online (Sandbox Code Playgroud)
例子:
x % 2 == x & 1
x % 4 == x & 3
x % 8 == x & 7
Run Code Online (Sandbox Code Playgroud)
两个数字的一般非权力怎么样?
让我们说:
x%7 ==?
有什么区别
int x=7;
Run Code Online (Sandbox Code Playgroud)
和
register int x=7;
Run Code Online (Sandbox Code Playgroud)
?
我正在使用C++.
例如,我有这个数组:
int a[] = new int[]{3,4,6,2,1};
Run Code Online (Sandbox Code Playgroud)
我需要所有排列的列表,如果一个像这样{3,2,1,4,6}
,其他的必须不一样.我知道如果数组的长度是n那么就有n!可能的组合.如何编写这个算法?
更新:谢谢,但我需要一个伪代码算法,如:
for(int i=0;i<a.length;i++){
// code here
}
Run Code Online (Sandbox Code Playgroud)
只是算法.是的,API函数很好,但它对我没有多大帮助.
我有以下信息:
Height Weight
170 65
167 55
189 85
175 70
166 55
174 55
169 69
170 58
184 84
161 56
170 75
182 68
167 51
187 85
178 62
173 60
172 68
178 55
175 65
176 70
Run Code Online (Sandbox Code Playgroud)
我想在Excel中构建二次和三次回归分析.我知道如何通过Excel中的线性回归来做到这一点,但是二次和立方呢?我搜索了很多资源,但找不到任何有用的东西.
我见过使用矢量的代码,
vector<int>s;
s.push_back(11);
s.push_back(22);
s.push_back(33);
s.push_back(55);
for (vector<int>::iterator it = s.begin(); it!=s.end(); it++) {
cout << *it << endl;
}
Run Code Online (Sandbox Code Playgroud)
它是一样的
for (auto it = s.begin(); it != s.end(); it++) {
cout << *it << endl;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下使用auto关键字有多安全?如果矢量类型是float
什么?string
?
如果序列单调增加然后单调减少,或者如果它可以循环移位到单调增加然后单调减少,则序列是比特的.例如,序列(1,4,6,8,3,-2),(9,2,-4,-10,-5)和(1,2,3,4)是比特的,但是(1 ,3,12,4,2,10)不是苦味的.
如何确定给定的序列是否是比特的?
我有以下意见.我们可以走到n/2,其中n是数组的长度,并检查是否
(a[i] < a[i + 1]) and (a[n - i - 1] < a[n-1 - (i + 1)])
Run Code Online (Sandbox Code Playgroud)
它是否正确?
当我只运行代码片段时
int *t;
std::cout << sizeof(char) << std::endl;
std::cout << sizeof(double) << std::endl;
std::cout << sizeof(int) << std::endl;
std::cout << sizeof(t) << std::endl;
Run Code Online (Sandbox Code Playgroud)
它给我一个这样的结果:
1
8
4
4
Run Code Online (Sandbox Code Playgroud)
总计:17.
但是,当我测试包含这些数据类型的sizeof结构时,它给了我24,我很困惑.额外的7个字节是多少?
这是代码
#include <iostream>
#include <stdio.h>
struct struct_type{
int i;
char ch;
int *p;
double d;
} s;
int main(){
int *t;
//std::cout << sizeof(char) <<std::endl;
//std::cout << sizeof(double) <<std::endl;
//std::cout << sizeof(int) <<std::endl;
//std::cout << sizeof(t) <<std::endl;
printf("s_type is %d byes long",sizeof(struct struct_type));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
:编辑
我已经像这样更新了我的代码
#include <iostream> …
Run Code Online (Sandbox Code Playgroud) c++ ×5
algorithm ×3
java ×2
benchmarking ×1
c ×1
c++11 ×1
clock ×1
excel ×1
regression ×1
stl ×1