0

I need to store an indeterminable number of triangles in a vector, therefore, it would be something like:

std::vector<Point[3]>

But that is not valid C++98. What are other solutions that do not involve a triangle struct?

Thanks

cHao
  • 84,970
  • 20
  • 145
  • 172
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • Why would you not use a struct? – Oliver Charlesworth Aug 13 '11 at 20:22
  • 1
    I'd create a new type Triangle that holds the 3 points, personally, and store that in the vector. – Joe Aug 13 '11 at 20:22
  • Check if your compiler comes with ``... – Kerrek SB Aug 13 '11 at 20:46
  • This is a duplicate of ["Pushing a static array into a std::vector?"](http://stackoverflow.com/questions/3191535/pushing-a-static-array-into-a-stdvector) asked by none other than yourself. It's good to see that our help has been useful to you and that you've learned from having asked so many questions. – James McNellis Aug 13 '11 at 20:58

2 Answers2

5

Try a boost::array<Point, 3>. That should do the necessities.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • @Ajay I think boost is designed to conform to C++ 98 – jmasterx Aug 13 '11 at 20:26
  • Yes, but the implementation of `array` - Probably that will work. But not sure if compiler would support all of complicated template stuff. – Ajay Aug 13 '11 at 20:29
  • @Ajay: Any compiler that can compile `std::vector` can compile `boost::array`. – Puppy Aug 13 '11 at 20:41
  • 1
    I do understand. But I don't know since when `boost::array` was in Boost library. It means if I get latest Boost, it may or may not compile on all compilers. May be Boost libraries are smart enough to enable/disable features depending on compiler version - but which compiler? That would/might break the portability. – Ajay Aug 13 '11 at 20:45
  • 1
    @Ajay: Boost goes to great lengths to remain portable, and `array` is a trivial template. – Puppy Aug 13 '11 at 20:50
  • I don't agree. VC6 couldn't compile most of template stuff, and Boost uses complicated template magic. – Ajay Aug 14 '11 at 08:11
  • 1
    @Ajay: `array` is a very, very trivial class template. Any compiler which has the necessary wherewithall to compile `std::vector` can handle `boost::array`. `array` does not use any complicated template magic, at all. Boost does have some such, but you need to actually know where it is. Furthermore, Boost has macros for telling you what compiler you're on so that you can have code that compiles for that compiler. Besides, if you're running VC6, then you seriously have to upgrade anyway. – Puppy Aug 14 '11 at 09:45
2
  • You may define a struct
  • You may use pair<pair<Point,Point>,Point> type
  • If you can use latest C++, there are many options and one of them is using std::tuple. If there are more of such cases, you may implement your own tuple class taking 3 or more types.
Ajay
  • 18,086
  • 12
  • 59
  • 105
  • 3
    `pair,Point>` - please, no, no, no! – Oliver Charlesworth Aug 13 '11 at 20:31
  • That depends on perspective. I know it would be complex to understand what `.first.second` would mean in such case. That's why I suggested `tuple`. – Ajay Aug 13 '11 at 20:33
  • Although I would never use a pair of pair point, and I was aware of tuple, and making a struct, I will accept your answer because you did not play it safe and go straight to boost :) – jmasterx Aug 13 '11 at 20:37