int index = 3;
    map<int, Class> list;
    std::map<int, Class>::iterator test = list.begin();
    std::advance(test, index);
    for(map<int, Class>::iterator it = test; it != list.end();++it)
    {
        cout << it->first << "    " << it->second.GetPrice() << endl;
    }

Hi Everyone,

Please help me on this issue. I know how to start at a certain position of the forloop but i am not able to end the loop at certain position.

Let's say for a normal for loop, [Pls see below].

int start = 2;
int end = 4;
for ( int i = start; i < end; i++)
{

}

Let's say the list have 10 index and i only want to loop the list from index 2 to 4 only. I only want to values from index = 2 till index = 4. How do i do that using iterator? Can someone please guide?

Please let me know if you don't understand.

There are a few options. Because a map iterator is only a bidirectional iterator (as opposed to a random-access iterator), you cannot simply write it + 4 to advance the iterator by four as you can with a random-access iterator.

I guess the first option is to use std::advance to get the iterator range that you want. As so:

std::map<int, Class>::iterator it = list.begin();
std::advance(it, 2);     // move to index 2
std::map<int, Class>::iterator it_end = it;
std::advance(it_end, 2); // move to index 4
for(; it != it_end; ++it)
{
    cout << it->first << "    " << it->second.GetPrice() << endl;
}

One of the problems with the above is that you are kind of doing the traversal (std::advance) before doing the traversal (for-loop), which is inefficient.

Another solution is to simply keep a separate count of the index. As so:

std::size_t index = 2;
std::map<int, Class>::iterator it = list.begin();
std::advance(it, index);     // move to index 2
for(; (it != list.end()) && (index < 4) ; ++it, ++index)  // inc. index, stop at index 4
{
    cout << it->first << "    " << it->second.GetPrice() << endl;
}

I think that this solution is better in general, and doesn't cost much at all.

#include <iostream>
#include <map>
#include <string>

int main()
{
    std::map< int, std::string > map { {  1, "one" }, { 3,"three" }, { 5,"five" },
                                       { 7, "seven" }, { 9, "nine" }, { 11, "eleven" } } ;

    std::map<int,std::string>::key_type two = 2 ;
    std::map<int,std::string>::key_type ten = 10 ;

    // iterate from two to ten inclusive
    // see: http://en.cppreference.com/w/cpp/container/map/upper_bound
    // see: http://en.cppreference.com/w/cpp/container/map/lower_bound
    auto end = map.upper_bound(ten) ;
    for( auto iter = map.lower_bound(two) ; iter != end ; ++iter )
        std::cout << '{' << iter->first << ',' << iter->second << "} " ;
    std::cout << '\n' ;

    // iterate from two up to not including ten
    end = map.lower_bound(ten) ;
    for( auto iter = map.lower_bound(two) ; iter != end ; ++iter )
        std::cout << '{' << iter->first << ',' << iter->second << "} " ;
    std::cout << '\n' ;

    map[2] = "two" ;
    map[10] = "ten" ;

    // iterate from two to ten inclusive
    end = map.upper_bound(ten) ;
    for( auto iter = map.lower_bound(two) ; iter != end ; ++iter )
        std::cout << '{' << iter->first << ',' << iter->second << "} " ;
    std::cout << '\n' ;

    // iterate from two up to not including ten
    end = map.lower_bound(ten) ;
    for( auto iter = map.lower_bound(two) ; iter != end ; ++iter )
        std::cout << '{' << iter->first << ',' << iter->second << "} " ;
    std::cout << '\n' ;
}

http://ideone.com/goG6Nj

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.