请考虑以下代码:
class T { ... }; // T has a non-trivial destructor
void foo() {
std::vector<T> v( 5 );
v.pop_back();
...
}
Run Code Online (Sandbox Code Playgroud)
之后v.pop_back(),STL是否保证T::~T()已被要求v.back()?注意:问题保持用于去除元件载体的任何方法(例如resize(),erase()等...)
我正在为我的应用程序编写容器类,我希望它们尽可能符合标准库使用的原则.如果我的集装箱舱没有做出这种保证,是否有任何风险?
使用基于策略的设计,EncapsulatedAlgorithm:
template< typename Policy>
class EncapsulatedAlgorithm : public Policy
{
double x = 0;
public:
using Policy::subCalculate;
void calculate()
{
Policy::subCalculate(x);
}
protected:
~EncapsulatedAlgorithm() = default;
};
Run Code Online (Sandbox Code Playgroud)
可能有一个Policy执行子计算的策略.算法不需要子计算:在某些情况下可以使用它来加速算法收敛.因此,为了对此进行建模,假设有三种策略.
一个只是"记录"的东西:
struct log
{
static void subCalculate(double& x)
{
std::cout << "Doing the calculation" << endl;
}
};
Run Code Online (Sandbox Code Playgroud)
一个计算:
struct calculate
{
static void subCalculate(double& x)
{
x = x * x;
}
};
Run Code Online (Sandbox Code Playgroud)
一个人把他们全部带到黑暗中并束缚他们:D - 绝对没有:
struct doNothing
{
static void subCalculate(double& x)
{
// Do …Run Code Online (Sandbox Code Playgroud) 用我加载linux内核模块后
user@c4:$ insmod mmaptest.ko
Run Code Online (Sandbox Code Playgroud)
我可以验证它是通过加载的
user@c4:$ cat /proc/modules
mmaptest 12727 0 - Live 0x0000000000000000 (OF)
Run Code Online (Sandbox Code Playgroud)
但是所有段都列出了0x00地址.
user@c4$:$ systool -vm mmaptest
Module = "mmaptest"
Attributes:
coresize = "12727"
initsize = "0"
initstate = "live"
refcnt = "0"
srcversion = "EABEF6F90BEAAD0D15B576A"
taint = "OF"
uevent = <store method only>
Parameters:
count = "0"
Sections:
.bss = "0x0000000000000000"
.data = "0x0000000000000000"
.exit.text = "0x0000000000000000"
.gnu.linkonce.this_module= "0x0000000000000000"
.init.text = "0x0000000000000000"
.note.gnu.build-id = "0x0000000000000000"
.rodata = "0x0000000000000000"
.rodata.str1.1 = "0x0000000000000000"
.rodata.str1.8 = "0x0000000000000000"
.smp_locks = …Run Code Online (Sandbox Code Playgroud) 我有一个模特
class TreeModel : public QAbstractItemModel
Run Code Online (Sandbox Code Playgroud)
我填充了我的TreeItem排除列== 1的实例.在第1列中,我创建了CheckBoxes:
QVariant TreeModel::data(const QModelIndex &index, int role) const {
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole) {
if (role == Qt::CheckStateRole) {
if (index.column() == 1) {
if (index.row() == 1) {
return Qt::Unchecked;
} else
return Qt::Checked;
}
}
return QVariant();
}
if (role == Qt::DisplayRole) {
if (index.column() != 1) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
return item->data(index.column());
}
}
return QVariant();
} …Run Code Online (Sandbox Code Playgroud) 我试图用C++模拟股票价格变动.我需要创建一个介于0到1之间的随机数.
但似乎随机数生成器值不断增加并且不是真正随机的.
代码如下所示:
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<time.h>
using namespace std;
int main()
{
double stockPrice = 25;
int start = 0, end = 0;
start = clock();
srand (time(NULL));
cout << (double) rand() / (double) (RAND_MAX) << endl;
system("pause");
while(stockPrice > 18)
{
if(stockPrice == 20)
{
double probability = (rand()/(double)RAND_MAX);
if(probability <= (1/10))
{
stockPrice = stockPrice-1;
}
else
{
stockPrice = stockPrice +1;
}
}
else if (stockPrice < 20)
{
double probability = (rand()/(double)RAND_MAX);
if(probability <= (1/3)) …Run Code Online (Sandbox Code Playgroud) 我正在尝试做这个函数来解决线性系统,A*x = b,其中 A = 下三角矩阵,线性无关矩阵,并且只有一个解。但结果总是显示 0 0 0 0 ... 我已经打印了总和,s,它也总是显示 0 ...
#include <iostream>
using namespace std;
void solve(int n, float a[][MAX], float b[], float x[]){
int i,j;
float s;
for(i = 0; i < n; i++){
s = 0;
for(j = 0; j < n; j++){
s = s + a[i][j]*x[j];
cout<<s<<endl;
}
x[i] = (b[i] - s)/a[i][i];
}
}
Run Code Online (Sandbox Code Playgroud) 有很多类似的问题,但我没有在那里找到解决方案.
如何在没有调用的情况下 在Linux Ubuntu 12.10上获得C或C++的CPU温度sensors?我当然可以从文件中读取它,但是我找不到它在12.10中的存储位置.并且只是简单地阅读文本文件的可能性或者我可以使用系统调用或信号查询内核吗?
我的文件夹/ proc/acpi /的内容就是
event wakeup
Run Code Online (Sandbox Code Playgroud)
没有THEMP0那里或类似的东西.sensors然而,应用程序可以在我的机器上显示温度.
没有/sys/class/thermal/thermal_zone0/目录
在/sys/class/thermal我有
cooling_device0@ cooling_device1@ cooling_device2@ cooling_device3@
Run Code Online (Sandbox Code Playgroud)
我正在尝试浏览lm-sensors源代码以寻找它如何检索温度,到目前为止无济于事,但我很接近.该文件是
http://lm-sensors.org/browser/lm-sensors/trunk/lib/sysfs.c
特别是:
第846行:
846 int sensors_read_sysfs_attr(const sensors_chip_name *name,
847 const sensors_subfeature *subfeature,
848 double *value)
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用参数作为全局实现operator new.如果没有args的new被重载没有问题,但是在尝试编译时我遇到了以下错误
inline void* operator new(size_t, void* p) {
//...
return p;
}
Run Code Online (Sandbox Code Playgroud)
c:\ bjarne_exercise_6.cpp(14):错误C2084:函数'void*operator new(size_t,void*)throw()'已经有一个正文c:\ program files\microsoft visual studio 10.0\vc\include \new( 55):看看'new'的先前定义
c:\ bjarne_exercise_6.cpp(40):错误C2264:'operator new':函数定义或声明中的错误; 功能未被调用
我刚刚解决了这个问题,你必须在#include stdafx.h之前声明这个.不,不是真的.它编译得很好,但仍然没有调用此函数,但是来自新头文件的版本.是这样,因为新标题中已经定义了新的(有2个参数).普通的new(只有1,size_t参数)只在那里声明,所以你仍然可以重载它.因此,如果你想要带有多个参数的特殊new,下面的@trion建议的解决方案是合适的.
一切正常,但我刚刚试图在NetBeans上的Linux Ubuntu 12.10上运行我的CUDA程序,我得到错误:
dist/Debug/GNU-Linux-x86/my_cuda_1:加载共享库时出错:libcudart.so.5.0:无法打开共享对象文件:没有这样的文件或目录
RUN FAILED(退出值127,总时间:191ms)
项目可以构建:编译+链接没有问题,我也可以从命令行运行它,但如果我尝试从NetBeans运行它我得到这个错误.几分钟前我能够运行它,可能会发生什么?
我已将文件my_lib添加到包含此类文本的ld.so.conf.d:
/usr/local/cuda-5.0/lib64:/lib
/usr/lib
/usr/lib64
它可以是一个链接问题libcudart.so.5.0 - > licudart.so?在cuda/lib libcudart.so.5.0(链接)和libcudart.so(链接)以及libcudart.so.5.0.35(共享库)中有两个库,为什么它抱怨.so.5.0?可能链接如下:sudo ln -s /usr/lib/x86_64-linux-gnu/libglut.so.3 /usr/lib/libglut.so是必要的
它与我认为的共享库的链接有关
root @ comp:#echo $ LD_LIBRARY_PATH /usr/lib/nvidia-current:/usr/local/cuda-5.0/lib::/usr/local/cuda-5.0/lib64:/lib
这不是::一个问题?似乎不是因为我改变了这个并且仍然是同样的错误.我指定PATHS,我运行ldconfig,我把输出放在.bashrc中,仍然没有.原因是如果从NetBeans GUI启动程序,我无法加载这些库,但是我始终以root身份启动NetBeans
我有一个 std::list ,如下所示(x 标记表示小于 500 的数字)
x,x,x,x,503,x,x,x,510,x,x,x,502,x,x,x,x,x,x,600 - std::list<int> originallist
Run Code Online (Sandbox Code Playgroud)
我希望将列表拆分为列表向量,std::vector<std::list<int> >如下所示
1st element of vector: x,x,x,x,503
2nd element of vector: x,x,x,510
...
...
last element of vector: x,x,x,x,x,x,600
Run Code Online (Sandbox Code Playgroud)
我现在的代码如下:
list<int> templist; vector<list<int> > v;
for(list<int>::iterator lit=originallist.begin(); lit!=oriniallist.end(); ++lit) {
if (*lit > 500) {
templist.push_back(*lit);v.push_back(templist); templist.clear(); continue;
}
templist.push_back(*lit);
}
Run Code Online (Sandbox Code Playgroud)
在 C++ 中不使用 templist 实现上述任务的最有效方法是什么?任何帮助表示赞赏。