pawel112 pawel112 - 1 month ago 8
C++ Question

C++ iterators in lists

I have a question about iterators on

list
s. In
function
I have to compare the
double
s, but I don't know how to get the elements from the second level, with only iterators to the first level.

void function (std::list<std::list <double>>::iterator *begin, std::list<std::list <double>>::iterator *end)
{
//do something
}

int main()
{
std::list <std::list <double>> a_list;
function (a_list.begin(), a_list.end());
}

Answer
void function (list<list <double> >::iterator begin, list<list <double> >::iterator end)
{
    for(std::list<std::list <double> >::iterator it =begin;it!= end;it++)
    {
        for(std::list <double>:: iterator it_inner = (*it).begin(); it_inner != (*it).end();it_inner++)
        {
            printf("%f ",*it_inner );
        }
        printf("\n");
    }

}



int main()
{
    std::list <std::list <double> > a_list;
    for(int i=0;i<=3;i++)
    {
        std::list <double> inner_list;
        for(double j=0;j<=8;j+=2.2)
        {
            inner_list.push_back(j);
        }
        a_list.push_back(inner_list);
    }    
    function (a_list.begin(), a_list.end());
}