1

多年来我没有使用 C/C++,我正在尝试复习基础知识,但我遇到了迭代 3D 向量的问题:

#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> >’
4

1 回答 1

0

我同意 chris,我在这里只看到 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++)
于 2013-04-13T03:07:08.307 回答