我在我的mac/xcode上编译了这个代码,没有任何问题.我在学校用linux上的g ++编译它,我得到这些错误:
:'numeric_limits'不是std的成员
:'>'标记之前的预期primary-expression
:没有用于调用'max()'的匹配函数
#include <iostream>
#include <cstdlib>
using namespace std;
int GetIntegerInput(int lower, int upper)
{
int integer = -1;
do
{
cin >> integer;
cin.clear();
cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); //errors here
}while (integer < lower || integer > upper);
return integer;
}
Run Code Online (Sandbox Code Playgroud)
我猜对了也许我必须加一个额外的标题.如果我带走了std ::它只是给了我一个类似的错误
'numeric_limits'未在此范围内声明
我有一个C++程序:
struct arguments
{
int a, b, c;
arguments(): a(3), b(6), c(9) {}
};
class test_class{
public:
void *member_func(void *args){
arguments vars = (arguments *) (*args); //error: void is not a
//pointer-to-object type
std::cout << "\n" << vars.a << "\t" << vars.b << "\t" << vars.c << "\n";
}
};
Run Code Online (Sandbox Code Playgroud)
在编译时它会抛出一个错误:
error: ‘void*’ is not a pointer-to-object type
Run Code Online (Sandbox Code Playgroud)
有人可以解释我做错了产生这个错误吗?
在C++中,当一个应该返回一个对象的函数在没有return语句的情况下结束时会发生什么?什么回来了?
例如
std::string func() {}
Run Code Online (Sandbox Code Playgroud) 在c ++中,什么是一个很好的启发式方法来估计内联函数的计算时间优势,特别是当函数被非常频繁地调用并占计划执行时间的≥10%时(例如,暴力的评估函数或随机优化过程).即使内联可能最终超出我的控制范围,我仍然很好奇.
void NetClass::Modulate(vector <synapse> & synapses )
{
int size = synapses.size();
int split = 200 * 0.5;
for(int w=0; w < size; w++)
if(synapses[w].active)
synapses[w].rmod = ((rand_r(seedp) % 200 - split ) / 1000.0);
}
Run Code Online (Sandbox Code Playgroud)
该功能rand_r(seedp)
严重瓶颈我的计划.具体来说,它在串行运行时减慢了3倍,在16核运行时减慢了4.4倍.rand()
不是一种选择,因为它更糟糕.有什么我可以做的来简化这个吗?如果它会产生影响,我认为我可以在统计随机性方面遭受损失.预先生成(在执行之前)一个随机数列表,然后加载到线程堆栈是一个选项吗?
#include <iostream>
#include <vector>
#include <algorithm>
class MyData
{
public:
int m_iData;
bool operator<(MyData rhs) { return m_iData < rhs.m_iData; }
};
int main ()
{
std:: vector <MyData> myvector(2, MyData() );
myvector[0].m_iData=2; myvector[1].m_iData=4;
std::sort(myvector.begin(), myvector.end());
}
Run Code Online (Sandbox Code Playgroud)
试图编译它给出:
error: passing 'const MyData' as 'this' argument of 'bool MyData::operator<(MyData)'
discards qualifiers
Run Code Online (Sandbox Code Playgroud) 为什么这个循环的时间复杂度是非线性的,为什么它如此慢?循环需要~38s for N=50k,
和~570s for N=200k
.有更快的方法吗?Rprof()
似乎表明写入内存非常慢.
df <- data.frame(replicate(5, runif(200000)))
df[,1:3] <- round(df[,1:3])
Rprof(line.profiling = TRUE); timer <- proc.time()
x <- df; N <- nrow(df); i <- 1
ind <- df[1:(N-1),1:3] == df[2:N,1:3];
rind <- which(apply(ind,1,all))
N <- length(rind)
while(i <= N)
{
x$X4[rind[i]+1] <- x$X4[rind[i]+1] + x$X4[rind[i]]
x$X5[rind[i]+1] <- x$X4[rind[i]+1] * x$X3[rind[i]+1]
x$X5[rind[i]+1] <- trunc(x$X5[rind[i]+1]*10^8)/10^8
x$X1[rind[i]] <- NA
i <- i + 1
};x <- na.omit(x)
proc.time() - timer; Rprof(NULL)
summaryRprof(lines = "show")
Run Code Online (Sandbox Code Playgroud)
该算法的目的是迭代数据帧并组合在某些元素上匹配的相邻行.也就是说,它会删除其中一行,并将该行的某些值添加到另一行.结果数据帧应该少n行,其中n是原始数据帧中匹配的相邻行的数量.每次组合一对行时,源数据帧和新数据帧的索引将不同步1,因为从新帧中删除/省略了一行,因此 …
如果我有:
struct a_struct
{
int an_int;
a_struct(int f) : an_int(f) {}
a_struct() : an_int(0) {}
};
class a_class
{
a_struct * my_structs;
a_class() {...}
};
Run Code Online (Sandbox Code Playgroud)
我可以:
a_class() {my_structs = new a_struct(1)}
//or
a_class() {my_structs = new a_struct [10]}
Run Code Online (Sandbox Code Playgroud)
但我不能这样做:
a_class() {my_structs = new a_struct(1) [10]}
//or
a_class() {my_structs = new a_struct() [10]}
Run Code Online (Sandbox Code Playgroud)
有没有正确的语法来使这个工作?或者轻松解决?
CPU周期(或者,实质上,"速度")之间有什么不同
x /= y;
Run Code Online (Sandbox Code Playgroud)
和
#include <cmath>
x = sqrt(y);
Run Code Online (Sandbox Code Playgroud)
编辑:我知道操作不等同,我只是随意提出x /= y
作为基准x = sqrt(y)
gprof说我的高计算应用程序占用了53%的时间std::vector <...> operator [] (unsigned long)
,其中32%用于一个使用频繁的向量.更糟糕的是,我怀疑我的并行代码未能扩展到3-6核心之外是由于相关的内存瓶颈.虽然我的应用确实花费了大量时间来访问和编写内存,但似乎我应该能够(或者至少尝试)做得比52%更好.我应该尝试使用动态数组(大多数情况下大小保持不变)吗?这可能有助于解决可能出现的瓶颈问题吗?
实际上,我的首选解决方案是解决瓶颈并保留向量,以方便使用.基于以上所述,是否有任何可能的罪魁祸首或解决方案(tcmalloc已经出局)?