'why distance(iterator,vector.begin()) returns a random number?

I am writing codes as follows. No matter how I call 'next' method, distance between iterator it and vector.begin() returns a random number, never returns the number of next method I call. Could someone explain this to me? And there is runtime error I shown at the bottom.

#include <vector>
#include<iostream>
#include<algorithm>
using namespace std;
class Vector2D {
       ::vector<::vector<int>> vector;
       std::vector<std::vector<int>>::iterator it;
       std::vector<int>::iterator inner;
   
public:
    Vector2D(std::vector<std::vector<int>>& vec):vector(vec),it(begin(vec)),inner(it->begin()){
    }
    int next() {
            if(it != (vector.end()-1)&& inner != it->end()-1)
            {   inner++;
                std::cout<<"it position:"<<distance(it,vector.begin())<<endl;
                return *inner;
            }
            else if (it != (vector.end()-1)&& inner == it->end()-1)
            {   advance(it,1);
                std::cout<< "it position:"<<std::distance(it,vector.begin())<<endl;
                if(it != vector.begin()+1){
                    std::cout<<"weird"<<endl;
                }
                inner=it->begin();
                return *inner;
                    
            }
            else if (it == (vector.end()-1)&& inner != it->end()-1)
            {
                
                inner++;
                return *inner;
            }
            else
            {          
                        return -1;
            }
         
            }
            
};

The error is as follows:

Line 815: Char 16: runtime error: reference binding to misaligned address 0xbebebebebebebebe for type 'int', which requires 4 byte alignment (stl_iterator.h)
0xbebebebebebebebe: note: pointer points here
<memory cannot be printed>
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_iterator.h:820:16


Solution 1:[1]

it(begin(vec))

it points to vec.

it != (vector.end()-1)

You compare it with an iterator to vector. vector is not the same container as vec. Iterators that you compare are not to the same container, and hence the behaviour of the program is undefined.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eerorika