[英]How to pass std::string to const unsigned char ** in C++
[英]How to pass a string to const char* in C++?
我正在尝试拆分字符串.
在C ++中,然后是第一个拆分的字符串,我需要传递给另一个接受const char* key
..但每次我这样做,我总是得到一个例外 -
以下是我的代码 -
istringstream iss(key);
std::vector<std::string> tokens;
std::string token;
while (std::getline(iss, token, '.')) {
if (!token.empty()) {
tokens.push_back(token);
}
}
cout<<"First Splitted String: " <<tokens[0] << endl;
attr_map.upsert(tokens[0]); //this throws an exception
}
下面是AttributeMap.hh文件中的upsert方法 -
bool upsert(const char* key);
以下是我经常得到的例外 -
no matching function for call to AttributeMap::upsert(std::basic_string<char>&)
有什么我想念的吗?
使用c_str()
获取指向“以null结尾的字符数组的指针,其数据与存储在字符串中的数据相同”(引自文档)。
attr_map.upsert(tokens[0].c_str()); //this won't throw an exception
你应该使用string :: c_str
attr_map.upsert(tokens[0].c_str())
//^^^
您可以查看参考以获取有关c_str()
函数的详细信息。
您收到错误是因为upsert
函数需要const char*
,但是您传递的是std::string
,类型不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.