Questions tagged [boost-foreach]

Boost.Foreach is a C++ library containing a macro for easily iterating over the elements of a sequence.

Boost.Foreach is a C++ library containing a macro for easily iterating over the elements of a sequence.

The BOOST_FOREACH macro is designed for ease-of-use and efficiency. It does no dynamic allocations, makes no virtual function calls or calls through function pointers, and makes no calls that are not transparent to the compiler's optimizer. This results in near-optimal code generation; the performance of BOOST_FOREACH is usually within a few percent of the equivalent hand-coded loop. And although BOOST_FOREACH is a macro, it is a remarkably well-behaved one. It evaluates its arguments exactly once, leading to no nasty surprises.

45 questions
34
votes
4 answers

How can I iterate over two vectors simultaneously using BOOST_FOREACH?

I'd like to replicate the following with BOOST FOREACH std::vector::const_iterator i1; std::vector::const_iterator i2; for( i1 = v1.begin(), i2 = v2.begin(); i1 < v1.end() && i2 < v2.end(); ++i1, ++i2 ) { doSomething( *i1,…
Candy Chiu
  • 6,307
  • 8
  • 43
  • 66
30
votes
3 answers

Boost 1.46.1, Property Tree: How to iterate through ptree receiving sub ptrees?

First of all I shall say that I think I got how it should be done but my code will not compile any way I try. I based my assumption on this official example of empty ptree trick. There you can find next line: const ptree &settings =…
Rella
  • 59,216
  • 102
  • 341
  • 614
19
votes
3 answers

Replace BOOST_FOREACH with "pure" C++11 alternative?

Is it possible to replace the BOOST_FOREACH in this example with a "pure" C++11 equivalent? #include #include #include #include int main() { std::map map =…
Flexo
  • 82,006
  • 22
  • 174
  • 256
18
votes
3 answers

Iterate over all files in a directory using BOOST_FOREACH

Can you iterate over all files in a directory using boost::filesystem and BOOST_FOREACH? I tried path dirPath = ... int fileCount = 0; BOOST_FOREACH(const path& filePath, dirPath) if(is_regular_file(filePath)) ++fileCount; This code…
Johan Råde
  • 18,399
  • 19
  • 62
  • 103
15
votes
3 answers

BOOST_FOREACH versus for loop

I would like to have your advice regarding the usage of BOOST_FOREACH. I have read around it is not really recommended in terms of performance being a very heavy header. Moreover, it forces the use of "break" and "continue" statements since you…
codeJack
  • 2,053
  • 3
  • 19
  • 27
14
votes
2 answers

Need help with BOOST_FOREACH/compiler bug

I know that boost or compiler should be last to blame, but I can't see another explanation here. I'm using msvc 2008 SP1 and boost 1.43. In the following code snippet execution never leaves third BOOST_FOREACH loop typedef Graph
Jacek Ławrynowicz
  • 2,600
  • 2
  • 20
  • 23
9
votes
1 answer

Parsing JSON with boost property tree

I'm building an application that gets movie information from themoviedb.com. The information is provided in a JSON file. I'm trying to store the information using boost property tree. But There is a little problem. I illustrate the problem by the…
Glenn Vonk
  • 93
  • 1
  • 1
  • 4
8
votes
4 answers

How can I use BOOST_FOREACH with a container supporting only const_iterator?

I have this container: class /*final*/ Row { public: typedef FieldIterator const_iterator; typedef FieldIterator iterator; FieldIterator begin() const; FieldIterator end() const; FieldIterator begin(); FieldIterator end(); …
mark
  • 49,076
  • 65
  • 227
  • 485
7
votes
2 answers

BOOST_FOREACH Iteration over boost::shared_ptr

I'm doing something similar to this item Correct BOOST_FOREACH usage? However, my returned list is wrapped in a boost::shared_ptr. If I do not assign the list to a variable before the BOOST_FOREACH loop, I get a crash at runtime as the list is…
PeskyGnat
  • 2,314
  • 17
  • 21
6
votes
2 answers

Changes to a container while BOOST_FOREACH is iterating through it?

What would happen if a container that BOOST_FOREACH is iterating through is changed inside the BOOST_FOREACH scope? Does BOOST_FOREACH "freeze" the initial state?
Jonathan
  • 84,911
  • 94
  • 244
  • 345
5
votes
2 answers

How can I test for the last element in a vector when using BOOST_FOREACH?

I have a vector which I iterate over. The final element of the vector is special case, and I'd like to test for it separately. For example, I may do something as such: for (iterator = vector.begin(); iterator != vector.end(); ++iterator) { if…
v8891
  • 133
  • 1
  • 5
5
votes
1 answer

Why BOOST_FOREACH on a map only work with a typedef

I tried to write a simple loop through mapelement and I'm wondering why the 1st syntax I used doesn't work/compile ? The 1st version I wrote was the following and it doesn't compile with VS'2008 / boost version 1.44: std::map
alexbuisson
  • 6,404
  • 2
  • 25
  • 40
3
votes
2 answers

Boost C++ macro argument count error

In the following piece of code: BOOST_FOREACH(std::pair &itval, completedEs_) { allCompleted &= it->second; } I'm getting this error: error: macro "BOOST_FOREACH" passed 3 arguments, but takes just 2 I'm only passing 2 arguments,…
Matt Joiner
  • 100,604
  • 94
  • 332
  • 495
3
votes
3 answers

Iterating through an rvalue container

Is the following code causing undefined behavior? std::map> foo() { return ... } BOOST_FOREACH(const int& i, foo()[42]) { std::cout << i << std::endl; } If undefined, What is the good way to fix it? What if I use c++11 range-for…
balki
  • 22,482
  • 26
  • 85
  • 135
3
votes
3 answers

using boost foreach with items that are themselves templates

I have a std::deque< std::pair > that I would like to iterate over using BOOST_FOREACH. I tried the following: #define foreach_ BOOST_FOREACH // declaration of the std::deque std::deque< std::pair > chosen; foreach_(…
BeeBand
  • 9,539
  • 19
  • 56
  • 83
1
2 3