如何在 C++ 中打印出嵌套向量的内容?

How to print out the contents of a nested vector in c++?

以下是有疑问的片段:

typedef std::vector<unsigned char> QInput; // defined in an external library

std::vector<QInput> extvec; // defined in a separate function

问题一:extvec是嵌套向量吗?

问题2:如何打印出extvec的内容?

我尝试使用 Whosebug 上许多答案中描述的传统方法打印出 extvec 的内容,但我遇到了很多错误。所以我决定这可能是一个嵌套向量。但该表格看起来与 等其他问题不同。

Is extvec a nested vector?

没有 "nested vector" 这样的东西。它是向量的向量。

How do I print out the contents of extvec?

通过遍历其中的向量。

extvec是嵌套向量吗?
是的,你可以这样想象,但不是官方术语。

如何打印出extvec的内容?

在 c++98 中:

for (std::vector<QInput>::iterator it = extvec.begin(); it != extvec.end(); ++it)
    {
        for (vector<unsigned char>::iterator it1 = (*it).begin(); it1 != (*it).end(); ++it1)
        {
            cout << *it1 << endl;
        }
    }

在 c++11 标准中:

for (const auto& v : extvec)
{
    for (auto i : v)
    {
        cout << i << endl;
    }
}