map<string>, vector<string>> myMap

Pages: 12
Thank you for the numerous examples.

About the
 
for( const auto& str : query( mmap, key, attribute ) ) std::cout << std::quoted(str) << ' ' ;


how the iteration knows that the cycle is end?




A range-based for loop iterates over a 'range' -
from the begin() of the range up to, not including, the end() of the range.

http://www.stroustrup.com/C++11FAQ.html#for
http://en.cppreference.com/w/cpp/language/range-for
Thank you.


In this loop i'm trying to do some conditional check:

for( const auto& str : query( mmap, key, attribute ) ) std::cout << std::quoted(str) << ' ' ;

example
1
2
3
4
5
6
7
for( const auto& str : query( mmap, key, attribute ) ) {

if ((str)=="solar system" {

     std::cout << "Pluto is part of our solar system" << endl;
}
}


but sometimes i get an error. The program crash and i get Process returned 255 (0xFF).

I think it's related to something out of bound. Why?
The lifetime of a temporary used as the range-expression is extended till the end of the loop.

Nevertheless, try these:

Do you still get the same error if you write:
1
2
3
4
5
6
7
for( /*const auto&*/ auto str : query( mmap, key, attribute ) ) {

     if ((str)=="solar system" {

         std::cout << "Pluto is part of our solar system" << endl;
     }
}


Do you get the same error even if you write:
1
2
3
4
5
6
7
8
const auto /*&*/ range = query( mmap, key, attribute ) ; // *** EDIT 
for( /*const auto&*/ auto str : range ) {

     if ((str)=="solar system" {

         std::cout << "Pluto is part of our solar system" << endl;
     }
}
Last edited on
Sorry for the late replay.

Seems that the mentioned routine was not involved.
I have to investigate in other direction.

Thank you, however.

Topic archived. No new replies allowed.
Pages: 12