我正在尝试使用以下代码将当前时间戳(以毫秒为单位)与我在 vector 中的整数连接起来 -
基本上,我需要将 timestamp.integer
作为字符串
struct timeval tp;
gettimeofday(&tp, NULL);
uint64_t ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
struct timeval tp;
gettimeofday(&tp, NULL);
uint64_t ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
std::vector<uint32_t> myvector;
for (uint32_t i=1; i<=5; i++) myvector.push_back(i);
std::cout << "myvector contains:";
for (std::vector<uint32_t>::iterator it = myvector.begin() ; it != myvector.end(); ++it) {
string id = boost::lexical_cast<std::string>(ms)+"."+*it; // this line gives me exception?
std::cout << ' ' << id;
std::cout << '\n';
}
我希望打印出来的结果是这样的字符串 -
1384391287812.1
1384391287812.2
1384391287812.3
1384391287812.4
1384391287812.5
我得到的异常是 -
error: no match for âoperator+â in âstd::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>](((const char*)".")) + it.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator*<int*, std::vector<int> >()â
最佳答案
你想将it
迭代器引用的uint32_t
转换成字符串
string id = boost::lexical_cast<std::string>(ms)+"."+ boost::lexical_cast<std::string>(*it);
您不能简单地向字符串附加一个整数,因为没有编译器正确告诉您的 operator+(std::string&, uint32_t)
。
关于c++ - 将带有时间戳的 vector 整数连接为 C++ 中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19967717/