Data structures 表的数据结构?
我需要创建一个包含许多行和两个以上列的表。建议一个良好且快速的数据结构。我不会更新和删除该表中的某些条目。我将只使用查找函数。 例如,我有一个表: 我有:Data structures 表的数据结构?,data-structures,tabular,Data Structures,Tabular,我需要创建一个包含许多行和两个以上列的表。建议一个良好且快速的数据结构。我不会更新和删除该表中的某些条目。我将只使用查找函数。 例如,我有一个表: 我有: 最后,c中的值应为asgf。必须使用基于哈希表的关联数组,即所谓的字典。查找的平均时间复杂度-最坏情况下为O1+n/k和On。必须将表组织为以列名为键的列字典。列必须是以行名称为键的值字典 更多信息: C中的示例: using System; using System.Collections; using System.Collectio
最后,c中的值应为asgf。必须使用基于哈希表的关联数组,即所谓的字典。查找的平均时间复杂度-最坏情况下为O1+n/k和On。必须将表组织为以列名为键的列字典。列必须是以行名称为键的值字典 更多信息: C中的示例:
using System;
using System.Collections;
using System.Collections.Generic;
namespace Test {
public class Test
{
class Table {
class Column {
public Dictionary<string, string> cells;
public Column() {
cells = new Dictionary<string, string>();
}
public string Find(string rowName) {
string resultValue;
if (cells.TryGetValue(rowName, out resultValue))
return resultValue;
else
throw new Exception("oops, no such cell");
}
}
Dictionary<string, Column> columns;
List<string> rowNames;
public Table() {
columns = new Dictionary<string, Column>();
rowNames = new List<string>();
}
public void AddColumn(string columnName, params string[] values) {
Column column = new Column();
columns.Add(columnName, column);
// fill new cells
int counter = 0;
foreach (string rowName in rowNames) {
if (counter < values.Length)
column.cells.Add(rowName, values[counter]);
else
column.cells.Add(rowName, "");
counter++;
}
}
public void AddRow(string rowName, params string[] values) {
rowNames.Add(rowName);
// fill new cells
int counter = 0;
foreach (KeyValuePair<string, Column> columnPair in columns) {
Column column = columnPair.Value;
if (counter < values.Length)
column.cells.Add(rowName, values[counter]);
else
column.cells.Add(rowName, "");
counter++;
}
}
public string Find(string columnName, string rowName) {
Column resultColumn;
if (columns.TryGetValue(columnName, out resultColumn))
return resultColumn.Find(rowName);
else
throw new Exception("oops, no such cell");
}
}
public static void Main()
{
Table table = new Table();
table.AddRow("a");
table.AddRow("b");
table.AddColumn("column 1", "asd", "asgf");
table.AddColumn("column 2", "awd", "aasf");
table.AddColumn("column 3", "asfc", "asgfc");
Console.WriteLine(table.Find("column 1", "b") );
}
}
}
这完全取决于要存储的数据。您需要提供有关您要执行的操作的更多信息。查找条件是什么?我将查找列中的特定条目。就像数据库一样。我将具有行的“id”和“列名”,并且需要该表中此特定项的值。多个哈希表?哈希表:id-column1、id-column2、id-column3。查找将采用O1+n/k平均值,在最坏的情况下为On。en.wikipedia.org/wiki/Hash_table p.S:Example不抵抗添加重复的行和列当您为每个单元格和行分配非常大的对象时,似乎效率不高。另外,您将列名作为键复制到所有位置。我宁愿创建一个类,其中包含用于列名的内部数组,以及用于记录的列表。该列表可以很容易地交换为向量或链表,以便更快地查找。 String a = "column 1" String b = "b" String c = find(a,b);
using System;
using System.Collections;
using System.Collections.Generic;
namespace Test {
public class Test
{
class Table {
class Column {
public Dictionary<string, string> cells;
public Column() {
cells = new Dictionary<string, string>();
}
public string Find(string rowName) {
string resultValue;
if (cells.TryGetValue(rowName, out resultValue))
return resultValue;
else
throw new Exception("oops, no such cell");
}
}
Dictionary<string, Column> columns;
List<string> rowNames;
public Table() {
columns = new Dictionary<string, Column>();
rowNames = new List<string>();
}
public void AddColumn(string columnName, params string[] values) {
Column column = new Column();
columns.Add(columnName, column);
// fill new cells
int counter = 0;
foreach (string rowName in rowNames) {
if (counter < values.Length)
column.cells.Add(rowName, values[counter]);
else
column.cells.Add(rowName, "");
counter++;
}
}
public void AddRow(string rowName, params string[] values) {
rowNames.Add(rowName);
// fill new cells
int counter = 0;
foreach (KeyValuePair<string, Column> columnPair in columns) {
Column column = columnPair.Value;
if (counter < values.Length)
column.cells.Add(rowName, values[counter]);
else
column.cells.Add(rowName, "");
counter++;
}
}
public string Find(string columnName, string rowName) {
Column resultColumn;
if (columns.TryGetValue(columnName, out resultColumn))
return resultColumn.Find(rowName);
else
throw new Exception("oops, no such cell");
}
}
public static void Main()
{
Table table = new Table();
table.AddRow("a");
table.AddRow("b");
table.AddColumn("column 1", "asd", "asgf");
table.AddColumn("column 2", "awd", "aasf");
table.AddColumn("column 3", "asfc", "asgfc");
Console.WriteLine(table.Find("column 1", "b") );
}
}
}