using Boost.foreach foreachAuthor: Jiang
Blog:http://blog.csdn.net/jiangfriend
Time: 2007-7-28 14:56
Friends with other languages must have been greatly facilitated by this command, but C + + did not provide, as 1.34.0 Boost.foreach officially became part of boost, and Boost.foreach quite easy to use. From then on C + + also have foreach.
first, the use of methodsGrammar:
Boost_foreach (VAR, COL)
{
Todo
}
Very simple and clear.
Cases:
#include <string>
#include <iostream>
using namespace Std;
#include <boost/foreach.hpp>
int main (int argc, char* argv[])
{
String Str ("The quick brown fox jumps over the lazy dog");
Boost_foreach (char c,str)
{
cout<<c<<endl;
}
return 0;
}
If #define foreach Boost_foreach, you can almost think of foreach as an internal reserved keyword.
ii. expansion of foreachNot all objects can support foreach.
Such as:
struct MyString
{
String str;
};
Then we need to manually extend the foreach itself.
Accordingly, you need to rewrite the following related functions and structures
range_iterator<link>;
Boost_range_begin (link&);
Boost_range_end (link&);
range_const_iterator<link>; Constant
Boost_range_begin (const link&);
Boost_range_end (const link&);
The Modify program is
#define foreach Boost_foreach
#include <string>
#include <iostream>
using namespace Std;
#include <boost/foreach.hpp>
Inline Range_const_iterator<mystring>::type boost_range_const_begin (const mystring& STR)
{
return Str.begin ();
}
Inline Range_const_iterator<mystring>::type boost_range_const_end (const mystring& STR)
{
return Str.end ();
}
}
int main (int argc, char* argv[])
{
MyString Str ("The quick brown fox jumps over the lazy dog");
foreach (char c,str)
{
cout<<c<<endl;
}
return 0;
}
It's a bit annoying, but it's quite simple.
iii. Matters of noteBoost_foreach (std::p air<int,int>& p,m) is not supported due to language limitations
Because there is a comma between int and int, and Boost_foreach is a macro, Boost_foreach is understood as (Std:pair<int, int>&p,m)
Such as
int main (int argc, char* argv[])
{
Map<int,int> m;
M[1]=1;
foreach (const map<int,int>::value_type& p,m)//Error
{
cout<<p.first<<endl;
}
return 0;
}
Need to be rewritten as
int main (int argc, char* argv[])
{
typedef map<int,int> Map_int;
Map_int m;
M[1]=1;
foreach (const map_int::value_type& p,m)
{
cout<<p.first<<endl;
}
return 0;
}
Four: Post-languageforeach simplifies the programming process is really gratifying, but the boost penetration rate is not high, but also only internal use, more regrettable.
In addition, Boost.foreach for non-copyable and lightweight also have a special treatment, the specific situation can refer to the official documents.
Five: RELATED LINKSforeach Official document Http://www.boost.org/doc/html/foreach.html
Boost Official site http://www.boost.org
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.