C++ Primer 学习笔记 第十七章 标准库特殊设施

标准库特殊设施

637 初始化tuple

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
using namespace std;


int main(){
    tuple<size_t, size_t, size_t> threeD; // all zero
    tuple<string, vector<double>, int, list<int>> someVal("constants", {3.14, 2.718}, 42, {0, 1, 2, 3, 4, 5});
    auto item = make_tuple("0-99-4324", 3, 20.00);
    return 0;
}

637 访问元组数据

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
using namespace std;


int main(){
    auto item = make_tuple("0-99-4324", 4, 20.00);
    typedef decltype(item) trans;

    int n = tuple_size<trans>::value;
    tuple_element<1, trans>::type cnt = get<1>(item);
    return 0;
}

638 元组的比较

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
using namespace std;


int main(){
    tuple<size_t, size_t> t1(0, 4);
    tuple<size_t, size_t> t2(1, 2);
    if (t1 < t2){
        cout << "t1<t2" << endl;
    }
    return 0;
}

638 使用tuple在函数里面返回多个值

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
using namespace std;

typedef tuple<string, int> match;

match func(int &i, string& s){
    return make_tuple(s, i);
}

int main(){
    int a = 1;
    string b = "dfsd";
    match  t = func(a, b);
    
    return 0;
}

646 正则表达式

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
using namespace std;


