std::vector<std::pair<std::string > >
can be used to store a list of a pair of strings. Is there a similar way to store a list of triplets of strings?
One way I can think of is to use std::vector
std::vector<std::vector<std::string > > v (4, std::vector<std::string> (3));
but this will not allow me to use the handly first and second accessors.
So, I wrote my own class
#include <iostream>
#include <vector>
using namespace std;
template <class T>
class triad {
private:
T* one;
T* two;
T* three;
public:
triad() { one = two = three =0; }
triad(triad& t) {
one = new T(t.get1());
two = new T(t.get2());
three = new T(t.get3());
}
~triad() {
delete one;
delete two;
delete three;
one = 0;
two = 0;
three = 0;
}
T& get1() { return *one; }
T& get2() { return *two; }
T& get3() { return *three; }
};
int main() {
std::vector< triad<std::string> > v;
}
I have two questions
- Can my class code be improved in any way?
- Is there any better way to do store triplets of strings than the three ways described above?
std::tupleis a generalization ofstd::pairthat lets you store a collection of some size that you specify. So, you can say:to get a type
Triadthat stores three strings.