奇怪的c++字符串连接行为
Strange C++ string concatenation behaviour
我一直注意到字符串连接的这种奇怪行为,尽管strcmp
返回零,表明两种形式是相同的
包含文件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无法找到指定的文件。指针吗?(一语双关)
strcmp期望cstring,您可以通过
std::string x = "blah blah";
assert(strcmp(x.c_str(), x.c_str()) == 0);
但是如果你使用std::strings为什么不直接使用std::string::compare
std::string x = "blah blah";
assert(x.compare(x) == 0);
http://www.cplusplus.com/reference/string/string/compare/警告:如果你试图做比较unicode字符串那么简单的STRCMP可能并不总是工作
相关文章:
- 在 c++ 中连接字符串和整数,以便在 C++ 11 不支持计算机的情况下读取多个文件
- 这两个C++语句连接字符串有什么区别?
- 连接字符串族
- 如何使用 + 运算符连接字符串
- 如何按照 Google C++风格指南连接字符串文字?
- 我的字符串类的 += 运算符有问题,它没有正确连接字符串
- 如何编写模板函数来添加整数但连接字符串和字符类型?
- 无法创建栅格堆栈,因为我无法连接字符串(dir_name + 文件名)
- 如何在不使用对象的情况下连接字符串、整数和浮点数
- 在std :: string中连接字符串的选项
- 连接字符串的最佳方法是什么?
- 在实体框架C /CLI上配置连接字符串
- 在 c++ 中连接字符串和 int
- 如何连接字符串并传递给 system() 调用
- 有没有办法方便地为 Winsock send() 生成 HTTP 标头,而不是手动连接字符串
- 在可变参数模板函数中连接字符串(和数字)
- 连接字符串时出现意外问题
- 如何在 c++ 中连接字符串
- 自动连接字符串和 int C++
- 我的c++程序在我不告诉他的地方连接字符串