Hi, i would like to know if list is able to achieve these :

1) to get the range of elements such as ..
A list of 10 elements beginning from value 1 to 10. I would like to get the range from element 3 to element 5 and add them to another list. Is it possible?

2) Within the list, can i get all the elements and assign to variables respectively?

1) to get the range of elements such as ..
A list of 10 elements beginning from value 1 to 10. I would like to get the range from element 3 to element 5 and add them to another list. Is it possible?

Yes, it is possible. A list is bidirectional. You can go forward and backward, but there is no random access. Getting a range from x to y is fine, but if x is far into the list it might not be as efficient as with a vector.

2) Within the list, can i get all the elements and assign to variables respectively?

Probably, unless you mean something different from what I understand. :)

Possible if you show me some examples.. thanks

This example is intentionally weak, because you might be a student looking for a free ride on homework. But it shows that what you asked for is possible.

#include <algorithm>
#include <iostream>
#include <list>

int main()
{
    typedef std::list<int> list_t;
    typedef list_t::iterator iter_t;
    typedef list_t::const_iterator citer_t;

    list_t list;

    for (int x = 0; x < 10; ++x) list.push_back(x);

    citer_t x = list.begin();
    list_t::size_type k = 0;

    // get a range [2..5)
    while (x != list.end() && k < 2) ++x, ++k;
    while (x != list.end() && k < 5)
    {
        std::cout << *x++ << ' ';
        ++k;
    }

    std::cout.put('\n');

    int a[10];

    // assign to variables?
    for (x = list.begin(), k = 0; x != list.end(); ++x) a[k++] = *x;
    for (k = 0; k < list.size(); ++k) std::cout << a[k] << ' ';

    std::cout.put('\n');
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.