Vector curly braces initialization list

In a textbook I am reading, the author initializes a vector using initialization list using curly braces
: vector{ numOfElements, -1 }
to initialize a vector of size numOfElements all with value -1.

But even in C++11, this creates a two item vector containing {128, -1}. I would have to resort to the old method of using parenthesis
: vector( numOfElements, -1 )
to initialize a vector of numOfElements that all contain -1.

How would you make curly braces initialization list work with vectors? Or is the author wrong?

1
2
3
4
// initializes vector of size numOfElements with all value -1
classObject::classObject( int numOfElements ) : vector( numOfElements, -1 ) 
{
}


1
2
3
4
// initializes vector of size 2 of {numOfElements, -1}
classObject::classObject( int numOfElements ) : vector{ numOfElements, -1 }
{
}
Last edited on
I would doubt the quality of any book which tries to store a size in an int instead of a std::size_t. It is most definitely a mistake.
Last edited on
Yea, the codes are not very optimized. It's mostly about the data structure and algorithm analysis. I'm reading the C++ book by Mark Allen Weiss, his page is here: http://users.cis.fiu.edu/~weiss/

There are a bunch of source codes on his website. The code pertaining to my question is in his C++ book and in the Disjoint Sets class "DisjSets.cpp" if you're interested: http://users.cis.fiu.edu/~weiss/dsaa_c++4/code/DisjSets.cpp

It's the first method of the code.
Last edited on
A quote from the site:
Throughout the text, the code has been updated to use C++11. Notably, this means use of the new C++11 features, including the auto keyword, the range for loop, move construction and assignment, and uniform initialization.
I wouldn't put it past the author to have only checked if the code compiled without checking that it had the same behavior. It's also worth noting that bad things can happen when you try to fix what isn't broken ;)
> But even in C++11, this creates a two item vector containing {128, -1}.
> I would have to resort to the old method of using parenthesis

Yes. Yes. For this particular case, favour the old method of using parenthesis.

(Using std::size_t to specify the type won't save you in all situations where {} is used.
Worse, it can lead to non-portable code.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>

int main()
{
    std::size_t n = 10 ;
    unsigned long long v = 7 ;

    std::vector<unsigned long long> one { n, v } ; // [ 20, 7 ] if there is no narrowing,
                                                   // error otherwise
    std::cout << one.size() << '\n' ; // 2 (if there was no error)

    std::vector<unsigned long long> two( n, v ) ; // always [ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 ]
    std::cout << two.size() << '\n' ; // 10
    
    // std::vector<int> three { n, 0 } ; // *** error: narrowing conversion
}

http://coliru.stacked-crooked.com/a/c4fd1c63c35931a5
Topic archived. No new replies allowed.