结构向量和迭代C++11
Struct Vector and iteration C++11
我有一个简单的问题。根据其他例子,我的for迭代是正确的,但Eclipse给我带来了错误。。。我的功能
billing::billing(std::istream &is) {
unsigned day;
std::string number;
float time;
struct call call;
while (!is.eof()) {
is >> day >> number >> time;
call.day = day;
call.number = number;
call.time = time;
blng_.push_back(call);
}
for(std::vector<call>::const_iterator it; it = blng_.begin(); it != blng_.end(); ++it)
// THROWS HERE ERRORS!
std::cout << it->day << std::endl;
}
编译后,他扔给我一些类似的东西
expected a type, got 'call' billing.cpp
'it' was not declared in this scope billing.cpp
expected ';' before 'it' billing.cpp
expected ')' before ';' token billing.cpp
invalid type in declaration before 'it' billing.cpp
template argument 2 is invalid billing.cpp
the value of 'call' is not usable in a constant expression billing.cpp
type/value mismatch at argument 1 in template parameter list for
'template<class _Tp, class _Alloc> class std::vector' billing.cpp
根据这个,我如何在常数向量上迭代?主题它应该工作,但它不是,我不知道为什么。当我改变整个std::vectro。。。自动工作!
在C++11
中,您可以重写:
for(std::vector<call>::const_iterator it = blng_.begin(); it != blng_.end(); ++it)
std::cout<<it->day<<std::endl;;
作为
for(const auto& c: blng_)
std::cout << c.day << std::endl;
附加说明:
永远不要使用eof()
循环:为什么循环条件中的iostream::eof被认为是错误的?
你真的应该这样做:
while(is >> day >> number >> time) {
call.day = day;
call.number = number;
call.time = time;
blng_.push_back(call);
}
相关文章:
- C++11 迭代向量的新方法?
- 从迭代器转换为变量的类型.(C++11 模板)
- 在 c++11 中迭代函子元组
- C++11 std::列出拼接后的迭代器失效
- 是否有一个很好的方法可以在C 11中打印出像JSON一样的Trie结构(仅迭代解决方案)的扁平命名空间
- C++11 迭代器和返回的 std::unique_ptr 的范围
- 矢量迭代器在尝试对图形进行深度优先搜索时导致分割错误 11
- 在 C++11 中迭代多个 seq. 容器
- 使用 C++11 迭代语法时从 STL 列表中删除
- C++11 迭代器接口
- 访问迭代器值会导致segfault 11
- C 11 forward_list迭代器仍指向删除值
- 如何将基于迭代器的 for 循环重写为基于范围的循环 (C++11)
- 结构向量和迭代C++11
- VC++11映射和多映射迭代器(重载)C2535
- 如何最好地在C++11中为unique_ptr中的普通数组制作迭代器
- DEV-C++5.11 STL迭代器的调试问题
- 在C++11中,从向量中删除元素,同时对其进行迭代
- C++11 中的过去终结迭代器失效
- c++ 11迭代锯齿数组