简体   繁体   English

将所有向量元素读取为 C++ 中的一个 integer

[英]Read all vector elements as one integer in C++

I have this deque and this vector:我有这个双端队列和这个向量:

std::deque<uint8_t> Time(3);
std::vector<uint8_t> deque_buffer(3);

I do some push_backs :我做了一些push_backs

Time.push_back(1);
Time.push_back(2);
Time.push_back(3);

I copy the data to the vector我将数据复制到vector

for(int i=0; i<3; i++)
{
    deque_buffer.at(i) = Time.at(i);
}

I want to read all the vector data and store it into an int .我想读取所有矢量数据并将其存储到int中。 Meaning I want 0x010203 to be interpreted as 66051 .意思是我希望0x010203被解释为66051 How do I do it?我该怎么做?

Here is what you can do: (sample code on how to "merge" vector elements).这是您可以执行的操作:(有关如何“合并”矢量元素的示例代码)。

#include <iostream>
#include <vector> 
using namespace std;

int main()
{


    vector<unsigned char> vec = {1,2,3};
    int res = 0;

    for (const auto &val:vec)
    {
        res = (res << 8 ) | val;
    }

    printf ("0x%x\n",res);
    printf ("%d\n",res);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM