[英]iterating over vector of vectors in c++
I've just started to code in C++, so i'm new to STL. Here i'm trying to iterate over a graph stored as vector of vectors.我刚开始在 C++ 中编码,所以我是 STL 的新手。在这里,我试图迭代存储为向量向量的图形。
#include <iostream>
#include <vector>
#include <iostream>
using namespace std;
int reach(vector<vector<int> > &adj, int x, int y) {
vector<vector<int> >::iterator it;
vector<int>::iterator i;
for (it = adj.begin(); it != adj.end(); it++)
{
cout << (*it) << endl;
if ((*it) == x)
for (i = (*it).begin(); i != (*it).end(); i++)
{
cout << (*i) << endl;
if ((*i) == y)
return 1;
}
}
return 0;
}
int main()
{
}
I'm getting an error std::vector<int>
is not derived from const gnu cxx.我收到错误std::vector<int>
is not derived from const gnu cxx。 Can someone point me in the right direction?有人能指出我正确的方向吗?
*it pointing to vector not int that is why you are getting error *它指向向量not int,这就是为什么您会出错
following code may work for you 以下代码可能适合您
#include <vector>
#include <iostream>
using namespace std;
int reach(vector<vector<int> > &adj, int x, int y) {
vector<vector<int> >::iterator it;
vector<int>::iterator i;
for (it = adj.begin(); it != adj.end(); it++)
{
cout << (*(*it).begin()) << endl;
if (( (*(*it).begin())) == x)
for (i = (*it).begin(); i != (*it).end(); i++)
{
cout << (*i) << endl;
if ((*i) == y)
return 1;
}
}
return 0;
}
int main()
{
}
for accessing first element of the vector of the use 用于访问使用向量的第一个元素
(*(*it).begin()) in place of (*it)
if you are studying graph then use array of vector. 如果您正在研究图形,则使用向量数组。 for more details please go through following url C++ Depth First Search (DFS) Implementation 有关更多详细信息,请通过以下URL C ++深度优先搜索(DFS)实现
cout << (*it) << endl;
Here, you declared it
as a: 在这里,您将it
声明为:
vector<vector<int> >::iterator it;
Therefore, *it
is a: 因此, *it
是:
vector<int>
So you are attempting to use operator<<
to send it to std::cout
. 因此,您尝试使用operator<<
将其发送到std::cout
。 This, obviously, will not work. 显然,这将行不通。 This is equivalent to: 这等效于:
vector<int> v;
cout << v;
There is no operator<<
overload that's defined for what cout
is, and a vector<int>
. 没有为cout
定义的operator<<
重载,也没有vector<int>
。 As you know, in order to print the contents of a vector, you have to iterate over its individual values, and print its individual values. 如您所知,为了打印向量的内容,您必须迭代其单个值,并打印其单个值。
So, whatever your intentions were, when you wrote: 因此,无论您的意图是什么,当您写时:
cout << (*it) << endl;
you will need to do something else, keeping in mind that *it
here is an entire vector<int>
. 您需要做其他事情,请记住*it
这里是整个vector<int>
。 Perhaps your intent is to iterate over the vector and print each int
in the vector, but you're already doing it later. 也许您的意图是遍历vector并打印vector中的每个int
,但是您稍后已经在做。
Similarly: 类似地:
if ((*it) == x)
This won't work either. 这也不行。 As explained, *it
is a vector<int>
, which cannot be compared to a plain int
. 如前所述, *it
是vector<int>
,不能与普通int
进行比较。
It is not clear what your intentions are here. 目前尚不清楚您的意图是什么。 "Graph stored as a vector or vectors" is too vague. “将图形存储为一个或多个矢量”太含糊。
The following code compiles with the option std=c++11. 以下代码使用选项std = c ++ 11进行编译。 But x
is missing in vector<vector<int>>
. 但是x
在vector<vector<int>>
丢失。 If adj
had type vector<pair<int, vector<int>>>
it would better match. 如果adj
类型为vector<pair<int, vector<int>>>
则最好匹配。
The following code compiles for vector<vector<int>>
but it doesn't use x
. 以下代码针对vector<vector<int>>
编译,但不使用x
。
using std::vector;
using std::pair;
using std::cout;
using std::endl;
int reach(vector<vector<int> > &adj, int x, int y) {
vector<vector<int> >::iterator it;
vector<int>::iterator i;
for(it=adj.begin();it!=adj.end();it++)
{
// cout << (*it) << endl;
for (const auto& nexts: *it)
cout << nexts << ' ';
cout << endl;
for(i=(*it).begin();i!=(*it).end();i++)
{
cout << (*i) << endl;
if((*i)==y)
return 1;
}
}
return 0;
}
This code compiles with <vector<pair<int, vector<int>>>
and uses x
. 该代码使用<vector<pair<int, vector<int>>>
编译,并使用x
。
using std::vector;
using std::pair;
using std::cout;
using std::endl;
int reach(vector<pair<int, vector<int> > > &adj, int x, int y) {
vector<pair<int, vector<int> > >::iterator it;
vector<int>::iterator i;
for(it=adj.begin();it!=adj.end();it++)
{
cout << it->first << endl;
if (it->first == x)
for(i=it->second.begin();i!=it->second.end();i++)
{
cout << (*i) << endl;
if((*i)==y)
return 1;
}
}
return 0;
}
Wrap it up in an iterator.将其包装在迭代器中。 This can be templated for reuse.这可以被模板化以供重用。
Here is a minimal working example for the std::vector<T>
container:这是std::vector<T>
容器的最小工作示例:
#include <iostream>
#include <utility>
#include <vector>
/// Iterable vector of vectors
/// (This just provides `begin` and `end for `Vector2Iterable<T>::Iterator`).
template<typename T>
class VovIterable
{
public:
static const std::vector<T> EMPTY_VECTOR;
/// Actual iterator
class Iterator
{
typename std::vector<std::vector<T>>::const_iterator _a1;
typename std::vector<T>::const_iterator _a2;
typename std::vector<std::vector<T>>::const_iterator _end;
public:
/// \param a1 Outer iterator
/// \param a2 Inner iterator
/// \param end End of outer iterator
explicit Iterator(typename std::vector<std::vector<T>>::const_iterator a1, typename std::vector<T>::const_iterator a2, typename std::vector<std::vector<T>>::const_iterator end)
: _a1(a1)
, _a2(a2)
, _end(end)
{
Check();
}
bool operator!=(const Iterator &b) const
{
return _a1 != b._a1 || _a2 != b._a2;
}
Iterator &operator++()
{
++_a2; // Increment secondary
Check();
return *this;
}
const T &operator*() const
{
return *_a2;
}
private:
void Check()
{
while (true)
{
if (_a2 != _a1->end()) // Is secondary live?
{
break;
}
// Increment primary
_a1++;
if (_a1 == _end) // Is primary dead?
{
_a2 = EMPTY_VECTOR.end();
break;
}
_a2 = _a1->begin(); // Reset secondary
}
}
};
private:
std::vector<std::vector<T>> _source;
public:
explicit VovIterable(std::vector<std::vector<T>> source)
: _source(std::move(source))
{
}
/// Start of vector of vectors
[[nodiscard]] Iterator begin() const
{
if (this->_source.empty())
{
return end();
}
return Iterator(this->_source.cbegin(), this->_source.cbegin()->cbegin(), this->_source.cend());
}
/// End of vector of vectors
[[nodiscard]] Iterator end() const
{
return Iterator(this->_source.cend(), EMPTY_VECTOR.end(), this->_source.cend());
}
};
template<typename T>
const std::vector<T> VovIterable<T>::EMPTY_VECTOR = {0};
/// Sample usage
int main()
{
std::vector<std::vector<int>> myVov{{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
for (int i: VovIterable(myVov))
{
std::cout << i << std::endl;
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.