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
std::tuple
is a generalization of std::pair
that lets you store a collection of some size that you specify. So, you can say:
typedef std::tuple<std::string, std::string, std::string> Triad;
to get a type Triad
that stores three strings.
You can use tuples from boost: http://www.boost.org/doc/libs/1_49_0/libs/tuple/doc/tuple_users_guide.html
(In C++11 tuples are part of standard library: http://en.wikipedia.org/wiki/C%2B%2B11#Tuple_types)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With