0

I have a question about iterators on lists. In function I have to compare the doubles, 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());
}
4
  • Can you give more detail of your second level requirement? Commented Nov 7, 2016 at 13:38
  • First of all, function should take the iterators by value, not pointers to iterators. Then, it depends on how exactly you want to "compare the doubles". You should expand on what you mean by that. Commented Nov 7, 2016 at 13:41
  • Hope my solution provides the solution to your doubt Commented Nov 7, 2016 at 14:02
  • Thanks. I mean about the second level - that is, the ability data from the list inside another list. Commented Nov 7, 2016 at 15:20

2 Answers 2

1
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());
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can pass the iterators as parameters or just the entire list, like this:

#include <iostream>
#include <list>

using namespace std;

void funcA (list<list<double>>::iterator begin, list<list<double>>::iterator end) {
    //do something

    list<double>::iterator sublist1 = begin->begin();
    cout << *sublist1 + 1 << endl;
}

void funcB (list<list<double>> list) {

    for (auto&& sublist: list) {
        for (auto&& value: sublist) {
            cout << value << ' ';
        }
    }
    cout << endl;
}


int main() {
    list <list<double>> a_list = {{1,2,3},{4,5,6}};

//  list<list<double>>::iterator a = a_list.begin();
//  list<double>::iterator b = a->begin();
//  
//  cout << *b + 2 << endl;

    funcA(a_list.begin() , a_list.end());
    funcB(a_list);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.