Marcos Sánchez Marcos Sánchez - 10 months ago 79
C++ Question

STL containers vs Structs

I would like to know what is better to use and why:

Before I used to create types with STL Containers, but recently I've found that APIs use struct like types, which I think is due to the use of C instead of C++, but I want to know what is best in terms of performance and memory.


typedef std::tuple<std::string, int, int, bool> SomeType;


typedef struct tagSomeType { std::string sVar; int iVar, iVar2; bool bVar; } SomeType;

Thank you

[EDIT] I put this example of tuple to make it visible, but for example

typedef std::pair<int, int> iCoord; /* VS */ struct iCoord{int x, y;};

Seems that the only diference is that struct has more readability than STL Container

Answer Source

There is no significant difference in memory usage and performance. So the main criterion should be readability.

One difference is that in a struct, each element has a name, which is not the case for tuples. Especially if you have more that 2 or 3 elements, this can make structs more readable.

By the way, your struct declaration is C style. In C++ you can write it like this:

struct SomeType { std::string sVar; int iVar, iVar2; bool bVar; };