How to convert int vector to ASCII char vector in C++

1 Answer

0 votes
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> vint {97, 98, 99, 100};
    std::vector<char> vchar {};

    vchar.reserve(vint.size());
    for (auto &n : vint) {
        vchar.push_back(n);
    }

    std::copy(vchar.begin(), vchar.end(),
              std::ostream_iterator<char>(std::cout, " "));
              
    return 0;
}



/*
run:

a b c d 

*/

 



answered May 16, 2021 by avibootz

Related questions

1 answer 378 views
378 views asked Jun 13, 2017 by avibootz
1 answer 337 views
3 answers 394 views
394 views asked Mar 3, 2017 by avibootz
1 answer 305 views
305 views asked Nov 27, 2019 by avibootz
1 answer 293 views
293 views asked Nov 27, 2019 by avibootz
1 answer 257 views
257 views asked Jun 13, 2018 by avibootz
1 answer 204 views
204 views asked Sep 20, 2021 by avibootz
...