Search code examples
c++stringvectorconstructorinitializer-list

Why is this declaration of a vector invalid?


The error I'm getting is "expected a ;".

    const int SIZE = 9;

    vector<string>possiblePalindromes(SIZE) = // error is shown here
        { "A man a plan a canal Panama",
          "The rain in Spain",
          "No lemon, no melon",
          "radar",
          "CS1D",
          "Was it a cat I saw?",
          "Racecar",
          "Saddleback",
          "dad" };

My question is, would this not be a valid declaration? The error goes away if I remove (SIZE), but my intention is to declare the vector with a specific size and a set of predefined values. That way when I decide to iterate through the vector, like

for (int i = 0; i < SIZE; ++i)

I can refer to the SIZE of the vector directly, rather than some constant "9".

My inkling to this not working is that when I do

vector<string>possiblePalindromes(SIZE)

I am declaring the vector of size 9 with 9 string default values. Meaning that everything enclosed in the {} is not being read into the vector at all.


Solution

  • In fact you are trying to initialize an object of the type std::vector<std::string> using two constructors simultaneously.

    The first constructor is the following

    explicit vector(size_type n, const Allocator& = Allocator()); 
    

    and the second constructor is

    vector(initializer_list<T>, const Allocator& = Allocator()); 
    

    This does not make sense.

    You always can get the number of elements in a vector by using its member function size. And you always can resize a vector by using its method resize.

    Moreover you can use the range-based for loop where knowing the number of elements is not required.

    So just write

    vector<string>possiblePalindromes = 
        { "A man a plan a canal Panama",
          "The rain in Spain",
          "No lemon, no melon",
          "radar",
          "CS1D",
          "Was it a cat I saw?",
          "Racecar",
          "Saddleback",
          "dad" };
    

    Here is a demonstrative program

    #include <iostream>
    #include <string>
    #include <vector>
    
    int main() 
    {
        std::vector<std::string> possiblePalindromes = 
        { 
            "A man a plan a canal Panama",
            "The rain in Spain",
            "No lemon, no melon",
            "radar",
            "CS1D",
            "Was it a cat I saw?",
            "Racecar",
            "Saddleback",
            "dad" 
        };
    
        std::cout << "The vecor contains " << possiblePalindromes.size() << " elements\n";
    
        std::cout << "They are:\n";
    
        for ( const auto &s : possiblePalindromes )
        {
            std::cout << '\t' << s << '\n';
        }
    
        return 0;
    }
    

    Its output is

    The vecor contains 9 elements
    They are:
        A man a plan a canal Panama
        The rain in Spain
        No lemon, no melon
        radar
        CS1D
        Was it a cat I saw?
        Racecar
        Saddleback
        dad