c++ - 将带有时间戳的 vector 整数连接为 C++ 中的字符串?

标签 c++ string boost vector

我正在尝试使用以下代码将当前时间戳(以毫秒为单位)与我在 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/

相关文章:

c++ - 使用模板的 C++ 部分编译如何工作?

php - 使用 php 将带有路径的参数传递给命令行可执行文件

c++ - 流氓缺少 C++ 符号 - 调试策略?

python - 如何删除这个特殊字符?

c - 倒序时最后一个词没有倒序

c++ - 带重定位但不带 PIC 的可执行文件中共享库的引用函数

java - 将字符串更改为用户设置的值

c++ - BGL 迭代宏、类型名和模板

boost - 找不到 Boost(缺少 : python3) (found version "1.76.0") - CMake Windows

C++ boost 线程和互斥量