std::vector.push_back() C++

Viewed 306

I want to initialize a vector with alphabetical letters using push_back function. Is it the right way to do it?

vector<char> v;
char letter = 'A';
for(int i=0; i<26; i++)
{
    v.push_back(letter+i);
}

It works. I am just wondering whether I should use a type cast letter to an int before adding i to it?

Or is there a more efficient way?

2 Answers

Note that your code relies on a character encoding scheme that encodes the letters contiguously, like e.g. ASCII.

If that assumption holds, you could create the vector using the correct size initially, and use std::iota to initialize all elements:

std::vector<char> v(26);  // Create a vector of 26 (default-initialized) elements
std::iota(begin(v), end(v), 'A');  // Assign a letter to each element in the vector

If you want your code to be portable to systems where letters aren't contiguously encoded (like a system which uses EBCDIC) then you're better of to create a string using the letters explicitly:

std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";  // Thanks Nathan Oliver :)

And if you have a string with all the letters, then perhaps you won't need the vector even.

Looks pretty good!

  • I guess maybe std::array() would be an option too, as compared to std::vector() for similar tasks:
#include <iostream>
#include <array>
#include <vector>
#include <chrono>

void function1() {
    std::vector<char> alphabets;

    for (unsigned int index = 0; index < 26; ++index) {
        alphabets.push_back(index + 'A');
        // std::cout << alphabets[index] << "\t";
    }

    // std::cout << "\n\n";
}

void function2() {
    std::vector<char> alphabets;

    for (unsigned int index = 0; index < 26; ++index) {
        alphabets.emplace_back(index + 'A');
        // std::cout << alphabets[index] << "\t";
    }

    // std::cout << "\n\n";
}

void function3() {
    std::array<char, 26> alphabets;

    for (unsigned int index = 0; index < 26; ++index) {
        alphabets[index] = index + 'A';
        // std::cout << alphabets[index] << "\t";
    }

    // std::cout << "\n\n";
}

int main() {

    const auto t1 = std::chrono::high_resolution_clock::now();

    for (std::size_t i = 0; i < 1000000; ++i) {
        function1();
    }

    const auto t2 = std::chrono::high_resolution_clock::now();
    const auto duration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();

    std::cout << duration <<
              " is the rough runtime of std::vector function with push_back\t\t\n\n";



    const auto t3 = std::chrono::high_resolution_clock::now();

    for (std::size_t i = 0; i < 1000000; ++i) {
        function2();
    }

    const auto t4 = std::chrono::high_resolution_clock::now();
    const auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>( t4 - t3 ).count();

    std::cout << duration2 <<
              " is the rough runtime of std::vector function with emplace_back\t\t\n\n";


    const auto t5 = std::chrono::high_resolution_clock::now();

    for (std::size_t i = 0; i < 1000000; ++i) {
        function3();
    }

    const auto t6 = std::chrono::high_resolution_clock::now();
    const auto duration3 = std::chrono::duration_cast<std::chrono::microseconds>( t6 - t5 ).count();

    std::cout << duration3 << " is the rough runtime of std::array function\t\t\n\n";

    return 0;
};
Related