【问题标题】:Dynamically sized data types in C++C++ 中动态大小的数据类型
【发布时间】: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


【解决方案1】:

你的问题太笼统了。正如StoryTeller 评论的那样,您可能想要使用std::vector(因为它是标准 C++14 和C++11 的一部分,您可以在任何符合标准的编译器上获得它) .首选standard containers。

或者,您可以使用 closures 并定义一个高阶成员函数,该函数将闭包作为参数并迭代(例如,在每一行上)直到该闭包返回 false。在调用高阶成员函数时,您通常会传递 lambda expression。

(我不知道你的Vector是什么,所以我不能举一个简单的例子;但是......)

这是一个示例,它将std::vector<std::string> vecstr 中的每个元素添加到给定的std::set<std::string> setstr,直到函数predicate 返回true:

void do_every_until (std::set<std::string>& setstr,
                     const std::vector<std::string>& vecstr,
                     std::function<bool(const std::string&)> predicate) {
  for (auto str : setstr) {
     if (predicate(str)) 
        return;
     setstr.insert(str);
  }
}

这是一个使用示例:

std::set<std::string> mysetstr;
std::vector<std::string> myvecstr = something();
do_every_until(mysetstr, myvecstr,
               [&](const std::string& curstr) {
                  // stop iterating when curstr is empty or after K
                  if (curstr.size() == 0 || curstr[0] >= 'K') 
                     return true;
                  // otherwise, add to set
                  mysetstr.insert(curstr);
                  return false;});

最后,您可以在Row 数据结构中定义一些iterator。小心您可能可以在其上使用range-based for loop(通过在Row 中定义适当的begin 和end 成员函数)。

【讨论】:

  • 这与使用像 std::vector<:vector>> 表这样的二维向量有什么不同吗?现在我可以接收向量类型的行并将它们插入到二维向量表中。 ?
  • 我不知道。请注意,像std::vector&lt;std::vector&lt;std::string&gt;&gt; 这样的二维向量不是std::string-s 的矩阵,而是各种大小的向量组成的向量。
猜你喜欢
  • 1970-01-01
  • 2011-03-14
  • 2013-12-17
  • 2019-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
  • 1970-01-01
相关资源
最近更新 更多