我一直注意到字符串连接的这种奇怪行为,尽管strcmp返回0,这表明这两种形式是相同的
包含文件options.h如下所示
struct options = {
std::string ctifile;
...
};主文件以两种方式写入
方法1
#include "options.h"
#include <string>
#include "cantera/IdealGasMix.h"
options opt = {
"mech/tot.cti"
};
IdealGasMix gas(opt.ctifile, ...);在第二方法中,
#include "options.h"
#include <string>
#include "cantera/IdealGasMix.h"
options opt = {
"tot.cti"
};
IdealGasMix gas(std::string("mech/") + opt.ctifile, ...);由于某些原因,只有方法2无法找到指定的文件。有什么建议吗?(双关语)
发布于 2016-10-09 02:48:20
strcmp需要cstring,您可以通过下面的命令获得
std::string x = "blah blah";
assert(strcmp(x.c_str(), x.c_str()) == 0);但是如果你正在使用std::string,为什么不直接使用std::string::compare呢
std::string x = "blah blah";
assert(x.compare(x) == 0);http://www.cplusplus.com/reference/string/string/compare/
警告:如果您尝试比较unicode字符串,那么简单的strcmp可能并不总是有效的
https://stackoverflow.com/questions/39935939
复制相似问题