Using Vector Data Structures - Design and Syntax Issues

I have some basic design and syntax issues in C ++, and I will be grateful for your answer.

  • I have N number of regions.
  • Each region must store information about the element

i.e. I want to achieve something like this:

region[i].elements = list of all elements for region i.

Question 1: Does the following syntax look right (see code below) / design. Am I missing something here?

EDIT

The struct elem instances are created by some other class, and its freeing memory is processed by this class, only I just want to access this object and its members using reg [i]. list (vector) ... so how should I add these element elements to vector "elements" in the Region class?

// Already have this structure that I need to use

 struct elemt { int* vertex; int foo1; double foo2; }; class Region{ public: // I am not sure what should be the syntax here! // is it correct? std::vector <elemt*> elements; } // Following is the constructor of "class A" A::A(){ // --header file defines: Region *reg; // Let numOfRegions be a class variable. ( changes based on "Mac" suggestion) numOfRegions = 100; //allocate memory: reg = new Region[numOfRegions]; } A::~A(){ delete [] reg; reg = NULL; } A::doSomething(){ // here I want to append the elements to the vector // Let i be region 10. // Let e1 be an element of "struct elemt" that needs to be added reg[i].elements.push_back(e1); } 

Question 2: Is the syntax in doSomething() correct? Later I want to run an iterator over all the elements in reg[i] and want to access, e1->foo1, e1->foo2 and the like.

Question 3: In some method, how can I guarantee that e1 is not yet in the "elements"

UPDATE

Some syntax errors have been fixed and, hopefully, a memory leak detected by a Mac user has been fixed.

+4
source share
4 answers

First of all, get rid of a memory leak from the code.

 A::A(int numOfRegions = 100){ m_reg = new Region[numOfRegions]; // define Region *m_reg in the class } A::~A(){ delete [] m_reg; m_reg = NULL; } You are allocating memory in the constructor and storing return address in local variable and it ll get destroyed when its scope is over . You should store the base address so that you can delete it . 
+5
source

How do you know if two elements are the same or not?

From your discovery question, if e1 is already present in the elements, it looks like map / set / hash might be a better data structure than a vector.

In addition, I believe that this is push_back, not pushBack.

+2
source

You must separate the syntax from the design question. Syntax is what the compiler checks and applies, but again, you may get unwanted semantics ...

Focusing on design, what are your requirements? (Sorry, I tend to read diagonally if there are several lines, so I might have missed something)

There is a class that creates / destroys elemt objects, is dynamic allocation really necessary? or is it just because the elements are built with data available in the build class? If dynamic allocation is a requirement, then it looks like elemt objects should be held by a pointer (possibly smart pointers). Is it a class designed to remove objects, track them, or does it just process objects in the outside world, expecting the user code to call the dellocator function? Are elemt objects shared between different objects? And there are other issues that you should consider ...

Perhaps you should try to change your question, the code you posted is mostly syntactically correct (just check with the compiler, I didn’t), but without knowing what you really want to achieve, you really cannot ask if it is a good or bad design. Describe your real problem, and then ask if the code could be the solution to your requirements.

+1
source

When you request syntax, I can immediately notice the missing half-line.

Also, publicly available data (such as items) is not recommended. Instead, try using the getter and setter functions.

The numOfRegions parameter must be const int , not just int . It is also disabled to declare it in c'tor instead of declaring a class, as this is a custom setting. As soon as you want to iterate over reg, you will have to update it if you save it as it is now.

Regarding the third question, you just need to check if you can find it. You might need bool operator==(const elemt &, const elemt &) to make this convenient.

0
source

Source: https://habr.com/ru/post/1300717/


All Articles