用C ++迭代2D向量

时间:2013-04-13 03:01:54

标签: c++ vector

多年来我一直没有使用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> >’

1 个答案:

答案 0 :(得分: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++)