1

我有一种方法可以接受const char *,如下所示 -

bool get_data(const char* userid) const;

现在下面是我的 for 循环,我需要get_data通过传递来调用方法const char *。目前下面 for 循环使用uint64_t. 我需要将其转换uint64_tconst char *然后传递给get_data方法。

for (TmpIdSet::iterator it = userids.begin(); it != userids.end(); ++it) {
    // .. some code
    // convert it to const char *
    mpl_files.get_data(*it)
}

这里TmpIdSettypedef std::set<uint64_t> TmpIdSet;

所以我的问题是我应该如何将uint64_t上面的代码转换为const char *

4

3 回答 3

3

一种方法是首先将其转换为std::stringusing std::to_string然后使用std::string::c_str()成员函数访问原始数据:

#include <string>

....

uint64_t integer = 42;
std::string str = std::to_string(integer);
mpl_files.get_data(str.c_str());
于 2014-05-30T00:10:19.897 回答
1
 mpl_files.get_data( std::to_string( *it ).c_str() )

包括<string>标题。对于 g++ 指定-std=c++11.

于 2014-05-30T00:10:26.840 回答
-1

您可以简单地转换为 const char *:

uint64_t integer;
const char * newpointer = (const char *)integer;
于 2021-02-04T15:33:48.610 回答