-4

I have a simple class that is the concatenation of 2 objects from other libraries:

class MyClass {
public:
   Thing1 thing1;
   Thing2 thing2;  
};

What I want is to be able to ensure that when I plow through a list of thing1s and I want to know about something in the associated thing2, I can get the right answer. So I make a vector out MyClass:

std::vector<MyClass> myClasses; 

So far so good. Now I want to populate this vector's thing1 members from a vector of thing1s. All I could come up with is:

MyClass someStuff;
for(unsigned i=0; i<thing1Vec.size(); i++) {
    someStuff.thing1 = thing1Vec[i];
    myClasses.push_back(someStuff);
}

This works, but it seems inelegant and non-C++ to me.

I thought about inheriting from Thing1 and Thing2, but I have to pass instances to other methods later on. I don't know how else to set up the class that allows for this.

I'm a bit of a C++ newb, so I could be thinking about this problem all wrong. If anyone has better code fo this kind of thing, I'd love to see it.

I think this is exactly why people use Python.

dmedine
  • 1,430
  • 8
  • 25
  • It's not clear how you populate `thing2`. Is that in another vector the same size as `thing2Vec`? – paddy Feb 25 '16 at 00:55
  • who cares about that? the point is that I can't just have a vector of thing1s – dmedine Feb 25 '16 at 00:56
  • "I think this is exactly why people use Python.", use Python. – Acha Bill Feb 25 '16 at 01:00
  • Excuse me, but it may very much matter, because you have asked for a tidy answer. If the two are parallel vectors, there _is_ a very nice solution. If the relationship is different, then _other_ solutions are more appropriate. If you are asked for clarification on an unclear part of your question by people who have a lot of C++ experience, it is a little rude to say "who cares". – paddy Feb 25 '16 at 01:06
  • I apologize -- I didn't mean to be rude, just being direct. I suppose you are right and that I was thinking about this problem incorrectly. But I would still like to know if there is a more idiomatic way to shove members of a class into the objects in a vector of that class than what I described in the question. – dmedine Feb 25 '16 at 01:09
  • It would help greatly if you would explain what the actual relationship of `thing2` is. Are they in a vector? Or is there only one? – paddy Feb 25 '16 at 01:13
  • thing2 would also be in a vector, but the sizes might not actually match. I think pm100 gave me the answer I am looking for. However, I'm not sure the objects will actually 'map nice as he says.. – dmedine Feb 25 '16 at 01:15
  • How do you determine which element of `thing2Vec` will be associated with an element of `thing1Vec`? – paddy Feb 25 '16 at 01:19
  • If `thing1vec` and `thing2vec` are of different sizes and you still want to associated `thing1` to `thing2`, then map the first `n` elements in the both vectors (n is the size of the smaller vector) and leave out the rest. Or maybe you have a way to determine the which elements go in the mapping. – Acha Bill Feb 25 '16 at 01:22
  • Well, I have a potential answer for you but it depends on how you describe the true relationship between elements in your two vectors. – paddy Feb 25 '16 at 01:46
  • I ended up structuring the whole method differently. I thought this would be an interesting question (I was curious as to the answer) but I guess not. I will continue to work with vectors in my Luddite fashion. Thank you all for your help. – dmedine Feb 25 '16 at 21:10

1 Answers1

3

it sounds like you want to make a relationship between a thing1 and a thing2. putting them in the same class is not necessarily the right way to do it

how about

std::map<Thing1, Thing2> thingrel;
// create map
thingrel[t1_1] = t2_1;
.....
// lookup later
Thing2 t2_x= thingrel[t1_x];

Its hard to say if this is right - since your code is so abstract

you will need to make sure Thing1 is 'map nice' . see std::maps with user-defined types as key

Community
  • 1
  • 1
pm100
  • 48,078
  • 23
  • 82
  • 145