Function returning vector with type from template

Hey. I want to simply make a function that will return a random vector of given type and sizes, but I may suck with templates & oop in c++. I tried something like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int RandomNumber() { return (rand()%100); }
template<typename T>
vector<int>* rand_vec(size_t _SIZE, T)
{
    vector<T> ret_vec(_SIZE);
    generate(ret_vec.begin(), ret_vec.end(), RandomNumber);
    return ret_vec;
}

int main()
{
    srand(time(NULL));
    vector<int_fast8_t> random_vec = rand_vec(8, int_fast8_t);

    for (auto X : random_vec) cout << X << "\t" ;
    return 0;
}
Last edited on
(1) You don't pass the type of the template as a function parameter, you either pass it as a template parameter, with identifier<type>(...), or the compiler can sometimes implicitly know which type is being passed (based on the types of objects being passed in).

(2) Also, don't return a pointer to a local object. Just return the object.

(3) Despite making a vector<T>, you always attempt to return a vector<int>. Make the return type match the object you're turning.

(4) Do not create an identifier that starts with an underscore followed by a capital letter, or an identifier that has two underscores in a row (your _SIZE variable name is not allowed).

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
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdint>
#include <ctime>
#include <cstdlib>

using namespace std;

int RandomNumber() { return (rand()%100); }

template<typename T>
vector<T> rand_vec(size_t size)
{
    vector<T> ret_vec(size);
    generate(ret_vec.begin(), ret_vec.end(), RandomNumber);
    return ret_vec;
}

int main()
{
    srand(time(NULL));
    vector<int_fast8_t> random_vec = rand_vec<int_fast8_t>(8);

    for (auto X : random_vec) cout << X << "\t" ;
    return 0;
}

Note that you're attempting to print out "int_fast8_t" objects, which I assume ultimately become chars, so it's going to be printing out potentially non-printable ascii characters.
Last edited on
Using C++ random:

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
include <iostream>
#include <algorithm>
#include <vector>
#include <random>

auto Rand {std::mt19937 {std::random_device {}()}};

template<typename T>
auto rand_vec(size_t size, T mn, T mx)
{
	const auto randNo {std::uniform_int_distribution<T> {mn, mx}};
	std::vector<T> ret_vec(size);

	generate(ret_vec.begin(), ret_vec.end(), [&randNo]() {return randNo(Rand); });
	return ret_vec;
}

int main()
{
	const auto random_vec {rand_vec<short>(10, 0, 100)};

	for (const auto X : random_vec)
		std::cout << X << "\t";

	std::cout << '\n';

	const auto random_vec1 {rand_vec<int>(10, -10, 10)};

	for (const auto X : random_vec1)
		std::cout << X << "\t";
}



57      17      89      25      80      80      20      99      97      77
2       3       -9      -6      10      -4      -8      7       6       -5

Thank you for help. Indeed Idk how to print int_fast8_t with cout but it works like a charm with int specifier for printf.
You can just do cout << (int)my_8_bit_var;
Topic archived. No new replies allowed.