VS2017编译配置Boost库_煮雪品茶的博客-CSDN博客
boost 库安装的时候,要 cd 到库目录里面编译,如果没有 cd 进去,而是直接绝对路径编译,可能导致找不到 build.bat 的错误,如果库目录垮盘符,cd 是不能直接切盘符的,参考下面
Windows的cmd中cd指令无法转换路径怎么办? 【百科全说】
boost::split 是把 string 切分到 vector
- #include
- #include
- #include
-
- using namespace std;
-
-
- int main() {
- std::string s1("aa bb cc");
- std::vector
split_string; -
- boost::split(split_string, s1, boost::is_any_of(" "));
-
- for (auto i : split_string) {
- cout << "size:" << i.length() << ",i:" << i << endl;
- }
- cout << "======" << endl;
- std::string s2(" aa bb cc");
- split_string.clear();
-
- boost::split(split_string, s2, boost::is_any_of(" "));
-
- for (auto i : split_string) {
- cout << "size:" << i.length() << ",i:" << i << endl;
- }
- return 0;
- }
- /*
- 输出
- size:2,i:aa
- size:2,i:bb
- size:2,i:cc
- ======
- size:0,i:
- size:0,i:
- size:0,i:
- size:0,i:
- size:0,i:
- size:2,i:aa
- size:2,i:bb
- size:2,i:cc
- */
boost::trim 可以删除字符串前后空格、换行符等
- #include
- #include
-
- using namespace std;
- int main() {
-
- string str = "\r\n\t aa bb cc\r\n\t";
- boost::trim(str);
- cout << str << endl;
- cout << str.size();
- return 0;
- }
- /*
- 输出
- aa bb cc
- 8
- */
lexical_cast 的数值转换对标C语言的 atoi 等,字符串转换对标 C++ 的 to_string,据说类型检查会更严格
- #include
- #include
-
- using namespace std;
- int main() {
-
- long y = boost::lexical_cast<long>("2000");
- cout << y << endl;
-
- string str = boost::lexical_cast
(3000); - cout << str << endl;
- return 0;
- }
- /*
- 输出
- 2000
- 3000
- */
字符串擦除可以删除所有我们指定的子字符串
- #include
- #include
-
- using namespace std;
- int main() {
- string line = "a,,b,,c";
- boost::algorithm::erase_all(line, ",,");
- cout << line << endl;
- return 0;
- }
- /*
- 输出
- abc
- */
有时候,我们对于一些变量是否初始化会定义一个特殊的值作为没初始化的标志,比如:0,但是万一 0 也是一个正常值怎么办,这个时候就可以使用我们的 boost::optional,里面有个初始化的函数判断,帮助我们判断该变量是否已经初始化
像编译完的 protobuf,也有类似的函数
- #include
- #include "boost/optional.hpp"
- using namespace std;
-
- void print(boost::optional<int> &i) {
- // 是否初始化判断
- if (i.is_initialized()) {
- cout << i.get() << endl;
- } else {
- cout << "该值未初始化" << endl;
- }
- }
-
- int main() {
- boost::optional<int> a = 1;
- boost::optional<int> b;
- print(a);
- print(b);
- }
- /*
- 输出
- 1
- 该值未初始化
- */
如果我们想直接标记某个变量没有初始化(标记异常情况,但是函数又必须返回一个值),我们就可以使用 boost::none,这个在一些返回值的函数中使用比较多
- #include
- #include "boost/optional.hpp"
- using namespace std;
-
- boost::optional<int> set(int i) {
- // 假设 i > 0 是正常情况
- if (i > 0) {
- boost::optional<int> x = i;
- return x;
- }
- return boost::none;
- }
-
- void print(boost::optional<int> &i) {
- // 是否初始化判断
- if (i.is_initialized()) {
- cout << i.get() << endl;
- } else {
- cout << "该值未初始化" << endl;
- }
- }
-
- int main() {
- boost::optional<int> a = set(1);
- boost::optional<int> b = set(-1);
- print(a);
- print(b);
- }
- /*
- 输出
- 1
- 该值未初始化
- */
std::transform 就是把 A 数组的数据倒腾到 B 数组,如下面,A 和 B 可以不是同一个类型,但是接收者 B 的容量必须大于等于 A,不过我们可以在倒腾的过程中对参数进行处理,如下面的 op 函数
- #include
// std::transform - #include
// std::cout - #include
// std::vector -
- struct temp {
- int x;
- float y;
- };
-
- int op(temp t) { // 参数必须为 A 的元素类型
- // 这里可以对参数进行处理
- return t.x * 2;
- }
-
- int main() {
- std::vector
A; - std::vector<int> B;
-
- // set some values:
- for (int i = 1; i < 6; i++) {
- temp t = {i, 0};
- A.push_back(t); // A: 1 2 3 4 5
- }
-
- B.resize(A.size()); // B 的容量不能比 A 小
-
- std::transform(A.begin(), A.end(), B.begin(), op); // B: 2 4 6 8 10
- for (int &it : B) {
- std::cout << it << ' '; // 2 4 6 8 10
- }
-
- return 0;
- }