所以对于像这样的代码:
class foo{
void bar(){
static int var = 2;
}
};
Run Code Online (Sandbox Code Playgroud)
我知道只有在var所有类型对象的实例上,foo但是var在创建foo之前C++是否为变量分配内存?我问这个是因为即使在foo被销毁之后,var也会在整个程序中存在.
这可能是微不足道的,我希望有人能解释一下。为什么 strlen 给我的数字比 char 数组的实际大小更大,而不是 4:这是我的代码:
#include <stdio.h>
#include <string.h>
int main ()
{
char szInput[4];
szInput [0] = '1';
szInput [1] = '1';
szInput [2] = '1';
szInput [3] = '1';
printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我以为我应该得到 4 作为输出,但我得到了 6。
输入的句子长度为 6 个字符。
boost::icl::interval_set<uint> test_set;
test_set.insert(boost::icl::discrete_interval<uint>::closed(10u, 20u));
test_set.insert(boost::icl::discrete_interval<uint>::closed(21u, 30u)); //should merge to single interval
test_set.insert(boost::icl::discrete_interval<uint>::closed(15u, 25u)); //should not change
test_set.erase(boost::icl::discrete_interval<uint>::closed(12u, 18u)); //should split in two intervals
uint i1min = test_set.begin()->lower();
uint i1max = test_set.begin()->upper();
uint i2min = (++test_set.begin())->lower();
uint i2max = (++test_set.begin())->upper();
std::cout<<i1min<<"\n";
std::cout<<i1max<<"\n";
std::cout<<i2min<<"\n";
std::cout<<i2max<<"\n";
Run Code Online (Sandbox Code Playgroud)
由于我正在添加和减去闭区间,因此我希望得到以下输出:
10
11
19
30
Run Code Online (Sandbox Code Playgroud)
但我得到:
10
12
18
30
Run Code Online (Sandbox Code Playgroud)
为什么我要删除的间隔的端点仍然存在?这是减去闭区间的预期行为还是我做错了什么?
我试图在类中定义模板成员函数,每次我尝试构建此代码时MSVC崩溃.我不确定这是否是Visual Studio 2008中的错误.这是一个最小的例子.
testTemp.h 头文件:
#pragma once
#include <vector>
#include <iostream>
class testTemp
{
public:
testTemp(void);
~testTemp(void);
template<typename T>
std::vector<T> m_vMonitorVec;
int MonitorSignal(T x, std::vector<T> vec, int len);
};
Run Code Online (Sandbox Code Playgroud)
这是testTemp.cpp:
#include "StdAfx.h"
#include "testTemp.h"
testTemp::testTemp(void)
{
}
testTemp::~testTemp(void)
{
}
template<typename T>
int testTemp::MonitorSignal(T inp, std::vector<T> monVec, int len)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和stdafx.h是:
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
Run Code Online (Sandbox Code Playgroud)
我在MSVC 2008中运行它,每当我尝试构建此代码时,我都会遇到以下崩溃:

正如标题所说,我有一个使用以下创建的线程:
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)this->TaskProc, this, 0, NULL);
Run Code Online (Sandbox Code Playgroud)
在TaskProc,我有一个无限的while循环:
while(true)
{
// large code with lots of initialization
// get task from another thread
// Switch (task)
// at each case you would perform a task
Sleep(1);
}
Run Code Online (Sandbox Code Playgroud)
我试图减少我的代码所花费的时间,我觉得Sleep(1)在while循环结束时没有必要!因为在点击之前已经在while循环中完成了很多计算Sleep(),是否可以删除Sleep()?
编辑1:我不知道为什么这个问题引起了混乱和几次投票,这是一个包含很多线程(大约5个)的非常大的代码的一部分,我想知道什么时候我们必须在每个循环中添加等待!接受的答案似乎给出了从哪里开始的良好暗示.虽然,我认为这个问题可以有更好的答案.
我有这个qt应用程序,在我的头文件中
class report_intro: public QWizardPage
{
Q_OBJECT
public:
report_intro(QWidget *parent = 0);
~report_intro(void);
int nextId() const;
private:
Ui::Rep_Wiz_Intro ui;
QWidget *pWnd;
std::vector<int> m_vTests;
int m_iNumTest;
};
Run Code Online (Sandbox Code Playgroud)
在我有的Cpp文件中
int report_intro::nextId() const
{
int i;
// check to see which check box was checked
for (i=1; i<m_iNumTest; i++)
{
QString str = QString("checkBox_T%1").arg(i);
if ((ui.groupBox_tests->findChild<QCheckBox *>(str))->isChecked())
m_vTests.push_back(i); // **** here is the error****
}
return newreport::report_page;
}
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
error C2663: 'std::vector<_Ty>::push_back' : 2 overloads have no legal conversion for 'this' pointer
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助...
我一直在努力解决这个问题.使用MATLAB读取包含float格式的三列数字的二进制文件时.
我正在使用这一行一次读一个数字.
pt(j) = fread(fid,1,'float','a');
Run Code Online (Sandbox Code Playgroud)
我发现有时(很少)MATLAB而不是为浮点读取四个字节,它使用5个字节.它错过了其余的阅读.我不确定文件是否已损坏或MATLAB存在错误.当我将文件打印为txt并在txt中读取时,一切正常.
我在这里做的工作就是:
cur = ftell(fid);
if (cur - prev)~= 4
pt(j) = 0; % I m throwing this reading away for the sake of saving the rest of the data. This is not ideal
cur = prev +4;
fseek(fid, cur,'bof');
end
prev = cur;
Run Code Online (Sandbox Code Playgroud)
我尝试了不同格式的不同组合float32 float64等...... MATLAB在这个特定的位置,没有什么可以读取5个字节而不是4个字节.
编辑:根据克里斯的答案解决它.我正在使用此命令打开文件.
fid = fopen(fname,'rt');
Run Code Online (Sandbox Code Playgroud)
我用它替换了它
fid = fopen(fname,'r');
Run Code Online (Sandbox Code Playgroud)