Copy structure to a different structure [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 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;

Related

How can I ensure range1 is smaller than range2? [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 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.

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.

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);

One way swap for vectors, is it possible? [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 6 years ago.
Improve this question
In C++98, this is what I want to do (this does not work as _M_start, etc are private members):
void vectormove(std::vector<int>& a, std::vector<int>& b)
{
a._M_impl._M_start = b._M_impl._M_start;
a._M_impl._M_finish = b._M_impl._M_finish;
a._M_impl._M_end_of_storage = b._M_impl._M_end_of_storage;
}
This is how I want to call
//vectors are predefined already
std::vector<int> a;
std::vector<int> b(10000000,20);
//in later part of code
//a=b; //expensive copy
//std::swap(a,b); //inexpensive swap
vectormove(a,b); //i need an inexpensive one way swap instead
I may have butchered terminology by using copy, move and swap loosely, but I dont have a better grasp on this subject. I hope I was able to convey my intent.
It looks like you just want a to refer to b's internals. The way to do that is just:
std::vector<int>& a = b;
if you want a to take over b's internals, that's:
std::vector<int> a = std::move(b);
which in C++03 could be implemented as a swap with an empty vector:
std::vector<int> a;
using std::swap;
swap(a, b);
If your question is how to write a compilable implementation of your vectormove(), it's like this:
// NOT USEFUL CODE
void vectormove(std::vector<int>& a, std::vector<int>& b)
{
memcpy(&b, &a, sizeof(std::vector<int>));
}
But this is not useful, because once you've done this you are on the path to undefined behavior. Perhaps double-free, perhaps use-after-free, etc.
It seems like you would be best served either by the regular a.swap(b) or by storing a as type int* instead, like this:
std::vector<int> b(10000000,20);
std::pair<int*, int*> a(&b[0], &b[b.size()]);
Now you have an a which can be passed around cheaply and refers to the original vector. Just make sure you don't invalidate a by modifying b afterwards.