我正在尝试调用该方法
bool someMethod(char const * begin, char const * end, size_t & count);
只有一个字符串。
std::string sString = "test";
if (someMethod(....)) {
std::cout << "working!";
}
但是我不知道如何正确地转换字符串。
我尝试了以下方法:
if (someMethod(&sString[0], &sString[sString.length() - 1], sString.length()) {
std::cout << "working!";
}
但是我收到以下消息:
error: invalid initialization of non-const reference of type ‘size_t& {aka long unsigned int&}’ from an rvalue of type ‘std::basic_string<char>::size_type {aka long unsigned int}’
如果有人给小费,那就太棒了。
谢谢!
这是一个“皮带和吊带”功能签名,因为它同时要求结束指针和字符计数。
如果您将其作为练习来完成,那么以下是您需要了解的内容:
std::string
通过调用获取指向C字符串的指针c_str()
size()
请注意,由于大小引用是非常量的,因此您需要在进行调用之前将其放入变量中:
size_t count = s.size();
const char *start = s.c_str();
someFunction(start, start+count, count);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句