boost-foreach

Iterating through an rvalue container

我的梦境 提交于 2019-12-22 11:06:13
问题 Is the following code causing undefined behavior? std::map<int, vector<int>> 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 loop instead of BOOST_FOREACH? 回答1: This is, unfortunately, most probably undefined behavior. The problem is that you have two levels here: std::map<...> is an r-value, its lifetime will be expanded until the end of the full-expression std::vector<int

Need help with BOOST_FOREACH/compiler bug

二次信任 提交于 2019-12-21 07:12:04
问题 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<unsigned, unsigned>::VertexIterator Iter; Graph<unsigned, unsigned> g; g.createVertex(0x66); // works fine Iter it = g.getVertices().first, end = g.getVertices().second; for(; it != end; ++it) ; // fine std::pair<Iter, Iter> p = g.getVertices(); BOOST_FOREACH(unsigned

Iterate over all files in a directory using BOOST_FOREACH

杀马特。学长 韩版系。学妹 提交于 2019-12-20 18:24:06
问题 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 compiles, runs, but does not produce the desired result. 回答1: You can iterate over files in a directory using BOOST_FOREACH like this: #include <boost/filesystem.hpp> #include <boost/foreach.hpp> namespace fs = boost::filesystem; fs::path targetDir("/tmp"); fs:

How can I iterate over two vectors simultaneously using BOOST_FOREACH?

放肆的年华 提交于 2019-12-17 10:52:34
问题 I'd like to replicate the following with BOOST FOREACH std::vector<int>::const_iterator i1; std::vector<int>::const_iterator i2; for( i1 = v1.begin(), i2 = v2.begin(); i1 < v1.end() && i2 < v2.end(); ++i1, ++i2 ) { doSomething( *i1, *i2 ); } 回答1: Iterating over two things simultaneously is called a "zip" (from functional programming), and Boost has a zip iterator: The zip iterator provides the ability to parallel-iterate over several controlled sequences simultaneously. A zip iterator is

Why is BOOST_FOREACH not exactly equivalent to handcoded one?

天大地大妈咪最大 提交于 2019-12-14 03:54:59
问题 From boost doc, This results in near-optimal code generation; the performance of BOOST_FOREACH is usually within a few percent of the equivalent hand-coded loop. I guess using macros and non standard typeof operator, we can generate exactly equivalent one. What feature of BOOST_FOREACH makes it not exact? Edit: My version: #define EACH(it,v) \ for(typeof(v.begin()) it = v.begin();it != v.end(); ++it) //use this if you want a const_iterator from a non-const container #define CONST_EACH(it,v) \

Accessing values using a boost::property_tree::string_path

谁说胖子不能爱 提交于 2019-12-12 21:12:24
问题 I am playing with boost::property_tree::ptree, using namely the following json file: { "menu": { "foo": "true", "bar": "true", "value": "102.3E+06", "popup": [ { "value": "New", "onclick": "CreateNewDoc()" }, { "value": "Open", "onclick": "OpenDoc()" } ] } } I have been trying to access nested "value" with no luck so far, here is what I did: #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <boost/foreach.hpp> int main(int argc, char *argv[]) {

Using BOOST_FOREACH with a constant intrusive list

我只是一个虾纸丫 提交于 2019-12-11 03:12:50
问题 Consider the following code to iterate over an intrusive list using the BOOST_FOREACH macro: #include <boost/foreach.hpp> #include <boost/intrusive/list.hpp> typedef boost::intrusive::list< boost::intrusive::list_base_hook<> > MyList; void iterate (const MyList& xs) { BOOST_FOREACH (MyList::const_reference node, xs); } int main () { MyList xs; iterate (xs); return 0; } Given boost version 1.48 the code fails with clang 3.2 (SVN) and gcc 4.6.3, but works with gcc 4.5.3. With non-const

Parsing JSON with boost property tree

余生长醉 提交于 2019-12-10 02:21:15
问题 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 following code: #include <vector> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <boost/foreach.hpp> using namespace std; using boost::property_tree::ptree; class single_t{ int sID; string sName;

Iterating through an rvalue container

谁说胖子不能爱 提交于 2019-12-06 03:26:35
Is the following code causing undefined behavior? std::map<int, vector<int>> 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 loop instead of BOOST_FOREACH? This is, unfortunately, most probably undefined behavior. The problem is that you have two levels here: std::map<...> is an r-value, its lifetime will be expanded until the end of the full-expression std::vector<int>& is an l-value reference (into an object), its lifetime is that of the object. The problem arises because

Replace BOOST_FOREACH with “pure” C++11 alternative?

吃可爱长大的小学妹 提交于 2019-12-05 12:51:02
问题 Is it possible to replace the BOOST_FOREACH in this example with a "pure" C++11 equivalent? #include <map> #include <functional> #include <boost/foreach.hpp> #include <iostream> int main() { std::map<int, std::string> map = {std::make_pair(1,"one"), std::make_pair(2,"two")}; int k; std::string v; BOOST_FOREACH(std::tie(k, v), map) { std::cout << "k=" << k << " - " << v << std::endl; } } The key feature being keeping the key/value pair in the references to k and v . I tried: for(std::tie(k,v)