Reputation: 109
I have:
std::vector<unsigned int> data;
data[0] = 1;
data[1] = 1;
data[2] = 0;
data[3] = 1;
data[4] = 0;
data[5] = 2;
data[6] = 0;
data[7] = 2;
data[8] = 1;
data[9] = 2;
data[10] = 1;
data[11] = 2;
Now I want to arrange it in pairs, like 11 01 02 02 12 12.
i.e.
paired_data[0] = data[0]data[1]
paired_data[1] = data[2]data[3] etc....
paired_data[0] = 11;
paired_data[1] = 01;
paired_data[2] = 02;
paired_data[3] = 02;
paired_data[4] = 12;
paired_data[5] = 12;
I think concatenating vectors will work out here, but I am not sure how. Can someone suggest me to handle this with vector concatenation (or any other logic)?
If the data were of type bool
then it would be easy to handle with left/right shifts. But data
contain ternary data(0,1,2), so how can I handle that?
Upvotes: 0
Views: 144
Reputation: 1518
u can easy convert it and contatenate this way, but iam not sure what u want
vector<unsigned int> data; // vector
unsigned int a = 4;
unsigned int b = 5;
data.push_back(a); //push in vector
data.push_back(b);
int count=0;
ostringstream oss; //convert
oss << a << b; //concatenate
istringstream iss(oss.str());
int c;
iss >> c; // convert to int
vector<unsigned int >paired_data;
paired_data.push_back(c);
try to put this in a loop where u concatenate always two loop interations
Upvotes: 0
Reputation: 94
First of all you need to properly define "concatenation" operation. Do you mean bitwise concatenation or something complicated?
One approach is to use std::pair template. This will provide you with concatenation of arbitrary types. Another approach is to use multidimensional vector, i.e.
std::vector<int> data;
std::vector<std::array<int,2>> paired_data;
for (size_t i = 0; i < data.size() - 1; i+=2)
{
paired_data.push_back(std::array<int,2>{{data[i], data[i+1]}});
}
Upvotes: 1