Converting a vector of digits(ints) into a string
Is there a way to convert a vector of digits into a string made of those digits?
my case
vector <int> c;
c.push_back(1);
c.push_back(2);
c.push_back(3);
c.push_back(4);
some magic =========> string s = '4321' ( i need it backwards)
tnx in advance
If you mean without writing a function or a lambda, I think there isn't.
Otherwise, you could use std::transform:
1 2
|
string s;
std::transform(c.rbegin(), c.rend(), std::back_inserter(s), [](int int_value) { return std::to_string(int_value).front(); });
|
1 2 3 4
|
string out = "";
for (int i = c.size() - 1; i >= 0; --i) {
out += c[i] + 48;
}
|
Although if you need multiple digit numbers added you would need to change the inside of the for, this seems to work.
Topic archived. No new replies allowed.