开发者

Alternative way to hold members in a data structure

I need to remember pairs of values for n instances. I know one solution i.e either making a separate class or structure, declare 2 member variable & place it in a list or array.

But is there any other efficient way of doing this in C++/V开发者_JAVA技巧C++ MFC?


You can create a pair using std::pair<X,Y> or std::make_pair(T1, T2). You can then store these pairs in data structure of your choice as you would like to modify the

std::vector<std::pair<X,Y> > or std::set<std::pair<X,Y> >


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

Sample code:

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


You may use tr1::tuple.

Also, described in my article here.


There are loads of other ways of doing it but thats probably one of the most efficient way. I can think of several really inefficient ways of doing it.


You could do worse than check out Boost::Tuple<>

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

Tuples are convenient in many circumstances. For instance, tuples make it easy to define functions that return more than one value.


If one of the values is a key for the other, you could use a Dictionary or other map-type data structure.

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

As MSalters pointed out, MFC has its own map type as well, CMap.

Also check out std::map if you want to use something similar in non-CLI or MFC code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