C++可变大小数组的数组

发布于 2024-08-28 14:38:03 字数 373 浏览 6 评论 0原文

我对 C++ 很陌生,我意识到以下内容不一定像我希望的那么简单,但我真的很感激更专业的意见。

我本质上是试图在可变大小数组的可变大小数组上实现动态迭代,类似于以下内容。

String *2d_array[][] = {{"A1","A2"},{"B1","B2","B3"},{"C1"}};

for (int i=0; i<2d_array.length; i++) {
    for (int j=0; j<2d_array[i].length; j++) {
         print(2d_array[i][j]);
    }
}

有没有合理的方法来做到这一点?也许通过使用向量或其他结构?

谢谢 :)

I am very new to C++ and I realise the following is not necessarily as easy as I'd like it to be, but I'd really appreciate a more expert opinion.

I am essentially trying to achieve a dynamic iteration over a variable sized array of variable sized arrays similar to the following.

String *2d_array[][] = {{"A1","A2"},{"B1","B2","B3"},{"C1"}};

for (int i=0; i<2d_array.length; i++) {
    for (int j=0; j<2d_array[i].length; j++) {
         print(2d_array[i][j]);
    }
}

Is there a reasonable way to do this? Perhaps by using a vector, or another struct?

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友谊不毕业 2024-09-04 14:38:03

您正在使用 C++ 字符串对象的纯 C 数组。 C 语言中没有可变大小的数组。除此之外,这段代码无论如何都不会编译,在这样的构造中,编译器将生成一个声明了最大长度的数组。在示例案例中,

String *2d_array[3][3] 

如果您想要可变大小的数组,则必须使用 C++ STL(标准模板库)容器,例如向量或列表:

#include <string>
#include <vector>

void f()

{
    typedef std::vector<std::string>   CStringVector;
    typedef std::vector<CStringVector> C2DArrayType;
    C2DArrayType theArray;

    CStringVector tmp;
    tmp.push_back("A1");
    tmp.push_back("A2");
    theArray.push_back(tmp);

    tmp.clear();
    tmp.push_back("B1");
    tmp.push_back("B2");
    tmp.push_back("B3");
    theArray.push_back(tmp);

    tmp.clear();
    tmp.push_back("C1");
    theArray.push_back(tmp);

    for(C2DArrayType::iterator it1 = theArray.begin(); it1 != theArray.end(); it1++)
        for(CStringVector::iterator it2 = it1->begin(); it2 != it1->end(); it2++)
        {
            std::string &s = *it2;
        }
}

You are using a plain C array of C++ string objects. In C there are no variable sized arrays. Besides that this code wont compile anyway, in such a construct the compiler will generate a array of arrays that have the maximum length declared. In the sample case that would be

String *2d_array[3][3] 

If you want variable sized arrays, you have to use C++ STL(Standard template library)-containers such as vector or list:

#include <string>
#include <vector>

void f()

{
    typedef std::vector<std::string>   CStringVector;
    typedef std::vector<CStringVector> C2DArrayType;
    C2DArrayType theArray;

    CStringVector tmp;
    tmp.push_back("A1");
    tmp.push_back("A2");
    theArray.push_back(tmp);

    tmp.clear();
    tmp.push_back("B1");
    tmp.push_back("B2");
    tmp.push_back("B3");
    theArray.push_back(tmp);

    tmp.clear();
    tmp.push_back("C1");
    theArray.push_back(tmp);

    for(C2DArrayType::iterator it1 = theArray.begin(); it1 != theArray.end(); it1++)
        for(CStringVector::iterator it2 = it1->begin(); it2 != it1->end(); it2++)
        {
            std::string &s = *it2;
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文