An alternative way to keep members in a data structure

I need to remember value pairs for n instances. I know one solution, either by creating a separate class or structure, declare a 2-member variable and put it in a list or array.

But is there any other effective way to do this in C ++ / VC ++ MFC?

+4
source share
6 answers

You can create a pair using std::pair<X,Y> or std::make_pair(T1, T2) . You can then save these pairs in the data structure of your choice, since you want to change

 std::vector<std::pair<X,Y> > or std::set<std::pair<X,Y> > 
+2
source

If the pairs of values mentioned in the question mean integer values, I think you can use CArray from CPoint or CSize due to the readability of the code.

Code example:

 CArray<CPoint, CPoint> Array; Array.Add(CPoint( 2, 3 )); Array.Add(CPoint( 2, 4 )); 
+2
source

You can use tr1 :: tuple .

Also described in the article here .

+1
source

There are many other ways to do this, but this is probably one of the most effective ways. I can think of several really ineffective ways of doing this.

0
source

You can do worse than check Boost :: Tuple <>

A tuple (or n-tuple) is a collection of elements of a fixed size. Pairs, triples, fours, etc. - tuples. In a programming language, tuple is a data object that contains other objects as elements. These element elements can be of different types.

Tuples are convenient in many cases. For example, tuples easily define functions that return more than one value.

0
source

If one of the values ​​is the key for the other, you can use the Dictionary data structure or another map type structure.

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx (.NET only)

As MSalters pointed out, MFC also has its own card type, CMap .

Also check std::map if you want to use something like this in code other than CLI or MFC.

0
source

All Articles