I wish to output data from my program to a text file. Here is a working example showing how I do it currently, where I also include the date/time (I am running Windows):
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;
int main()
{
char dateStr [9];
char timeStr [9];
_strdate(dateStr);
_strtime(timeStr);
ofstream output("info.txt", ios::out);
output << "Start time part 1 " << "\t" << timeStr << " on " << dateStr << "\n";
output << "Start time part …Run Code Online (Sandbox Code Playgroud) 我在C++ 11中使用新的随机数生成器.尽管存在不同的意见,但从这个主题看来,大多数人认为它们不是线程安全的.因此,我想制作一个程序,每个线程使用自己的RNG.
在如何使用OpenMP实现此目的的相关讨论中给出了一个示例:
#include <random>
#include <iostream>
#include <time.h>
#include "omp.h"
using namespace std;
int main()
{
unsigned long long app = 0;
{
//mt19937_64 engine((omp_get_thread_num() + 1)); //USE FOR MULTITHREADING
mt19937_64 engine; //USE FOR SINGLE THREAD
uniform_real_distribution<double> zeroToOne(0.0, 1.0);
//#pragma omp parallel for reduction(+:app) //USE FOR MULTITHREADING
for (unsigned long long i = 0; i < 2000000000; i++)
{
if(zeroToOne(engine) < 0.5) app++;
}
}
cout << app << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行该程序的多线程和单线程版本并跟踪时间时,执行后需要相同的时间才能完成.此外,app两种情况下的大小不一样,但我怀疑这仅仅是因为种子不同.
问题 …
我有一个数据向量,我想找到数据集的峰度.我想用Boost这样做,这是我到目前为止(不可编译):
#include <boost/math/distributions.hpp>
using namespace std;
int main()
{
vector<double> a;
a.push_back(-1);
a.push_back(0);
a.push_back(1);
cout << "Kurtosis:"<< kurtosis(a) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?我的编译器给了我错误:"[...]\main.cpp | 28 |错误:'kurtosis'未在此范围内声明|"
我正在使用GCC版本4.4.7的服务器,不幸的是我被迫使用这个版本.我想利用<random>C++ 0x库,但我在这里读到,在这个版本中uniform_real_distribution被调用uniform_real.当我尝试调用此函数时normal_distribution,我没有得到有用的输出.看这个例子:
#include <random>
#include <iostream>
using namespace std;
int main()
{
typedef std::mt19937 Engine;
typedef std::uniform_real<double> Dis1;
typedef std::normal_distribution<double> Dis2;
Engine eng(0);
Dis1 dis1(0, 1);
cout << dis1(eng) << endl; //OUTPUTS 3.49921e+09
Dis2 dis2(0, 1);
cout << dis2(eng) << endl; //STALLS, NO OUTPUT
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我编译,g++44 -std=c++0x main.cpp我已经显示了我得到的输出.这是什么问题?
我正在尝试使用 Boostproperty tree来解析 JSON 文件。这是 JSON 文件
{
"a": 1,
"b": [{
"b_a": 2,
"b_b": {
"b_b_a": "test"
},
"b_c": 0,
"b_d": [{
"b_d_a": 3,
"b_d_b": {
"b_d_c": 4
},
"b_d_c": "test",
"b_d_d": {
"b_d_d": 5
}
}],
"b_e": null,
"b_f": [{
"b_f_a": 6
}],
"b_g": 7
}],
"c": 8
}
Run Code Online (Sandbox Code Playgroud)
和一个 MWE
#include <iostream>
#include <fstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
namespace pt = boost::property_tree;
using namespace std;
int main()
{
boost::property_tree::ptree jsontree;
boost::property_tree::read_json("test.json", jsontree);
int v0 = …Run Code Online (Sandbox Code Playgroud) 我在Rcpp中有一个函数,该函数在一个类中创建一个很长的地图结构。我在下面给出了一个简单的例子:
#include <Rcpp.h>
using namespace Rcpp;
class A{
private:
std::map<int, int> m_map;
public:
void fill_map(const size_t limit){
for(size_t i=0; i<limit; ++i){
m_map[i] = i;
}
}
size_t size_map(){return m_map.size();}
};
// [[Rcpp::export]]
void func1(const size_t limit) {
A a;
a.fill_map(limit);
}
/* NOT WORKING */
// [[Rcpp::export]]
void func2(A a)
{
std::cout << a.size_map() << "\n";
}
/* NOT WORKING */
Run Code Online (Sandbox Code Playgroud)
假设我致电func1(1e7),这将在a-object中填满地图。我需要将此对象传递A给其他函数,如上所示func2。
但是,我的示例func2不起作用。在Rcpp-framework中,用上func2一个函数中定义的对象进行调用的正确,最有效的方法是什么?
我正在使用C++ 11提供的RNG,我也在使用OpenMP.我为每个线程分配了一个引擎,作为测试,我给每个引擎提供相同的种子.这意味着我希望两个线程产生完全相同的随机生成数字序列.这是一个MWE:
#include <iostream>
#include <random>
using namespace std;
uniform_real_distribution<double> uni(0, 1);
normal_distribution<double> nor(0, 1);
int main()
{
#pragma omp parallel
{
mt19937 eng(0); //GIVE EACH THREAD ITS OWN ENGINE
vector<double> vec;
#pragma omp for
for(int i=0; i<5; i++)
{
nor(eng);
vec.push_back(uni(eng));
}
#pragma omp critical
cout << vec[0] << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我经常得到输出0.857946 0.857946,但有几次得到0.857946 0.592845.当两个线程具有相同的,不相关的引擎时,后一个结果怎么可能?!
在Fortran中,我输出结果,tanh(1)并得到值0.7615941763:
open(unit=2, file='test_digits.txt', ACTION="write")
write(2, '(1000F14.10)')( real(tanh(1.0)))
Run Code Online (Sandbox Code Playgroud)
但是,我现在尝试在MatLAB中做同样的事情,输出是0.761594155955765.第8位有差异.
这种精确度差异的原因是什么?我能以某种方式修复它吗?
我正在尝试按照 Moo 和 Kernig 的Accelerated C++(第 257-257 页)构建一个通用句柄类。但是,为了调用clone基类的函数,我需要创建通用的Handle类 a friendof Base。
下面的例子给了我错误:
\n\nmain.cpp:30:18: error: template argument required for \xe2\x80\x98class Handle\xe2\x80\x99\n friend class Handle;\n ^\nmain.cpp:30:5: error: friend declaration does not name a class or function\n friend class Handle;\n ^\nmain.cpp: In instantiation of \xe2\x80\x98Handle<T>& Handle<T>::operator=(const Handle<T>&) [with T = Base]\xe2\x80\x99:\nmain.cpp:42:7: required from here\nmain.cpp:33:19: error: \xe2\x80\x98virtual Base* Base::clone() const\xe2\x80\x99 is protected\n virtual Base *clone() const { return new Base; }\nRun Code Online (Sandbox Code Playgroud)\n\nHandle交朋友的正确表达方式是什么Base …
我有一个数学函数,它依赖于三个变量{n,a和b}
当n是偶数时,{a = n + 1,b = n}
当n为奇数时,{b = n + 1,a = n}
我的函数被调用很多次,各种N-.有没有一种有效的方法来实现它?我认为从长远来看a switch和if声明可能效率不高.
编辑:这是一个最小的例子:
void func(int n)
{
int a, b;
if(!(n%2))
{
a=n+1;
b=n;
}
else
{
a=n;
b=n+1;
}
//continue ...
}
Run Code Online (Sandbox Code Playgroud)