int main(){
    string pattern("[^c]ei");
    pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
    regex r(pattern);
    smatch results;
    string test_str = "receipt friend theif receive";
    if (regex_search(test_str, results, r)){
        cout << results.str() << endl; // theif
    }
    return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
using namespace std;


int main(){
    string pattern("[0-9]+");
    regex r(pattern);
    smatch results;
    string test_str = "abc123def";
    if (regex_search(test_str, results, r)){
        cout << results.str() << endl; // theif
    }
    return 0;
}

648 匹配所有cpp cxx cc结尾的文件

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
using namespace std;


int main(){
   regex r("[A-Za-z0-9]+\\.(cpp|cxx|cc)$", regex::icase);
   smatch result;
   string filename;
   while(cin>>filename){
       if(regex_search(filename, result, r)){
           cout << result.str() << endl;
       }
   }
    return 0;
}

正则表达式抛出异常

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
using namespace std;


int main(){
    try {
        regex r("[[:alnum:]+\\.(cpp|cxx|cc)$", regex::icase);
    } catch(regex_error e) {
        cout << e.what() << "\ncode: " << e.code() << endl;
    }
    return 0;
}
C:\Users\ethan\CLionProjects\untitled\cmake-build-debug\untitled.exe
regex_error(error_brack): The expression contained mismatched [ and ].
code: 4

650 smatch和cmatch

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
using namespace std;


int main(){

    regex r("[[:alnum:]]+\\.(cpp|cxx|cc)$", regex::icase);
    string file("myfile.cpp");
    // using smatch input must be string type
    smatch result;
    if (regex_search(file, result, r)){
        cout << result.str() << endl;
    }

    // char[]
    cmatch result2;
    if (regex_search("test.cpp", result2, r)){
        cout << result.str() << endl;
    }
    
    return 0;
}

651 sregex_iterator 循环调用regex_search来找出所有匹配的字符

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
using namespace std;


int main(){
    string file = "receipt friend theif receive keif";

    string pattern("[^c]ei");
    pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
    regex r(pattern, regex::icase);
    for (sregex_iterator it(file.begin(), file.end(), r), end_it; it!= end_it; ++it){
        cout << it->str() << endl;
//        theif
//        keif
    }
    return 0;
}

652 匹配以后前后只取20个字符

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
using namespace std;


int main(){
    string doc = "hey read or write according to the type being handled. The input operators ignore whi";
    regex r("being");
    for (sregex_iterator it(doc.begin(), doc.end(), r), end_it; it!=end_it;++it){
        auto pos = it->prefix().length();
        pos = pos > 20? pos-20:0;
        cout << it->prefix().str().substr(pos)
        << "\n\t\t>>>" << it->str() <<"<<<\n"
        << it->suffix().str().substr(0,20)
        << endl;
        /*
         * cording to the type
                >>>being<<<
            handled. The input
         * 
         * */
    }
    return 0;
}

17.17

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
using namespace std;


int main(){
    string test_str = "receipt friend theif receive weik";
    string pattern("cei");
    pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
    regex r(pattern);

    for (sregex_iterator it(test_str.begin(), test_str.end(), r), it_end; it!=it_end; ++it){
        cout << it->str() << endl;
    }
    
    return 0;
}

654 只打印子表达式

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
using namespace std;


int main(){
    regex r("([A-Za-z0-9]+)\\.(cpp|cxx|cc)$", regex::icase);
    string test_str("test.cpp");
    smatch result;

    regex_search(test_str, result, r);
    cout << result.str(0) << " " << result.str(1) << " " << result.str(2) << endl;
    // test.cpp test cpp
    return 0;
}

655 子表达式用于数据验证

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
using namespace std;


int main(){

    string phone = "(\\()?(\\d{3})(\\))?([-. ]?)(\\d{3})([-. ]?)(\\d{4})";
    regex r(phone);

    string test_str = "(020) 123 4567";
    smatch result;
    regex_search(test_str, result, r);
    cout << result.str() << endl;

    return 0;
}

657 regex_replace格式转换

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
using namespace std;


int main(){
    // 一共有7个子表达式
    string phone = "(\\()?(\\d{3})(\\))?([-. ]?)(\\d{3})([-. ]?)(\\d{4})";
    regex r(phone);

    string test_str = "(020) 123-4567";
    smatch result;
    regex_search(test_str, result, r);
    cout << result.str() << endl;
    
    // 只保留了 2,5,7 号表达式而忽略其他
    string fmt = "$2.$5.$7";
    cout << regex_replace(test_str, r, fmt) << endl; // 020.123.4567
    return 0;
}

658 单行读取然后替换

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
using namespace std;


int main(){
    string phone = "(\\()?(\\d{3})(\\))?([-. ]?)(\\d{3})([-. ]?)(\\d{4})";
    regex r(phone);

    smatch m;
    string s;
    string fmt = "$2.$5.$7";
    while (getline(cin, s)){
        cout << regex_replace(s, r, fmt) << endl;
    }
    return 0;

}

659 用来控制已经匹配的部分格式化输出

如下不输出没有匹配到的部分

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
using namespace std;



int main(){
    string phone = "(\\()?(\\d{3})(\\))?([-. ]?)(\\d{3})([-. ]?)(\\d{4})";
    regex r(phone);

    smatch m;
    string s;
    string fmt = "$2.$5.$7";
    while (getline(cin, s)){
        cout << regex_replace(s, r, fmt, std::regex_constants::format_no_copy) << endl;
    }
    return 0;

}

660 生成随机数

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
using namespace std;


int main(){
    default_random_engine e;
    for(size_t i=0; i<10; ++i){
        cout << e() << " "; 
        // 3499211612 581869302 3890346734 3586334585 545404204 4161255391 3922919429 949333985 2715962298 1323567403
    }
    return 0;

}

661 生成0-9范围的随机数

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
using namespace std;


int main(){
    uniform_int_distribution<unsigned> u(0, 9);
    default_random_engine e;
    for(size_t i=0; i<10; ++i){
        cout << u(e) << " ";
    }
    return 0;

}

662 每次调用引擎产生的随机数是一样,产生问题

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
using namespace std;


vector<unsigned> bad_randVec(){
    default_random_engine e;
    uniform_int_distribution<unsigned> u(0, 9);
    vector<unsigned> ret;
    for (size_t i=0; i<100; ++i){
        ret.push_back(u(e));
    }
    return ret; //equal
}




int main(){
    vector<unsigned> v1(bad_randVec());
    vector<unsigned> v2(bad_randVec());
    cout << ((v1==v2)? "equal":"not equal")<< endl;



    return 0;

}

662 解决方法,引擎和关联分布都声明成static

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
using namespace std;


vector<unsigned> bad_randVec(){
    static default_random_engine e;
    static uniform_int_distribution<unsigned> u(0, 9);
    vector<unsigned> ret;
    for (size_t i=0; i<100; ++i){
        ret.push_back(u(e));
    }
    return ret; //not equal
}




int main(){
    vector<unsigned> v1(bad_randVec());
    vector<unsigned> v2(bad_randVec());
    cout << ((v1==v2)? "equal":"not equal")<< endl;



    return 0;

}

663 设定随机数种子就可以生成2组不一样的随机数

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
using namespace std;


int main(){
    default_random_engine e1(18);
    default_random_engine e2(20);
    uniform_int_distribution<unsigned> u(0,9);
    for (size_t i=0; i!=5; ++i){
        cout << "e1: " << u(e1) << " e2: " << u(e2) << endl;
    }
    /*
     *  e1: 6 e2: 1
        e1: 3 e2: 0
        e1: 0 e2: 7
        e1: 5 e2: 3
        e1: 6 e2: 4
     * */
    return 0;

}

663 使用时间戳作为随机数种子

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
#include <ctime>
using namespace std;


int main(){

    cout << time(0) << endl;  // 1646705492
    // 使用时间戳作为随机数的种子,保证每次返回结果都不一样
    default_random_engine e(time(0));
    uniform_int_distribution<unsigned> u(0, 9);

    cout << u(e) << endl; // 7
    return 0;

}

664 生成浮点型的随机数

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
#include <ctime>
using namespace std;


int main(){
    default_random_engine e;
    uniform_real_distribution<double> u(0, 1);
    for (size_t i=0; i<10; ++i){
        cout << u(e) << " "; //0.135477 0.835009 0.968868 0.221034 0.308167 0.547221 0.188382 0.992881 0.996461 0.967695
    }
    return 0;
}

664 生成正态分布的随机数

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
#include <ctime>
using namespace std;


int main(){
    default_random_engine e;
    normal_distribution<> n(4, 1.5);

    vector<unsigned> vals(9);
    for (size_t i=0; i!=200; ++i){
        unsigned v = lround(n(e));
        if (v < vals.size()){
            ++ vals[v];
        }
    }

    for(size_t j=0; j!=vals.size();++j){
        cout<< j << ": " << string(vals[j], '*') << endl;
    }

    return 0;
}

0: ***
1: ***********
2: ******************
3: ********************************************
4: *******************************************************
5: ********************************
6: ******************************
7: *****
8: *

666 二项分布(伯努利分布)随机数

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
#include <ctime>
using namespace std;


int main(){
    string resp;
    default_random_engine e;
    bernoulli_distribution b(0.9);

    static size_t t, f =0;
    for (size_t i=0; i!=20; ++i){
        bool res = b(e);
        if (res){
            cout << "true" << endl;
            ++ t;
        } else{cout<< "false" << endl; ++f;}
    }

    cout << "t: " << t << " f: " << f; // t: 15 f: 5
    return 0;
}

666 格式化输入和输出

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
#include <ctime>
using namespace std;


int main(){
    cout << "default bool values: " << true << " " << false << endl;
    cout << "alpha bool values: " << boolalpha << true << " " << false << endl; // 直接输出true false

    cout << "default " << 12 << endl;
    cout << "octal " << oct << 12 << endl; // 8进制  14
    cout << "hex: " << hex << 12 << endl; // 16进制  c
    cout << "decimal " << dec << 12 << endl; // 改回10 进制


    return 0;
}

668 输出进制的控制符号

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
#include <ctime>
using namespace std;


int main(){
    cout << showbase << uppercase; // 显示进制的提示富豪
    cout << "default " << 12 << endl;
    cout << "octal " << oct << 12 << endl; // 8进制  014  0开头的为8进制
    cout << "hex: " << hex << 12 << endl; // 16进制  0xc   0x开头为16进制
    cout << "decimal " << dec << 12 << endl; // 改回10 进制  没有前缀为10进制


    return 0;
}

669 使用操控符号设置浮点数的精度

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
#include <ctime>
#include <iomanip>
using namespace std;


int main(){
    cout << "Precision: " << cout.precision() << ", value: " << sqrt(2.0) << endl; // Precision: 6, value: 1.41421
    cout.precision(12);
    cout << "Precision: " << cout.precision() << ", value: " << sqrt(2.0) << endl; // Precision: 12, value: 1.41421356237
    cout << setprecision(3);
    cout << "Precision: " << cout.precision() << ", value: " << sqrt(2.0) << endl; //Precision: 3, value: 1.41
    
    return 0;
}

670 使用科学计数法输出

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
#include <ctime>
#include <iomanip>
using namespace std;


int main(){
    cout << "default format: " << 100* sqrt(2.0) << '\n';
    cout << "scientific: " << scientific << 100 * sqrt(2.0) << '\n';
    cout << "fixed decimal: " << fixed << 100 * sqrt(2.0) << '\n';
    cout << "hexadecimal: " << hexfloat << 100 * sqrt(2.0) << '\n';
    cout << "use defaults: " << defaultfloat << 100 * sqrt(2.0);
    
    /*
     default format: 141.421
        scientific: 1.414214e+02
        fixed decimal: 141.421356
        hexadecimal: 0x1.1ad7bcp+7
        use defaults: 141.421
     * */
    return 0;
}

671 输出小数点

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
#include <ctime>
#include <iomanip>
using namespace std;


int main(){
    cout << 10.0 << endl;
    cout << showpoint << 10.0;
    cout << noshowpoint << endl;
    return 0;
}

672 格式化字符串

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
#include <ctime>
#include <iomanip>
using namespace std;


int main(){
    int i = -16;
    double d = 3.14159;

    cout << "i: " << setw(12) << i << "next col" << '\n'; // i:          -16next col
    cout << "d: " << setw(12) << d << "next col" << '\n'; // d:      3.14159next col

    cout << left << "i: " << setw(12) << i << "next col" << '\n'; // i: -16         next col
    cout << left << "d: " << setw(12) << d << "next col" << '\n'; // d: 3.14159     next col
    cout << right;

    cout << internal;
    cout << "i: " << setw(12) << i << "next col" << '\n'; // i: -         16next col
    cout << "d: " << setw(12) << d << "next col" << '\n'; // d:      3.14159next col

    cout << right << setfill('#');
    cout << "i: " << setw(12) << i << "next col" << '\n'; // i: #########-16next col
    cout << "d: " << setw(12) << d << "next col" << '\n'; // d: #####3.14159next col
    
    cout << setfill(' ');
    return 0;
}

noskipws cin不忽略空格

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
#include <ctime>
#include <iomanip>
using namespace std;


int main(){
    char ch;
    cin >> noskipws;
    while (cin >> ch){
        cout << ch;
    }

    return 0;
}

int main(){
    char ch;
    while (cin.get(ch)){
        cout.put(ch);
    }

    return 0;
}

678 IO流综合练习

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <iterator>
#include <memory>
#include <fstream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <regex>
#include <random>
#include <ctime>
#include <iomanip>
using namespace std;


int main(){
    fstream inOut("doc.txt", fstream::ate | fstream::in | fstream::out);
    if (!inOut){
        cerr << "Unable to open file!" << endl;
        return EXIT_FAILURE;
    }
    auto end_mark = inOut.tellg();
    inOut.seekg(0, fstream::beg);
    size_t cnt = 0;
    string line;
    while (inOut && inOut.tellg()!=end_mark && getline(inOut, line)){
        cnt += line.size() + 1;
        auto mark = inOut.tellg();
        inOut.seekp(0, fstream::end);
        inOut << cnt;
        if (mark!=end_mark) inOut << " ";
        inOut.seekg(mark);
    }
    inOut.seekp(0, fstream::end);
    inOut << "\n";

    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值