Có 80+ tài liệu thuộc chủ đề "programming guides"
tailieu.vn Xem trực tuyến Tải xuống
A Look at regex_iterator. We have seen how to use several calls to regex_search in order to process all of an input sequence, but there's another, more elegant way of doing that, using a regex_iterator . This iterator type enumerates all of the regular expression matches in a sequence. Dereferencing a regex_iterator yields a reference to an instance of match_results...
tailieu.vn Xem trực tuyến Tải xuống
programmers, but it is certainly one of the best. It is like a variant type on steroids: It will hold any type, but you have to know the type to retrieve the value. Unfortunately, all of these suffer from a lack of type safety, and only in the most controlled situations should we ever purposely defeat the type system. The...
tailieu.vn Xem trực tuyến Tải xuống
You use the class any to store values, and the template function any_cast to subsequently retrieve the stored values. To use any, include the header "boost/any.hpp". boost::any a;. a=std::string("A string");. Almost anything is acceptable to any! However, to actually do anything with the value contained in an any, we need to retrieve it, right? For that, we need to know...
tailieu.vn Xem trực tuyến Tải xuống
boost::any_cast<std::string>(properties[0].value. boost::any_cast<int>(properties[1].value. boost::any_cast<double>(properties[2].value. Notice that we didn't have to explicitly create the any s needed for property 's constructor. When retrieving the value of an instance of any , pass the any by const reference to any_cast if a failure indicates a real error.. std::string s=boost::any_cast<std::string>(a);. When a failure is not necessarily an error, pass the any by pointer.. std::string*...
tailieu.vn Xem trực tuyến Tải xuống
std::cout <<. "This any contained a boost::shared_ptr<A>\n";. Try boost::shared_ptr<B>. boost::shared_ptr<B>. boost::any_cast<boost::shared_ptr<B>. "This any contained a boost::shared_ptr<B>\n";. If anything else (like just a string), ignore it std::cout <<. "The any didn't contain anything that \ concerns this function!\n";. Next, we store all of the any s in the vector and send every element in it to the function foo, which examines...
tailieu.vn Xem trực tuyến Tải xuống
any_out(const T&. all we need is to make sure that the streamer in the source any_out a is not zero.. any_out(const any_out&. streamer_(a.streamer_?a.streamer_->clone():0),o_(a.o. any_out&. any_out(r).swap(*this);. operator=(const any_out&. ~any_out. swap(any_out&. std::swap(streamer_, r.streamer_);. It should accept a reference to an ostream and an any_out . The any stored in the any_out should be passed on to the virtual function print of the...
tailieu.vn Xem trực tuyến Tải xuống
std::for_each(vec.begin(),vec.end(),. std::cout <<. <<. Let's clear the contents of the container vec and add some new values.. vec.clear();. vec.push_back(std::string("This is a string"));. vec.push_back(42);. vec.push_back(3.14);. std::cout <<. it's a property of the design of the Standard Library. The type of the argument and the result type must be exposed through provided typedef s, and that means that we need a function...
tailieu.vn Xem trực tuyến Tải xuống
How Does the Variant Library Improve Your Programs?. Typesafe storage and retrieval of a user-specified set of types. The Variant library focuses on typesafe storage and retrieval of a bounded set of typesthat is, on discriminated unions. The Boost.Variant library has many features in common with Boost.Any, but there are different tradeoffs as well as differences in functionality. hereBoost.Variantsupports bounded...
tailieu.vn Xem trực tuyến Tải xuống
To start using variants in your programs, include the header "boost/variant.hpp".. my_first_variant;. When the variable my_first_variant is created, it ends up containing a default- constructed int, because int is first among the types that the variant can contain.. We can also pass a value that is convertible to one of those types to initialize the variant.. my_first_variant("Hello world");. At any...
tailieu.vn Xem trực tuyến Tải xuống
std::cout <<. <<. i <<. void operator()(std::string&. "It's a std::string: ". s <<. d <<. c:/boost_cvs/boost/boost/variant/variant.hpp:. c:/boost_cvs/boost/boost/variant/variant.hpp:807:. operator<<. #include "boost/variant.hpp". t) const { os_ <<. t <<. boost::variant<int,std::string>. The idea is that the member function template for the function call operator in stream_output_visitor will be instantiated once for each type visited ( int and std::string , in this case). Because...
tailieu.vn Xem trực tuyến Tải xuống
The fact that discriminated unions are useful in everyday programming should come as no surprise, and the Boost.Variant library does an excellent job of. Boost.Variant avoids this limitation through templates, which theoretically allows creating any variant type. it was necessary to test for the type of the current value before acting, creating maintenance headaches. Boost.Variant offers straightforward value extraction and...
tailieu.vn Xem trực tuyến Tải xuống
"boost/tuple/tuple.hpp". The relational operators are defined in the header "boost/tuple/tuple_comparison.hpp". Input and output of tuples are defined in "boost/tuple/tuple_io.hpp". A few of the key tuple components (tie and make_tuple) are also available directly in namespace boost. In this section, we'll cover how tuples are used in some typical scenarios, and how it is possible to extend the functionality of the...
tailieu.vn Xem trực tuyến Tải xuống
boost::tuple<unsigned int,std::string,base*>. tup5.get<2>()->test();. boost::tuple<int,std::string,derived&>. boost::tuple<unsigned int,std::string,base&>. tup7.get<2>()->test();. Note that typically smart pointers are best in tuples (as opposed to raw pointers), because they alleviate the need to manage the lifetime of the resources to which the pointers refer. "boost/tuple/tuple_comparison.hpp". The test for equality returns TRue if all of the. element pairs of the two tuple s are also equal. The...
tailieu.vn Xem trực tuyến Tải xuống
std::cout <<. boost::tuples::set_open. boost::tuples::set_close. boost::tuples::set_delimiter. <<. tup1 <<. boost::tuples::set_open('\". <<. boost::tuples::set_close('\". boost::tuples::set_delimiter('-');. tup2 <<. The support for streaming is convenient and, with the support of the delimiter manipulators, it's easy to make streaming compatible even with legacy code that has been updated to use tuple s.. metaprogram, we can print all the elements of a tuple.. #include "boost/tuple/tuple.hpp". struct print_helper...
tailieu.vn Xem trực tuyến Tải xuống
template called for_each_element to do just that. for_each_element. void for_each_element(. void for_each_element(Tuple&. for_each_element(t.get_tail(),func);. for_each_element(nums, print());. for_each_element(nums, print_type<double>());. The function for_each_element reuses the strategy from earlier examples, by overloading the function with a version that accepts an argument of type. null_type that signals the end of the tuple 's elements, to do nothing. Let's look at the function where the work...
tailieu.vn Xem trực tuyến Tải xuống
Header: "boost/bind.hpp". Rather than supplying all of the arguments to the function directly, arguments can be delayed, meaning that a binder can be used to create a function object with changed arity (number of arguments) for the function it binds to, or to reorder the arguments any way you like.. The return types of the overloaded versions of the function...
tailieu.vn Xem trực tuyến Tải xuống
p_statuses.begin. p_statuses.end(),. It would be nice if the syntax was identical to the first example, so that the focus is on what the code really does rather than how it does it. Using bind , we do not need to be explicit about the fact that we are dealing with elements that are pointers (this is already encoded in the...
tailieu.vn Xem trực tuyến Tải xuống
void print_string(const std::string&. s) const { std::cout <<. s <<. void print_string(const std::string s. std::cout <<. The first bind expression binds to the free function print_string . Because the function expects one argument, we need to use one placeholder (_1) to tell bind which of its arguments will be passed as the first argument of. print_string . To invoke the...
tailieu.vn Xem trực tuyến Tải xuống
boost::bind(&personal_info::surname,_1), boost::bind(&personal_info::surname,_2)));. Although it is technically possible to sort using binders based upon. complex criteria, it is not wise. Although it is sometimes tempting to do more in terms of binding, strive to write binders that are as clever as the people who must maintain it, but no more so.. Suppose that you need to test an int to see...
tailieu.vn Xem trực tuyến Tải xuống
boost::bind(&tracer::print,boost::ref(t),_1)(. boost::bind(&tracer::print,&t,_1)(. std::cout <<. boost::bind(&base::print,_1)(b);. boost::bind(&base::print,_1)(d);. s <<. std::map<int,std::string>. boost::bind(&print_string, boost::bind(. &std::map<int,std::string>::value_type::second,_1)));. typedef std::map<int,std::string>. boost::bind(&map_type::value_type::second,_1)));. #include "boost/bind.hpp". (*os) <<. i <<. std::map<std::string,std::vector<int>. boost::bind(&print,&std::cout, boost::bind(&std::vector<int>::size, boost::bind(. &std::map<std::string,. typedef std::map<std::string,std::vector<int>