How can I ensure range1 is smaller than range2? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to ensure that int range1 must be smaller than int range2 before using this structure. I am new to programming.
struct prime
{
int range1;
int range2;
};

You can write a constructor. Constructors are the place where you should make sure the object is constructed in a valid state:
struct prime
{
prime(int a,int b) : range1(std::min(a,b)), range2(std::max(a,b)) {}
private:
int range1;
int range2;
};
I assumed you just want to make sure that range1 < range2 but assume nothing about the passed parameters. If you want to make sure that a < b you could throw an exception when it is not the case.

Related

Is there a reason why std::priority_queue as MinHeap requires greater than comparer; MaxHeap, less than comparer? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
When I use std::priority_queue as a Min Heap for example, I'd need to do something like this:
struct MyType{
...
int data;
};
auto greaterThanCmp = [](const MyType& a, const MyType& b){ return a > b; };
std::priority_queue<MyType, std::vector<MyType>, greaterThanCmp> minHeap;
My question is: is there an advantage to providing the greaterThanCmp for a Min Heap or would a less than comparer works just as well, had the committee/implementer gone that route.
Or in other words, why did the standard choose one over the other?
I have tried implementing my MinHeap class with a lessThanCmp just for the heck of it. I tested my code, it works fine.
Thank you very much for your response in advance!
Once you have a greater object, you can easily make a less object out of it:
less ::operator() (a, b) { return greater (b, a) }. So the less requirement is not limiting.

why does'nt this output the max element of the vector? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
#include <bits/stdc++.h>
using namespace std;
int main()
{
int b;
vector<int>={32,3,4,34,32,5465,6543}
b=d[*max_element(d.begin(),d.end())];
cout << b;
}
why won't this code print the max element of the vector d.
Because what std::max_element() returns is an iterator. To access the element you can dereference it via *, which is what you do, but instead of printing that maximum value, you use it to index the vector.
Simply output it: std::cout << *std::max_element(d.begin(),d.end());.

Is it a good practice to instantiate an object with auto? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
solution 1 :
auto a = A()
Solution 2 :
A a;
I was wondering which one of the two solutions is the best manner to instantiate an object ?
I know that the solution 1 calls the default constructor and then the copy constructor, but I really don't know if there is any advantages of writing that.
Edit: I wrote a small class to test by my own and it appears that in accordance with the comments, these two "solutions" have exactly the same behavior.
class A
{
public:
A(){
std::cout <<"default_constructor\n";
}
A(const A &g){
std::cout <<"copy_constructor\n";
}
};
solution 1 :
default_constructor
solution 2 :
default_constructor
In my opinion you should always use solution 2 for this case, as you have to define the type somewhere. I prefer using auto just in cases where the type is already set and I can avoid duplicate the type e.g. getting an element from an container
std::vector<double> myVector{0.0, 1.0};
auto firstElement = myVector.front();
or
auto myInt = static_cast<int>(2.0);

I have used a vector to create a list of class objects. How can I call the constructor with parameters when I initialize the vector? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a class Random. The constructor has a parameter "int range" if I say
class Random
{
private:
int r;
public:
Random(int range)
{
r = range
}
}
I want to create a vector of objects of class Random. How do i pass the range parameter in that case
vector<Random> v;
Where does the parameter go? Bit confused.
There's no great way to do what you want.
Since Random is cheap to copy, you can do this:
vector<Random> v{Random(1), Random(2), Random(3)};
but if it wasn't cheap to copy (or not copyable at all), then you would do this instead:
vector<Random> v;
v.reserve(3);
v.emplace_back(1);
v.emplace_back(2);
v.emplace_back(3);

Copy structure to a different structure [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I was wondering if there was an elegant way to copy a structure to a different structure, where the second structure is essentially the same as the original one, except without the last field(s).
For example,
struct A {
int a;
int b;
int c;
};
struct B {
int a;
};
struct A v1;
struct B v2;
Would,
memcpy(&v2, &v1, sizeof(v2));
Achieve the functionality I wish?
Where v2 has the "a" value that was originally found in v1?
Thank you
If instead of copying all bytes in A, you only copy the number of bytes that B expects, you will achieve your desired result:
memcpy(&v2, &v1, sizeof(v2)); // remember that the first argument is the destination
However, this is not good coding style. With this minimal code example, it is hard to tell, but you would probably want A to inherit from B so that you can convert the two without having to physically copy memory.
Otherwise, this would be easier and cleaner:
b2.a = v1.a;