I have a vector like this:
std::vector<std::vector<char> > _lines;
I would like to iterate these vector and the vector contained within, however I'm not sure how I'd access the inside vector using the iterator of the first one?
I have the following already:
std::vector<std::vector<char> >::iterator first_iter = _lines.begin();
for (; first_iter != _lines.end(); first_iter++)
{
(*first_iter)::iterator second_iter = (*first_iter)->begin(); // something is wrong with this? How do I get the second vector to iterate?
}
for (std::vector<std::vector<char> >::iterator i = _lines.begin();
i != _lines.end(); ++i)
{
for (std::vector<char>::iterator j = i->begin(); j != i->end(); ++j)
{
// ... use *j
}
}
If you use a modern compiler, you can use the C++0x feature auto
to deduce the type automatically:
for (auto i = _lines.begin(); i != _lines.end(); ++i)
{
for (auto j = i->begin(); j != i->end(); ++j)
{
// ... use *j
}
}
You can't specify a type like this (*first_iter)::iterator
. Just use std::vector<char>::iterator
.
(*first_iter)->begin()
should be (*first_iter).begin()
(or first_iter->begin()
). *first_iter
is a std::vector<char>
, not a pointer.
您需要自己拼写内部迭代器的类型,而不能通过实例上的作用域运算符::访问类的类型。
std::vector<char>::iterator second_iter = ...
std::vector<std::vector<char> >::iterator first_iter =_lines.begin();
for (; first_iter != _lines.end(); first_iter++) {
std::vector<char>::iterator second_iter = (*first_iter).begin(); // or first_iter->begin()
...
}
This would be the way to do it:
std::vector<std::vector<char> >::iterator first_iter = _lines.begin();
for (; first_iter != _lines.end(); first_iter++)
{
std::vector<char>::iterator second_iter = first_iter->begin();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.