下面的代码显示了如何在C++ 11中随机加倍.在这个解决方案每一次,当我运行这个程序时,结果是一样的 - 我不知道为什么?当我运行程序时,如何更改它以获得不同的解决方案?
#include <random>
int main(int argc, char **argv)
{
double lower_bound = 0.;
double upper_bound = 1.;
std::uniform_real_distribution<double> unif(lower_bound, upper_bound);
std::default_random_engine re;
double a_random_double = unif(re);
cout << a_random_double << endl;
return 0;
}
//compilation: "g++ -std=c++0x program_name.cpp -o program_name"
Run Code Online (Sandbox Code Playgroud) 我想使用Gnuplot生成几个图,这就是为什么我需要使用循环.数据从文件"sort'i'.dat"加载.代码如下所示,但不起作用.我对主循环有一些问题.我不知道它为什么不起作用,也许它与我的Gnuplot版本有关.谢谢.
do for [i=0:10] {
set term png
set output 'sort'.i.'.png'
set title "Quick sort"
set xlabel "Position number"
set ylabel "Number on position"
unset key
plot 'sort'.i.'.dat' using 1:2 with points pt 5
}
Run Code Online (Sandbox Code Playgroud)
错误是:"第1行:无效复数常量"
下面的代码生成[0,PI)默认值之间的数字:
#include <iostream>
#include <random>
int main()
{
std::random_device rd;
std::default_random_engine re(rd());
//std::uniform_real_distribution<double> unifPhi(0., M_PI);//[0.,PI) // <- default
std::uniform_real_distribution<double> unifPhi{0.0, std::nextafter(M_PI, 2.*M_PI)};//probably [0.,PI]
for(unsigned int i=0u;i<10u;++i)
std::cout << unifPhi(re) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想在之间生成一个数字[0,PI].要明确第二个括号必须是],而不是)(具有封闭间隔).
有人可以告诉我上面的代码是否正确吗?
我有:
boost::shared_ptr<Car> sptr;
Run Code Online (Sandbox Code Playgroud)
现在我想从其他对象分配一个地址:
Car object;
sptr = &object;//error
Run Code Online (Sandbox Code Playgroud)
正如我们在评论中看到的那样,有一个错误.所以我的问题是如何将某个对象的地址分配给shared_ptr?如果它是正常的指针案例将是简单的:
Car *ptr;
Car object;
ptr = &object;//ok
Run Code Online (Sandbox Code Playgroud)
How to use in this case boost::shared_ptr? Thanks
下面的代码:
#include <iostream>
#include <list>
class A
{
public:
void printHello(){std::cout << "hello";}
};
int main(int argc, char *argv)
{
std::list<A*> lista;
lista.push_back(new A());
for(std::list<A*>::iterator it=lista.begin();it!=lista.end();++it)
{
//how to get to printHello method?
//it doesn't work
it->printHello();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码无效。我的问题是如何通过迭代器获取方法'printHello'?谢谢。
我为google测试配置了visual studio.然后我在vs2010中写了一些简单的google测试用例,如下所示:
TEST(simpleTest, test1){
float base = 4.f;
float exponent = 1.f;
float expectedValue = 4.f;
float actualValue = pow(base, exponent);
EXPECT_FLOAT_EQ(expectedValue, actualValue);
}
TEST(simpleTest, test2){
float base = 4.f;
float exponent = 2.f;
float expectedValue = 16.f;
float actualValue = pow(base, exponent);
EXPECT_FLOAT_EQ(expectedValue, actualValue);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何运行并非所有(RUN_ALL_TESTS)测试,而是一个特定的测试用例?有没有宏,例如RUN(simpleTest.test1); ?
我有代码:
double dd=2.99;
int ii=(int)(dd);//ii==2
Run Code Online (Sandbox Code Playgroud)
我希望在ii中有3个.有没有快速简单的方法?
下面的代码给出了Linux上exe文件的当前路径:
#include <iostream>
std::string getExePath()
{
char result[ PATH_MAX ];
ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX );
return std::string( result, (count > 0) ? count : 0 );
}
int main()
{
std::cout << getExePath() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我运行它时,给出了exe的当前路径和exe的名称,例如:
/home/.../Test/main.exe
Run Code Online (Sandbox Code Playgroud)
我想只得到
/home/.../Test/
Run Code Online (Sandbox Code Playgroud)
我知道我可以解析它,但有没有更好的方法呢?