[英]Vector iterator type unknown
I'm learning C++ and this is the first time I work with iterators我正在学习 C++,这是我第一次使用迭代器
I have written down this code:我已经写下了这段代码:
std::pair<max, max> ConvertToAStarMap::GetLimits(std::string map)
{
// Init max and min variables.
int max_x = std::numeric_limits<int>::min();
int min_x = std::numeric_limits<int>::max();
int max_y = max_x;
int min_y = min_x;
std::vector<std::string> map_cells = ConvertToAStarMap::split(map, ';');
for (std::string::iterator it = map_cells.begin(); it != map_cells.end(); ++it)
}
And I get this error:我得到这个错误:
ConvertToAStarMap.cpp: In member function ‘std::pair<max, max> ConvertToAStarMap::GetLimits(std::__cxx11::string)’:
ConvertToAStarMap.cpp:31:52: error: conversion from ‘std::vector<std::__cxx11::basic_string<char> >::iterator {aka __gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >}’ to non-scalar type ‘std::__cxx11::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >}’ requested
for (std::string::iterator it = map_cells.begin(); it != map_cells.end(); ++it)
~~~~~~~~~~~~~~~^~
ConvertToAStarMap.cpp:31:59: error: no match for ‘operator!=’ (operand types are ‘std::__cxx11::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >}’ and ‘std::vector<std::__cxx11::basic_string<char> >::iterator {aka __gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >}’)
for (std::string::iterator it = map_cells.begin(); it != map_cells.end(); ++it)
~~~^~~~~~~~~~~~~~~~~~
What is wrong?怎么了? I don't understand it.
我不明白。
Easiest to do is:最简单的做法是:
for (const auto& cell : map_cells)
If you don't want to use the range based for loop :如果您不想使用基于范围的 for 循环:
for (auto it = map_cells.begin();......
The problem is that You want to iterate over the vector, not over the string, so it should be:问题是你想迭代向量,而不是字符串,所以它应该是:
for (std::vector<std::string>::iterator it = map_cells.begin();......
This information is available in the error in point 2. :此信息在第 2 点中的错误中可用。:
'std::vector<std::__cxx11::basic_string<char> >::iterator
__gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >
}' __gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >
}'std::__cxx11::basic_string<char>::iterator
std::__cxx11::basic_string<char>::iterator
__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >
}' __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >
}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.