boost::size
References
Headers
boost::size
is available by including
any of the following headers:
boost/range/size.hpp
orboost/range/functions.hpp
orboost/range/iterator_range_core.hpp
orboost/range/iterator_range_io.hpp
orboost/range/algorithm_ext.hpp
orboost/range/counting_range.hpp
orboost/range/any_range.hpp
orboost/range.hpp
Examples
size.cpp
#include <iostream>
#include <string>
#include <vector>
#include <boost/range.hpp>
int main() {
std::string str = "abcde";
std::vector<int> vec = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// boost::empty() and boost::size() do about what you'd expect.
std::cout << "empty(str): " << boost::empty(str) << std::endl;
std::cout << "size(vec): " << boost::size(vec) << std::endl;
// Prefer boost::size() over boost::distance(). The former calls the
// .size() member function if available and degrades to boost::distance()
// otherwise. Thus, there's no reason to use boost::distance() directly.
std::cout << "distance(vec): " << boost::distance(vec) << std::endl;
return 0;
}
Output:
empty(str): 0
size(vec): 10
distance(vec): 10
string_conversions.cpp
#include <iostream>
#include <string>
#include <vector>
#include <boost/range.hpp>
// as_literal() and as_array() need explicit imports.
#include <boost/range/as_literal.hpp>
#include <boost/range/as_array.hpp>
using std::cout;
using std::endl;
const char * const csptr = "BOOST";
const char csarr[] = "boost";
void raw_pointer_demo() {
// Calling Boost Range functions on cstrings doesn't work very well.
// The first doesn't compile, the second gives size() == 6, as it counts
// the terminal \0:
cout << "size(csptr): doesn't compile" /* << boost::size(csptr) */ << endl;
cout << "size(csarr): " << boost::size(csarr) << endl;
}
void as_literal_demo() {
// boost::as_literal() solves both of these problems by converting the
// cstrings to a string range. The size is 5, as you'd expect from a string.
cout << "size(as_literal(csptr)): " << boost::size(boost::as_literal(csptr)) << endl;
cout << "size(as_literal(csarr)): " << boost::size(boost::as_literal(csarr)) << endl;
}
void as_array_demo() {
// boost::as_array() serves only a documentary purpose.
// Calling it indicates that the author realizes that the argument is a
// character array, but explicitly wants to treat it as an array as
// opposed to a string.
// Note that the behavior is exactly the same as in raw_pointer_demo().
cout << "size(as_array(csptr)): doesn't compile"
/* << boost::size(boost::as_array(csptr)) */ << endl;
cout << "size(as_array(csarr)): " << boost::size(boost::as_array(csarr)) << endl;
}
int main() {
cout << "csptr = (const char *)\"boost\"" << endl;
cout << "csarr = \"boost\"" << endl;
raw_pointer_demo();
as_literal_demo();
as_array_demo();
return 0;
}
Output:
csptr = (const char *)"boost"
csarr = "boost"
size(csptr): doesn't compile
size(csarr): 6
size(as_literal(csptr)): 5
size(as_literal(csarr)): 5
size(as_array(csptr)): doesn't compile
size(as_array(csarr)): 6
Boost Range for Humans
This reference is part of Boost Range for Humans. Click the link to the overview.