【发布时间】:2017-06-22 10:44:42
【问题描述】:
在 C++ 中,我有一个场景,调用者函数会将多个表的行作为参数发送。并且被调用函数需要将这些行存储在一些数据结构中。 我需要使用动态大小的数据结构,因为每个表的行大小可能不同。
//CALLEE CODE: vector Row here holds the elements of the table, like Row[0] is 1st column of this row, Row[1] - 2nd columns of this row and so on.
Size of this Row might change based on the no: of columns in the table. Hence, I need to use a dynamically sized data structure vector.
void FillTable(Vector Row, bool newTable)
{
if(newTable) // means caller is passing rows of a new table, hence no: of
Cols might change
{
Create a 2D vector called Table which can hold the rows.
Now Assign each Row to the 2D vector.
}
else
{
just Assign the row to existing 2D vecttor.
}
}
CALLER CODE:
.................
FillTable(RowOfATable, true);
while(Keep passing rows for this table)
{
FillTable(RowOfATable, false);
}
FillTable(RowOfATable, true);
while(Keep passing rows for this 2nd table)
{
FillTable(RowOfATable, false);
}
我的问题是向量是在 C++ 中使用的正确数据结构,还是有更好的方法在 C++ 中像任何容器等一样执行此操作?我需要让这个操作系统在 win、linux、mac、android 上运行。
【问题讨论】:
-
std::vector应该是您尝试的默认容器,是的 -
很不清楚你在问什么。将解释与示例代码分开。不要发布 C++ 和伪代码的几乎无法解析的混合。发布实际指示所需结果的伪代码。此外,您为什么不使用
vector?只需使用适合任务的标准容器,直到找到不使用它们的充分理由。您应该默认信任 stdlib 并要求有理由停止使用它,而不是相反。 -
@codeLover:请编辑您的问题以改进它
-
不要过度优化。如果矢量看起来可以工作,那就去做吧。如果结果在某种程度上很混乱,请在此处发布该代码并解释您想要的不同之处。在现有代码的基础上进行交流会容易得多。
标签: c++ c++11 vector data-structures c++14