### Vector problem

Hi, i've been doing some programming for a while, and im using a C++ book that i've bought. But then i came to the "vector" part. i got stuck. In my book, it sais
 ``1234567`` ``````#include using namespace std; int (main){ vector v3 = {1,2,3,4,5}; // v3 contains {1,2,3,4,5} }``````

But then i run it, i get a bunch of errors. Can someone explain how i can use Vector in a error-free way. And maybe a example when i can use it.
Thanks :D // DaHappy
Last edited on
That is a C++11 initialization, you need a C++11 compatible compiler or use
 ``123456`` ``````int main()//your syntax here is wrong { std::vector v3; for(int i = 1; i<=5; ++i) v3.push_back(i); }``````
Here's a slightly more complicated way but there are some good things to learn in here if you take the time to study and understand it

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````#include #include #include #include // This is a generic template declaration // Using this requires you to initialize it with something // like: MyGenerator mygenerator; template class MyGenerator { public: // These are constructors // the values after the colon are being initialized // when the MyGenerate class is constructed MyGenerator() : start(0),increment(1) { } MyGenerator(T st, T incr) : start(st),increment(incr) { } // This overloads the operator () so it can later be used with std::generate // This is one way to do it T operator()() { T value = start; start += increment; return value; } private: // These are private member variables T start; T increment; }; // This is an explicit template specialization // if you want to specialize the generator for the // double data type template <> class MyGenerator { public: MyGenerator() : start(0),increment(1) { } MyGenerator(double st,double incr) : start(st-incr),increment(incr) { } //** // this is a second way to do it (notice the difference in the constructor // initializations double operator()() { return start += increment; } private: double start; double increment; }; // main function int main(int argc, char** argv) { // initialize the vector to size = 5, fill data with 0.0 // so initial vector is: 0., 0., 0., 0., 0. std::vector myvector(5,0.0); // notice the declaration, this is a product // of using a template, because I have an explicit // specialization for the double data type the second // instance of MyGenerator is called, calls constructed noted // with ** MyGenerator mygenerator(1,1); // After the line below the data in vector will be 1, 2, 3, 4, 5 std::generate(myvector.begin(),myvector.end(),mygenerator); // These print the vector to the screen std::ostream_iterator ite (std::cout," "); std::copy ( myvector.begin(), myvector.end(), ite ); // Ends the line std::cout << std::endl; return 0; }``````

The benefit of creating all the MyGenerator class is it gives you the capability to initialize your vectors in this manner in the future without having to write a loop every time. Put it in its own header file (always do all implementation of template classes in headers), surround it by

 ``123456`` ``````#ifndef mygenerator_hh_ #define mygenerator_hh_ // ... MyGenerator class definition... #endif ``````

and simply include it and use the lines like the example:

 ``12`` `````` MyGenerator mygenerator(1,1); std::generate(myvector.begin(),myvector.end(),mygenerator);``````

whenever you want

The MyGenerator class is used to make what is called a function object, essentially the ` double operator()() ` (called an operator overload) tells the class how to act as a function. `mygenerator(1,1)` says start at 1 and increment by 1 and it will continue to do this until it reaches myvector.end()

So in `std::generate(myvector.begin(),myvector.end(),mygenerator);` generate is basically calling MyGenerator() to give it a value to put in myvector at the current position between begin() and end() it is at in myvector.

It has so many components because it is a class, then it also has even more components because I made both a generic template class (can handle any data type such as int, double, bool, float, etc..), and a specialized template class (not really needed here for double but would be if you were using strings, bools, etc)

The reason for doing something like this has its benefits. The most significant benefit is for the generation of vectors far more complicated than 1,2,3,4,5. It is a class so you add all the functionality you want to get it to return whatever value you are after. For example, if you were generating a vector of atomic data (as in hydrogen, helium, etc) according to temperature you would want to use something similar to MyGenerator instead of a bunch of loops.

Even in simpler cases it still has benefits: Write it once, don't write a loop again. Say you want vectors 5, 4, 3, 2, 1 and 3,5,7,9,11 later on - all you would have to do is write:

` MyGenerator<double> mygenerator2(5,-1); `
and
` MyGenerator<double> mygenerator3(3,2); `
then use the std::generate function with some new vector and either mygenerator2 or mygenerator3

Or you could use boost::assign library:

 ``1234567891011`` ``````#include #include using namespace boost::assign; int main(int argc, char* argv[]) { std::vector v; v += 1, 2, 3, 4, 5; // here v will contain {1, 2, 3, 4, 5} return 0; }``````

http://www.boost.org/doc/libs/1_50_0/libs/assign/doc/index.html#tutorial
Thank for everything! Jmadsen, you are those one of those guys who makes the world to a better place! You have wrote all these just for me and i really appreciate everything! Thanks again! And im also thanksfull for the other anwsers :)
Topic archived. No new replies allowed.