gpt4 book ai didi

c++ - 用 C++ 迭代二维 vector

转载 作者:太空宇宙 更新时间:2023-11-04 14:08:19 25 4
gpt4 key购买 nike

我已经很多年没有使用 C/C++ 了,我正在尝试复习基础知识,我有一个问题要迭代 3D vector :

#include        <string>
#include <vector>
#include <iterator>

int main() {
std::vector< std::vector<std::string> > container;
std::vector<std::string> table;
std::string line;

for (int container_it = 0; container_it < 2; container_it++) {
for (int table_it = 0; table_it < 5 ; table_it++) {
line = "TEST";
table.push_back(line);
}
container.push_back(table);
}


for (std::vector< std::vector<std::string> >::iterator container_it = container.begin(); container_it != container.end(); container_it++)
for (std::vector<std::string>::iterator it = (*container_it)->begin(); it != (*container_it)->end(); it++)
std::cout << *it << std::endl;
}

我得到错误:

base operand of ‘->’ has non-pointer type ‘std::vector<std::basic_string<char> >’
base operand of ‘->’ has non-pointer type ‘std::vector<std::basic_string<char> >’

最佳答案

我同意克里斯的观点,我在这里只看到 2 个维度,代码有一些语法错误,但它看起来像这一行:

for (std::vector<std::string>::iterator it = (*container_it)->begin(); it != (*container_it)->end(); it++)

应该是:

for (std::vector<std::string>::iterator it = (*container_it).begin(); it != (*container_it).end(); it++)

或者更简单地说:

for (std::vector<std::string>::iterator it = container_it->begin(); it != container_it->end(); it++)

关于c++ - 用 C++ 迭代二维 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15983745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com