如何将 a 中的字符串与 vector
我试过str,错误打印在下面。
.....
vector<std::string> dat;
vector<std::string> pdat;
dat = my();
for(int b = 2; b < dat.size(); b+=7){
// cout << dat[b] << " " << endl;
if(!strcmp(dat[b], "String\n"){ // The error is here
pdat.push_back(dat[b]);
}
}
my.cpp:在函数“std::vector > ngr()”中:
my.cpp:53:32: 错误:无法将参数“1”的“__gnu_cxx::__alloc_traits >>::value_type {aka std::basic_string}”转换为“const char*”到“int strcmp(const char*,常量字符*)'
最佳答案
std::string
与普通 ==
进行比较。这是有效的,因为 ==
运算符被重载以对 std::string
进行字符串比较。
if (dat[b] == "String\n") {
如果您正在处理 C++ 字符串,您不应该需要 string.h 中的任何 str* 函数,因此您最好根本不包含它。
关于c++ - 如何将字符串与 vector 值进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27118053/