0

I have a struct that is filled with vectors.

struct structtype{

vector<double> vec1;
vector<double> vec2;
vector<int> vec3;

};

I want to be able to take the specific elements from one struct and add them to another. Sort of like

structtype myStructA

myStructA.add(myStructB.at(i))

so I'm building myStructA out of specific elements of myStructB. I'm wondering if within the definition of structtype I can just include something like..

struct structtype{

vector<double> vec1;
vector<double> vec2;
vector<int> vec3;

void Add(structtype myStructtoAdd, int i){
vec1.push_back(myStructtoAdd.vec1.at(i));
vec2.push_back(myStructtoAdd.vec2.at(i));
vec3.push_back(myStructtoAdd.vec3.at(i));
};


};

Will this work? And regardless is there a better way to do this?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
SCFAWKS
  • 17
  • 5
    Maybe you want a vector of structs (of single variables), instead of a struct of vectors. – deviantfan Aug 01 '16 at 08:29
  • 2
    Agree with @deviantfan. The code suggests that all 3 vectors have the same size, and share the same index `int i`. That said, this is only a small part of the program. If other parts operate on one vector at a time, then a structure of vectors is reasonable. – MSalters Aug 01 '16 at 09:05

2 Answers2

1

Yes. It will work, because in c++ struct is very simmilar to class, and they support methods. The difference lies in accessibility of members by default - in class they're private, and in struct they're public. It also applies to inheritance - struct inherits publicly and class - privately (by default - it's still posiblle in both cases to add a desired access specifier)

Giezr
  • 19
  • 5
  • _"`struct` is very simmilar to `class`"_ For the avoidance of doubt, it's because both keywords define a class. I'm not sure what any of this has to do with the question, though. – Lightness Races in Orbit Aug 01 '16 at 10:33
  • I guess I hastily assumed it was a typical question for someone who already knows some oop language without stucts (eg. java), thus is familiar with classes, and now learning c++. Thus comparing the two made sense for me. – Giezr Aug 01 '16 at 14:50
0

Yes, this will work.

About the refinement of this code:

Since you need adding element i from one structure to another, probably you may need any other operations over such element, so it looks sensible to define a struct for it, and a retrieving operator:

struct element
{
    double f1, f2;
    int f3;
}

element& structtype::operator [] (int i)
{
    return {vec1[i], vec2[i], vec3[i]};
}

const element& structtype::operator [] (int i) const
{
    return {vec1[i], vec2[i], vec3[i]};
}

After implementation of these operation you probably may store element somewhere else, not only in the structtype, which means that push_back operation with element argument would be useful:

void push_back(const element& e)
{
    vec1.push_back(e.f1);
    vec2.push_back(e.f2);
    vec3.push_back(e.f3);
}

Also, given that the structtype class behaves like a container, it may be useful to implement some other features typical STL containers provide, like iterators. A good guide for STL-like containers implementation is here.

Or, as a second option, you may just use std::vector<element>, which already implements all of these features.

If its functionality is not enough for you, you may extend it creating a new class which inherites the implementation of std::vector. A standard way of doing it is using a private inheritance, like this:

class structtype : private std::vector<element>
{
public:
    /// Write such lines for all members of std::vector which you would like to have
    using std::vector<element>::size(); 
    ...
    /// Write some your own methods
    std::vector<double> get_vec1() const;
}
Community
  • 1
  • 1
alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51