TITLE: initialising STL containers (Newsgroups: comp.lang.c++.moderated, 5 May 2000) MERRICK: "Steve Merrick" > If I have an array full of fixed data, I used > to type: > > int array[] = {1, 2, 3, 4}; > > and that was that. There doesn't seem to be an equivalent syntax for > vectors: > > vector array = {1, 2, 3, 4}; // Illegal! > vector array(1, 2, 3, 4); // Illegal! > // and so on... > > Instead, I have to initialise a C-style array, and manually move its > contents into a vector. Is there a means of initialising a vector in this > way, or must I stick with C-style arrays for this purpose? AUSTERN: Matt Austern No, and no. You can't use a vector, because this initialization syntax only works for aggregates. (See section 8.5.1 of the C++ standard.) The class std::vector isn't an aggregate because it has user-defined constructors, it has private or protected data, and it might have base classes. However, you don't have to stick with C-style arrays. You can use an STL container class, it just can't be any of the STL container classes in the C++ standard. You can use a simple fixed-size container; Bjarne Stroustrup writes about a container called 'c_array' in the latest edition of _The C++ Programming Language_, and I write about a container called 'block' in my book. So, for example, you can write: block array = {1, 2, 3, 4}; _______________________________________________ cpptips mailing list http://cpptips.hyperformix.com