0

Trying to figure out a class declaration for the following code:

vector<int> myinputs;
random_vector(k,100,myinputs);
print_vector(myinputs);

So far I have this but I don't know if I'm heading in the right direction, or if I am where to go from here:

template <class T>
class RandomVector
{
 public:
    T random_vector();
    void print_vector(vector<T> & data);
};

template <class T>
void RandomVector<T>::print_vector(vector<T> & data)
{
    typename vector<T>::iterator it;
    for (it= data.begin(); it != data.end(); it++)
        cout << *it;
}

template <class T>
T RandomVector<T>::random_vector()
{
    vector<T> data(srand);
    for (int i=0; i<data.size(); i++)
        data[i] = srand;
}
  • 2
    I guess you don't need any class, you need template functions `random_vector` and `print_vector` – quantdev Jun 06 '14 at 00:40
  • 1
    `vector & data` should be `const vector & data` as well. You're not modifying the vector, so limiting what the function can be used with is pointless. Also, I'm not sure what you're trying to accomplish by setting things to `srand`. That's a function for setting the seed of the C pseudorandom number generator. – chris Jun 06 '14 at 00:41
  • you've missed 'inline' in function implementations as they normally go into .h files. – c-smile Jun 06 '14 at 00:44
  • 1
    @c-smile, Function templates are inlined automatically, seeing as how they need to "be in a header". – chris Jun 06 '14 at 00:45
  • By the way, consider `std::copy` in place of `print_vector` and `std::generate_n` in place of the loop in `random_vector`. – chris Jun 06 '14 at 00:46
  • @chris implementation of functions or methods outside of a class has to have 'inline' specifier. If you don't want to see `warning LNK4006: "XXX already defined in YYY.obj; second definition ignored"`. And if you have "treat warnings as errors" enabled it wont even compile. Cheers. – c-smile Jun 06 '14 at 01:09
  • 1
    @c-smile, But not templates. – chris Jun 06 '14 at 01:13
  • @chris the srand is used to create a vector of random values. srand is what I'd use to that isn't it? – user3708410 Jun 06 '14 at 17:13
  • @user3708410, Calling `srand` sets the seed. Calling `rand` generates a (not very) random number. Assigning `srand` without calling it does neither of those. – chris Jun 06 '14 at 17:36
  • @chris What does "But not templates" mean in this respect? I am speaking about precisely this construct: `template inline void RandomVector::print_vector(vector & data) ...`, it should have inline specifier to avoid warnings in at least VC++ – c-smile Jun 06 '14 at 19:40
  • @c-smile, Inline might be the wrong word for it. Function templates do not automatically get the `inline` keyword like normal member functions defined inside the class definition do, but they are allowed to be defined more than once in a TU (see [this](http://stackoverflow.com/questions/10535667/does-it-make-any-sense-to-use-inline-keyword-with-templates)). Sucking up to ODR is about the only purpose of the `inline` keyword nowadays and function templates do that on their own. – chris Jun 06 '14 at 19:47

1 Answers1

1
#include <iostream> 

template<typename T>
void print_thing(const T& v){
    for(auto x : v)
        std::cout << x << ',';
}

This is very simple and will leave you with a trailing comma.

But it certainly gets the job done!

You can also go through other data types with this, such as linked lists, binary trees, etc.

druckermanly
  • 2,694
  • 15
  • 27