我需要对C++进行排序std::vector<std::string> fileNames.fileNames标记为这样
YYDDDTTTT_Z_SITE
YY =年份(即2009 = 09,2010 = 10)DDD =一年中的某一天(即1月1日= 001,12月31日= 365)TTTT =一天中的时间(即午夜= 0000,中午= 1200)
ZONE =将是E或W.
SITE =四个字母的网站名称(即HILL,SAMM)
我需要按以下顺序对字符串进行排序:ZONE,SITE,YY,DDD,TTTT
我想直接从Weather Unground读取天气数据到Matlab.对于给定站点,您可以选择以逗号分隔格式输出数据.如何编写将Matlab信息读入Matlab的Matlab函数?我不想下载文件,而是从URL中读取它.
例如,这是一些数据的URL.是否有一些Matlab函数将URL作为输入并从其中找到的任何内容中保存数据?
我正在编写一个bash shell脚本,在用户需要将路径作为$ 1变量传递给它的地方运行.如何确定是否缺少$ 1并为其定义默认值?
我正在从URL中读取数据,解析它,然后尝试进一步格式化数据:
year = 2008;
month = 9;
day = 30;
raw = urlread(sprintf('http://www.wunderground.com/history/airport/KCVS/%i/%i/%i/DailyHistory.html?HideSpecis=0&theprefset=SHOWMETAR&theprefvalue=0&format=1',year,month,day));
data = textscan(raw,'%s %s %s %s %s %s %s %s %s %s %s %s','Delimiter',',','HeaderLines',2,'CollectOutput',true);
dir = data{1}(1:end-1,7);
wind = cellfun(@str2num,data{1}(1:end-1,8),'UniformOutput',false);
gust = cellfun(@str2num,data{1}(1:end-1,9),'UniformOutput',false);
wind{cellfun(@isempty,wind)} = 0;
gust{cellfun(@isempty,gust)} = 0;
Run Code Online (Sandbox Code Playgroud)
现在wind{cellfun(@isempty,wind)} = 0;可以工作,但gust{cellfun(@isempty,gust)} = 0;我得到这个错误说:??? 这项任务的右侧数值太少,无法满足左侧的要求.cellfun(@isempty,gust)正确返回逻辑数组.也gust{1} = 0将正常工作.为什么它适用于风而不是阵风?
从标准的角度来看,我应该使用C++ <limits>标题中的以下内容吗?
UCHAR_MAX这是c实现,或者
std::numeric_limits<unsigned char>::max()是C++实现.
结果在两个版本之间是等效的,但在这种情况下,我应该根据某些标准或可读性和可移植性选择实现.请注意,此实现必须是跨平台兼容的.我正在编写C++代码.
我需要根据C++中的输入文件名动态创建目录,并且它必须是跨平台兼容的.我也熟悉boost库.目录创建函数的输入将是一个包含以下原型的字符串:
void createDirectory (std::string name)
示例代码将非常感激.
我正在使用cstdio (stdio.h)从二进制文件读取和写入数据.由于遗留代码,我必须使用此库,它必须与Windows和Linux跨平台兼容.我有一个FILE* basefile_,我用的变量来读取configLabelLength和configLabel,在那里configLabelLength告诉我多少内存来分配的configLabel.
unsigned int configLabelLength; // 4 bytes
char* configLabel = 0; // Variable length
fread((char *) &configLabelLength, 1, sizeof configLabelLength, baseFile_);
configLabel = new char[configLabelLength];
fread(configLabel,1, configLabelLength,baseFile_);
delete [] configLabel; // Free memory allocated for char array
configLabel = 0; // Be sure the deallocated memory isn't used
Run Code Online (Sandbox Code Playgroud)
有没有办法在configLabel不使用指针的情况下读入?例如,有一个解决方案,我可以使用c ++矢量库或我不必担心指针内存管理的东西.
假设我有一个带有两个私有变量的C++类.固定大小的数组,data以及指向该数组的指针pnt.
class MyClass
{
private:
double *pnt;
double data[2];
public:
myClass();
virtual ~MyClass();
double* getPnt() const;
void setPnt(double* input);
};
MyClass::MyClass()
{
double * input;
data[0] = 1;
data[1] = 2;
input= data;
setPnt(input);
}
MyClass::~MyClass()
{
delete this->pnt; // This throws a runtime error
}
void MyClass::setPnt(double * input)
{
pnt = input;
}
double * MyClass::getPnt() const;
{
return pnt;
}
int main()
{
MyClass spam; // Construct object
delete spam; // Error C2440: …Run Code Online (Sandbox Code Playgroud) 我需要从函数返回unsigned int*.下面的代码将编译但在运行时将在Windows 64位计算机上崩溃.我知道我在某个地方犯了一个愚蠢的错误,有人可以为我指出它.:P.我也在我的标题中声明了这个函数,所以我知道它不是那个错误.
请注意我已经审查了变量名和数字,因为此函数所在的问题尚未公开发布.
功能:
unsigned int* convertTime(unsigned int inputInteger, unsigned short inputFrac) {
unsigned int* output = new unsigned int[2];
double messageTimeFraction = double(inputFrac) * 20e-6;
output[1] = unsigned int(inputInteger + 2209032000);
output[2] = unsigned int(messageTimeFraction * 2e32);
return output; // Seconds
}
Run Code Online (Sandbox Code Playgroud)
执行:
unsigned int* timeStamp;
timeStamp = convertTime(inputInteger,inputFrac);
Run Code Online (Sandbox Code Playgroud) 这是C++中Char*vs String Speed的后续问题.我已经声明了以下变量:
std::vector<std::string> siteNames_;
std::vector<unsigned int> ids_;
std::vector<std::string> names_;
Run Code Online (Sandbox Code Playgroud)
我称这个功能成千上万次,是一个主要的瓶颈.是否有更有效的方法来比较字符串?答案必须是跨平台兼容的.
unsigned int converter::initilizeSiteId(unsigned int siteNumber){
unsigned int siteId = 0;
for (unsigned int i = 0; i < ids_.size(); i ++){
if (siteNames_[siteNumber].compare(names_[i]) == 0){
siteId = ids_[i];
break; // Once found, will stop searching and break out of for loop
}
}
if (siteId == 0)
std::cerr << "Could not find ID for site number " << siteNumber << std::endl;
return siteId;
}
Run Code Online (Sandbox Code Playgroud) 假设您有一个带有指向数组的私有指针的类.如何使用getter访问(或有效复制数据),以便可以使用其他变量访问它.
class MyClass
{
private:
double *x;
public:
myClass();
virtual ~MyClass();
double* getX() const;
void setX(double* input);
};
MyClass::MyClass()
{
double foo[2];
double * xInput;
foo[0] = 1;
foo[1] = 2;
xInput = foo;
setX(xInput);
}
void MyClass::setX(double * input)
{
x = input;
}
double * MyClass::getX() const;
{
return x;
}
int main()
{
MyClass spam(); // Construct object
double * bar = spam.getX(); // This doesn't work
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,bar [0]和bar [1]等于jibberish : -9.2559631349317831e+061.
为什么this在C++中被弃用?thisC++中的指针this与Java中的指针有何不同?
早期版本的C++会让this指针改变; 通过这样做,程序员可以改变方法正在处理的对象.这个功能最终被弃用了,现在C++中的这个是const.