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 272 views
272 views asked Jun 13, 2017 by avibootz
1 answer 235 views
3 answers 259 views
259 views asked Mar 3, 2017 by avibootz
1 answer 206 views
206 views asked Nov 27, 2019 by avibootz
1 answer 212 views
212 views asked Nov 27, 2019 by avibootz
1 answer 166 views
166 views asked Jun 13, 2018 by avibootz
1 answer 130 views
130 views asked Sep 20, 2021 by avibootz
...