You do not need to link the boost::algorithm library. It is a header only library, hence you only need to #include
its header files.
For examples, see
You do not need to link the boost::algorithm library. It is a header only library, hence you only need to #include
its header files.
For examples, see
#include <boost/algorithm/string/predicate.hpp> #include <string> #include <iostream> int main() { std::string mystr = "foobar"; // Prints "true, false" std::cout << std::boolalpha << boost::algorithm::starts_with(mystr, "foo") << ", " << boost::algorithm::starts_with(mystr, "bar") << std::endl; }
Also see: How to use boost string algorithm ends_with: A minimal example
#include <boost/algorithm/string/predicate.hpp> #include <string> #include <iostream> int main() { std::string mystr = "foobar"; // Prints "false, true" std::cout << std::boolalpha << boost::algorithm::ends_with(mystr, "foo") << ", " << boost::algorithm::ends_with(mystr, "bar") << std::endl; }
Also see: How to use boost string algorithm starts_with: A minimal example
Add this to your CMakeLists.txt
:
# # Compile test suite # find_package(Boost COMPONENTS system filesystem unit_test_framework REQUIRED) add_executable(test_myprogram tests/MyTest.cpp ) target_include_directories(test_myprogram PUBLIC "${CMAKE_CURRENT_LIST_DIR}/include") target_compile_features(test_myprogram PRIVATE cxx_std_17) target_link_libraries(test_myprogram ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} ) add_test(test_myprogram test_myprogram) # make "make test" run the test program add_custom_target(test DEPENDS myprogram COMMAND ./test_myprogram )
In order to build the program and the test, run
make
In order to run the tests, use
make test
This minimal example shows you how to write data to a .gz
file in C++, compressing the data on-the-fly using boost::iostreams
. Using the modern iostreams layer, as opposed to a block-based approach like zlib
allows you to use the full power and ease-of-use of std::ostream
.
#include <fstream> #include <iostream> #include <boost/iostreams/filtering_streambuf.hpp> #include <boost/iostreams/copy.hpp> #include <boost/iostreams/filter/gzip.hpp> using namespace std; int main(int argc, char** argv) { if(argc < 2) { cerr << "Usage: " << argv[0] << " <output .gz file>" << endl; } //Read filename from the first command line argument ofstream file(argv[1], ios_base::out | ios_base::binary); boost::iostreams::filtering_streambuf<boost::iostreams::output> outbuf; outbuf.push(boost::iostreams::gzip_compressor()); outbuf.push(file); //Convert streambuf to ostream ostream out(&outbuf); //Write some test data out << "This is a test text!\n"; //Cleanup boost::iostreams::close(outbuf); // Don't forget this! file.close(); }
cmake_minimum_required(VERSION 3.0) find_package(Boost 1.36.0 COMPONENTS iostreams) include_directories(${Boost_INCLUDE_DIRS}) add_executable(iostreams-gz-compress iostreams-gz-compress.cpp) target_link_libraries(iostreams-gz-compress ${Boost_LIBRARIES})
This minimal example shows you how to open a .gz
file in C++, decompress it on-the-fly using boost::iostreams
and then copy its contents to stdout
:
#include <fstream> #include <iostream> #include <boost/iostreams/filtering_streambuf.hpp> #include <boost/iostreams/copy.hpp> #include <boost/iostreams/filter/gzip.hpp> using namespace std; int main(int argc, char** argv) { if(argc < 2) { cerr << "Usage: " << argv[0] << " <gzipped input file>" << endl; } //Read from the first command line argument, assume it's gzipped ifstream file(argv[1], ios_base::in | ios_base::binary); boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf; inbuf.push(boost::iostreams::gzip_decompressor()); inbuf.push(file); //Convert streambuf to istream istream instream(&inbuf); //Copy everything from instream to cout << instream.rdbuf(); //Cleanup file.close(); }
cmake_minimum_required(VERSION 3.0) find_package(Boost 1.36.0 COMPONENTS iostreams) include_directories(${Boost_INCLUDE_DIRS}) add_executable(iostreams-gz-decompress iostreams-gz-decompress.cpp) target_link_libraries(iostreams-gz-decompress ${Boost_LIBRARIES})
In our previous post Minimal PugiXML file reader example we provided a short example of how to read from an uncompressed XML file using PugiXML. In practice, many large XML files are distributed as .xml.gz
package.
Since you can use boost::iostreams to decompress gzipped data on the fly and pipe it directly into PugiXML, you don’t need to store the uncompressed data on your hard drive.
#include <iostream> #include <fstream> #include <pugixml.hpp> #include <boost/iostreams/filtering_streambuf.hpp> #include <boost/iostreams/copy.hpp> #include <boost/iostreams/filter/gzip.hpp> using namespace std; using namespace pugi; int main() { // Open "raw" gzipped data stream ifstream file("test.xml.gz", ios_base::in | ios_base::binary); // Configure decompressor filter boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf; inbuf.push(boost::iostreams::gzip_decompressor()); inbuf.push(file); //Convert streambuf to istream istream instream(&inbuf); // Parse from stream xml_document doc; xml_parse_result result = doc.load(instream); // Print content of root element cout << "Load result: " << result.description() << "\n" << doc.child("root-element").child_value() // "Test text" << endl; }
cmake_minimum_required(VERSION 3.0) find_package(Boost 1.36.0 COMPONENTS iostreams) include_directories(${Boost_INCLUDE_DIRS}) add_executable(pugixml-example pugixml-example.cpp) target_link_libraries(pugixml-example pugixml ${Boost_LIBRARIES})
<?xml version="1.0" encoding="UTF-8"?> <root-element>Test text</root-element>
Download all three files and then run
gzip test.xml cmake . make ./pugixml-example
You should see an output like
Load result: No error Test text
#include <boost/lexical_cast.hpp> #include <iostream> int main() { int a = boost::lexical_cast<int>("123"); int b = boost::lexical_cast<int>("456"); int c = a + b; std::cout << c << std::endl; //Prints 579 }
If you want to compile your C++ project using boost::program_options
but you see error messages like
/home/uli/myProject/main.cpp:28:5: error: ‘po’ has not been declared po::notify (vm);
you are missing this declaration which you need to place directly after #include <boost/program_options.hpp>
:
namespace po = boost::program_options;
This declares po
as an alias for the boost::program_options
namespace, because writing po::variables_map
is much easier to read than boost::program_options::variables_map
and using namespace boost::program_options
might cause some name collisions with other functions.
Simply add these lines to the end of your CMakeLists.txt
and replace myTarget
by the name of your build target (usually the first argument to add_executable(...)
or add_library(...)
):
# Include boost find_package( Boost 1.30 COMPONENTS program_options REQUIRED ) target_include_directories( myTarget PRIVATE ${Boost_INCLUDE_DIR}) target_link_libraries( myTarget ${Boost_LIBRARIES} )
If you have multiple targets, copy & paste the last two lines for each target.
If you need a specific version of boost, replace 1.30
by the minimum version you need.
Examples in this post:
More examples will possibly be added in the future