rub*_*nvb 3 c++ iterator initialization decltype
我正在尝试这样做:
typedef std::map<std::string, time_t> my_map;
const mymap& some_function();
class bla
{
private:
my_map::iterator current
bla(const mymap& m) : current(m.begin())
{ }
}
Run Code Online (Sandbox Code Playgroud)
它不起作用.更复杂的真实错误信息是:
错误:没有匹配函数来调用'std :: _ Rb_tree_iterator,long int >>> :: _ Rb_tree_iterator(std :: map,long int> :: const_iterator)'
我尝试将初始化/赋值移动到构造函数体:
{ current = m.begin(); }
Run Code Online (Sandbox Code Playgroud)
哪个也没用.所以我想,这种类型current是错误的,所以我只是让编译器推断出:
...
decltype(my_map::begin()) current;
...
Run Code Online (Sandbox Code Playgroud)
哪个也行不通.
我只需要一个在bla构造时设置的迭代器成员,所以我不必在一些愚蠢的额外函数中显式设置它,并且可以通过外部遍历地图:
bool bla::do_stuff(...output...)
{
if(current = m.end())
return false;
balblabla(current);
++current;
return true;
}
Run Code Online (Sandbox Code Playgroud)
你正试图分配const_iterator给iterator.有两种解决方案:
bla(my_map& m) : current(m.begin()) {})获取地图.const_iterator.