Cast
Nov 9, 2020 at 1:47pm UTC
In the first version my code gives me double. In the second version it gives me int. Why that?
1 version:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
std::cout << "Making and changing of container with the help of methods" << std::endl;
std::vector<double > v_each (5);
std::vector<double >::iterator pos1;
out << "Generate()" << std::endl;
std::generate(v_each.begin(), v_each.end(), Random);
bool flag1 = false ;
for (pos1 = v_each.begin(); pos1 != v_each.end(); ++pos1) {
auto minmax = std::minmax_element(v_each.begin(), v_each.end());
Avarage1 = (double )((*minmax.first + *minmax.second) / 2.0);
if (!flag1) {
out << "MAX = " << (double )*minmax.second << std::endl;
out << "MIN = " << (double )*minmax.first << std::endl;
out << "Avarage = " << (double )Avarage1 << std::endl;
flag1 = true ;
}
out << (double )*pos1 << std::endl;
}
out2 << "\nChanging of container with the help of method for_each()" << std::endl;
std::for_each(v_each.begin(), v_each.end(), AvarageSum1);
outfile(v_each);
2 version:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
std::list<double > v_transform (5);
std::list<double >::iterator pos2;
out << "\nGenerate()" << std::endl;
std::generate(v_transform.begin(), v_transform.end(), Random);
bool flag2 = false ;
for (pos2 = v_transform.begin(); pos2 != v_transform.end(); ++pos2) {
auto minmax = std::minmax_element(v_transform.begin(), v_transform.end());
Avarage2 = (double )((*minmax.first + *minmax.second) / 2.0);
if (!flag2) {
out << "MAX = " << (double )*minmax.second << std::endl;
out << "MIN = " << (double )*minmax.first << std::endl;
out << "Avarage = " << (double )Avarage2 << std::endl;
flag2 = true ;
}
out << (double )*pos2 << std::endl;
}
out2 << "\nChanging of container with the help of method transform()" << std::endl;
std::transform(v_transform.begin(), v_transform.end(), v_transform.begin(), AvarageSum2);
outfile(v_transform);
outfile():
1 2 3 4 5 6 7 8 9 10
template <class T>
void outfile(T& Container) {
std::ofstream out("output.txt" , std::ofstream::app);
typename T::iterator it3;
for (it3 = Container.begin(); it3 != Container.end(); ++it3) {
out << (double )*it3 << std::endl;
}
std::cout << "File output" << std::endl;
}
Last edited on Nov 9, 2020 at 1:47pm UTC
Nov 9, 2020 at 2:09pm UTC
What's different in the 2 versions?
Nov 9, 2020 at 2:12pm UTC
What's different in the 2 versions?
It's the same)) but it gives me another types. I don't know why
Last edited on Nov 9, 2020 at 2:12pm UTC
Nov 9, 2020 at 2:27pm UTC
What do you mean by "gives me another types"? Is it the formatting of the output?
Nov 9, 2020 at 2:35pm UTC
What do you mean by "gives me another types"? Is it the formatting of the output?
I have vector and list. I search for max and min then I get (max + min)/2 and It add to all elements of cotainer. But (max + min)/2 can be double. So sum in the end also can be double.
But in vector I get double. In list I get int but It should be double.
Nov 9, 2020 at 3:31pm UTC
What type are Avarage1 and Avarage2 ?
Nov 9, 2020 at 4:44pm UTC
You've divided by 2.0, which is a double, so result is double, not int. Why do you think one of them is an int?
Topic archived. No new replies allowed.