How to initialize a vector with characters in C++

2 Answers

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

int main() {
    // Direct initialization with characters
    std::vector<char> chars = {'a', 'b', 'c', 'd'};

    for (char ch : chars) {
        std::cout << ch << ' ';
    }
}

 
/*
run:
 
a b c d 
 
*/

 



answered Nov 24, 2025 by avibootz
0 votes
#include <iostream>
#include <vector>

int main() {
    // Create vector of size 5, all initialized to 'x'
    std::vector<char> chars(5, 'x');

    for (char ch : chars) {
        std::cout << ch << ' ';
    }
}


 
/*
run:
 
x x x x x 
 
*/

 



answered Nov 24, 2025 by avibootz
...