您可能想要制作一个std::map
,其中前4列为关键字,第5列为值。我已将列为std::string
和int
类型,但您可以将其推广到任何您喜欢的内容。
#include <map>
#include <utility>
#include <tuple>
#include <iostream>
#include <string>
typedef std::map< std::tuple<std::string, std::string, int, int>, int> Table;
int main()
{
Table my_table;
std::string a = "Kode", b = "Warrior";
int c = 3, d = 4, e = 5;
// 1. Support insert operations for each row.
my_table.insert(std::make_pair(std::make_tuple(a, b, c, d), e));
// 2. Given the values for Col1, Col2, Col3, Col4 find the value of Col5
auto it = my_table.find(std::make_tuple(a, b, c, d));
std::cout << it->second; // prints e
// 3. Given Col1, Col2, COl3, Col4 update Col5
it->second = 6; // assign some other value
}
Ideone的输出。
一个很大的缺点(但它不符合您的要求):它不支持列插入,所以它不是电子表格的好模型。您可以尝试使用std::map< std::vector<std::string>, std::string>
作为@NarutSereewattanawoot在评论中提到的内容。你可以修改你的代码来支持它,但是你需要一些初始化列表机制来使make_vector具有紧凑的查找语法。 OTOH,std::vector
作为一个关键的缺点是你需要类型同质性std::tuple
避免。如果你想得到真正的幻想,你可以有一个std::vector<boost::any>
作为灵活的类型和列大小的关键。
[你有什么试过](http://mattgemmell.com/2008/12/08/what-have-you-tried/)? – TemplateRex 2013-05-03 21:26:02
也许Boost.MultiIndex? – 2013-05-03 21:26:48
什么,你的意思是某种['std :: map'](http://en.cppreference.com/w/cpp/container/map)?然而,你的第二个约束似乎暗示你需要一些自定义的东西。 – 2013-05-03 21:27:55