Shashank
Shashank

Reputation: 166

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.

#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. Can someone point me in the right direction ?

Upvotes: 1

Views: 6450

Answers (4)

c z
c z

Reputation: 9037

Wrap it up in an iterator. This can be templated for reuse.

Here is a minimal working example for the std::vector<T> container:

#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;
}

Upvotes: 1

Abhishek
Abhishek

Reputation: 379

*it pointing to vector not int that is why you are getting error

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

Upvotes: 2

Franck
Franck

Reputation: 1635

The following code compiles with the option std=c++11. But x is missing in vector<vector<int>>. If adj had type vector<pair<int, vector<int>>> it would better match.

The following code compiles for vector<vector<int>> but it doesn't use 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.

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;
}

Upvotes: 0

Sam Varshavchik
Sam Varshavchik

Reputation: 118425

 cout << (*it) << endl;

Here, you declared it as a:

 vector<vector<int> >::iterator it;

Therefore, *it is a:

 vector<int>

So you are attempting to use operator<< to send it to 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>. 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>. Perhaps your intent is to iterate over the vector and print each int in the vector, but you're already doing it later.

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 is not clear what your intentions are here. "Graph stored as a vector or vectors" is too vague.

Upvotes: 1

Related Questions