我对这些术语thread以及core它们与我在家用计算机上编写和执行的程序的关系有一些概念上的混淆.假设我在我的机器上运行以下程序,这是一个带有四个线程的四核主板
#include <iostream>
using namespace std;
int main()
{
int long long num = 1E15;
while(num--)
{
cout << num << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我希望这个程序使用多个核心,我的程序是否需要支持多线程,还是需要并行化?或者在我的情况下是否相同,因为我的CPU每个核心有1个线程?
我正在测试我在计算机上制作一维矢量的大小.为此,我使用以下MWE:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<double> vec;
const unsigned long long lim = 1E8;
for(unsigned long long i=0; i<lim; i++)
{
vec.push_back(i);
}
cout << vec.max_size() << endl; //outputs 536.870.911 on my 32-bit system
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如图所示,max_size()我std::vector可以在系统中包含536.870.911个元素.但是,当我运行上面的MWE时,我得到了错误
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
我的电脑有2GB内存,但1E8整数只占381MB,所以我不明白为什么会出现bad_alloc错误?
我的函数test被添加到两个不同的.cpp文件中,并且这些函数对于各自的文件是私有的,如下所示
test1.cpp
#include <iostream>
using namespace std;
void test()
{
cout << "test" << endl;
}
Run Code Online (Sandbox Code Playgroud)
test2.cpp
#include <iostream>
using namespace std;
void test()
{
cout << "test" << endl;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp
#include <iostream>
using namespace std;
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在链接期间我收到错误multiple definition of test()- 但考虑到这两个文件有自己的私有范围,这怎么可能!?如果我在每个文件的相应标头中包含函数原型我可以理解它.cpp,但在这个例子中没有这样的东西。
我有一个长向量,包含> 100万个条目,根据概率密度函数(高斯)分布.我只需要正值,这些我在下面的MWE中找到
N = 1.5e6;
vals = normrnd(0, 1, N, 1);
final = [];
for i=1:length(vals)
if(vals(i)>0)
final = [final vals(i)];
end
end
Run Code Online (Sandbox Code Playgroud)
问题是这需要很长时间.在MatLAB中有更聪明的方法吗?
谢谢,奈尔斯.
我有一段用Fortran和Matlab编写的代码.他们做了完全相同的计算,即
tanh-field并找到它的拉普拉斯算子该乘法的结果产生矩阵,其中(4,4)和(6,6)I减去.
这个问题非常关键,因为我测试这个数字是否小于零. 问题:有没有办法执行计算,以便在Matlab中获得与Fortran相同的精度?
我列出以下代码:
MATLAB
clear all
weights = [4./9, 1./9,1./9,1./9,1./9, 1./36,1./36,1./36,1./36];
dir_x = [ 0, 1, 0, -1, 0, 1, -1, -1, 1];
dir_y = [ 0, 0, 1, 0, -1, 1, 1, -1, -1];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% CONSTANTS
length_y = 11; length_x = length_y;
y_center = 5; x_center = y_center;
densityHigh = 1.0;
densityLow = 0.1;
radius = 3.0;
c_width = 1.0;
average_density = 0.5*(densityHigh+densityLow);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for x=1:length_x
for y=1:length_y
for …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的程序,由于多个定义错误而无法编译.是这里:
main.cpp中
#include <iostream>
#include "read_p.h"
using namespace std;
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
read_p.cpp
#include "read_p.h"
using namespace std;
void read_p()
{
/*
some code here
*/
}
Run Code Online (Sandbox Code Playgroud)
read_p.h
#ifndef READ_P_H
#define READ_P_H
#include "buildings.h"
void read_p();
#endif
Run Code Online (Sandbox Code Playgroud)
buildings.h
#ifndef BUILDINGS_H
#define BUILDINGS_H
#include "flag.h"
using namespace std;
/*
some class here
*/
#endif
Run Code Online (Sandbox Code Playgroud)
flag.h
#ifndef FLAG_H
#define FLAG_H
using namespace std;
class
Test
{
private:
public:
int test_var;
Test(int);
};
Test::Test(int a)
{
test_var = a;
} …Run Code Online (Sandbox Code Playgroud) 我想将一个类的对象存储在一个std::map.这是一个工作示例,展示了我如何做到最新
#include <iostream>
#include <map>
class A
{
private:
int a;
std::string b;
public:
A(int init_a, std::string init_b) : a(init_a), b(init_b){};
void output_a() {std::cout << a << "\n";}
};
int main()
{
std::map<size_t, A> result_map;
for (size_t iter = 0; iter < 10; ++iter)
{
A a(iter, "bb");
result_map.insert(std::make_pair(iter, a));
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我对这个例子有两个问题:
这是专业的C++ - std::map在上述情况下存储对象的方式吗?或者我应该创建一个指向对象的指针A并存储它?我喜欢第一个(当前)选项,因为我不必担心使用new和自己进行内存管理delete- 但最重要的是我想要正确地做事.
我怎么会去调用一个成员函数result_map[0]呢?我天真地试过result_map[0].output_a(),但那给了我错误:error: no matching function for call to …