std::vector<std::pair<std::string > >
可用于存储一对字符串的列表。有没有类似的方式来存储字符串的三元组列表?我能想到的如何存储三个字符串集合的列表?
一种方法是使用std::vector
std::vector<std::vector<std::string > > v (4, std::vector<std::string> (3));
但这不会让我使用handly first
和second
存取。
所以,我写我自己的类
#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;
}
我有两个问题
- 可以将我的类代码以任何方式加以改进?
- 有没有比上述三种方式更好地存储三串字符串的方法?
'std :: tuple'是C++ 11中的新增功能,但应该在大多数比几年前更新的编译器中可用。 – 2012-03-23 07:43:37