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 }
{
}
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/
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 ;)