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
1
2
3
4
5
6
7
#include <vector>
using namespace std;

int (main){

 vector<double> 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
1
2
3
4
5
6
int main()//your syntax here is wrong
{
	std::vector<double> 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iterator>
#include <iostream>
#include <vector>
#include <algorithm>

// This is a generic template declaration
// Using this requires you to initialize it with something
// like:   MyGenerator<double> mygenerator;
template <typename T>
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 <double>
{
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<double> myvector(5,0.0);
    // notice the <double> 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<double> 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<double> 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

1
2
3
4
5
6
#ifndef mygenerator_hh_
#define mygenerator_hh_

// ... MyGenerator class definition...

#endif 


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

1
2
    MyGenerator<double> 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:

1
2
3
4
5
6
7
8
9
10
11
#include <vector>
#include <boost/assign/std/vector.hpp>

using namespace boost::assign;

int main(int argc, char* argv[])
{
	std::vector<double> 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.