How to initialize a vector with generate N alphabet letters in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <algorithm>
 
void printVector(std::vector<char> const &v) {
    for (auto const &n: v) {
        std::cout << n << " ";
    }
}
  
int main()
{
    std::vector<char> v(10);
    
    int i = 0;
    std::generate(v.begin(), v.end(),
                [&]{
                    return 'a' + i++;
                });
 
    printVector(v);
  
    return 0;
}
 
 
 
 
/*
run:
 
a b c d e f g h i j 
 
*/

 



answered Feb 19, 2021 by avibootz

Related questions

1 answer 161 views
1 answer 148 views
1 answer 152 views
1 answer 157 views
1 answer 163 views
1 answer 169 views
...