Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am currently coding an algorithm that involving a recursive class. In Matlab, I can put a struct inside a struct, but I wonder if I can do the same thing in C++.
For example, I have a square matrix which can be divided into 4-smaller matrices. And each small matrix will be again decomposed into 4-smaller ones. The matrix will be divided until it has the pre-defined size. As the result, I can represent a matrix under a hierarchical tree.
In order words, I can say that each matrix has its own children and properties. Can you guide me an efficient way for programming this kind of problem. As the speed is very important for the algorithm, I am still seeking for a fastest way to implement the algorithm.
Thank in advance.
Kind Regards,
You can use pointers. Or containers.
struct Matrix
{
// some members
Matrix* child; //version 1
std::vector<Matrix> Children; //version 2
}
It can easily be done in C and C++ using pointers:
struct some_struct
{
int some_field;
double some_other_field;
struct some_struct *some_pointer_to_struct;
};
In C++ you can use normal standard container for this, if you want more than one:
struct some_struct
{
int some_field;
double some_other_field;
std::vector<some_struct*> collection_of_struct_pointers;
};
The important thing here is that you have to use pointers, as using the structure directly in itself (like struct some_struct foo;) doesn't work until the structure is fully defined, and it's not fully defined until the closing brace.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to create a structure of array dynamically but i dont know the exact size so it will be added whenever i want...
Consider the following example..
Struct abc
{
double **ptrPoints;
int size;
};
i am defining pointer variable
abc* obj;
i dont know the exact size will be so i can not defile like
obj = new abc[size];
the elements will be added whenever condition satisfied.. i want it like vector but i dont want to use it ....
Please suggest me any way to write the functionality like this...
Thank u
Look up vector. Does all the leg work for you.
Wonder why you do not want to use vector. But you may initially declare array of sufficient size. Then maintain a counter of the elements being used up. If it has no space then use realloc.
Seems the only solution except vectors.
See THIS for realloc
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I have this test and while studying I came up with the following question that I don't even understand (nor the question or the answer)
I know coding, I just don't understand all the nonsense they try to teach us.
So the question is:
Q: The class string has pointers to an array of char. Which of the three copy control solutions for pointers, does the class must define?
A: value_like semantics
I do know that string is an array of chars, but I don't know what are "copy control solutions", I'm not even sure what is copy control (copy constructor maybe??) and what is value_like semantics??
Hope the question makes sense, it was translated from Hebrew.
Thanks for your help :)
"Copy control solutions" probably refers to implementation strategies for copy constructor and copy assignment. I would assume that your textbooks at some point list the three solutions for pointers, given the peculiarly specific wording of the question.
Therefore, look them up and understand what they are about. Knowing how to implement copying for resource-managing objects is one of the most essential language-specific skills for a C++ programmer.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to create a list class in C++ similar to the lists in Java. Is there a way I can have it be able to list whatever object it wants to? The class resizes arrays to create the list, but what I need to do is find out the kind of object that's needed to store.
Yes, C++ has templates that can be used to create generic containers roughly similar to Java generic containers.
While your immediate reaction might be to assume that std::list is similar to a Java list, that would be a mistake. In Java, a list basically just means a sequence. In C++, a std::list is a linked list (which is rarely useful). Most of the time you want to use an std::vector (which is more like Java's ArrayList).
Yes, there is, and it's called Templates
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have experience in Java, but not in C++ and unfortunately I have to write small application in C++ for Tizen. The problem is I have to store data as follows:
data should be stored in one object
ideal object would be java ArrayList (or LinkedList) of ArrayList of Points
How can I achieve that in C++?
Could you propose any sample declaration, definition and get(), add() examples? Is the following a good way to do that:
std::vector<std::vector<Tizen::Graphics::Point> > __strokes;
Use the std::vector class from the standard library
std::vector is a sequence container that encapsulates dynamic size arrays.The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets on regular pointers to elements. This means that a pointer to an element of a vector may be passed to any function that expects a pointer to an element of an array.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am working on the cutting problem, and I need to figure out how
to represent the solution.
For example look at this image, where the gray areas are unused material.
Can you please recommend me possible representations? By the way I am using c++ for this.
Thanks
You could use a vector of structs std::vector<sub> areas; like
struct sub
{
size_t x, y;
size_t extent_x, extent_y;
sub (void) : x(0U), y(0U), extent_x(0U), extent_y(0U) { }
};
Where (x,y) as well as (x+extent_x, y+extent_y) are mapped on the Points of the total image.
This vector may either store used or unused parts of the Image.
The 2D image looks like a system memory. Gray area is un-allocated memory and white space is allocated memory. The solution can be similar to memory management done by OS.