【问题标题】:How to retrieve a value from a vector of vectors?如何从向量向量中检索值?
【发布时间】:2021-04-18 17:37:35
【问题描述】:

我正在尝试制作一个 vector,其中 包含 个向量,其中包含一个 string。我不确定如何检索所述字符串。我已经尝试过 vector.at(),但我不确定如何制作一种 '多层' 版本。

这是我代码中的值:

std::vector<std::vector<std::string>> dialoguestore;

【问题讨论】:

  • vector.at 为您提供另一个向量,因此请再次调用at
  • 重复应用[] 索引运算符怎么样:std::string s = dialoguestore[0][0];
  • 哦,是的,这很有道理。

标签: c++ string vector


【解决方案1】:

使用 2D 矢量有很多方法可以解决这个问题。

选项 1:at():

dialoguestore.at(index).at(index2);

选项 2:operator[]:

dialoguestore[index][index2]

如果您有一个多维向量,请使用通常访问向量的方式,但要考虑维数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    相关资源
    最近更新 更多