9.5.3

来源:互联网 发布:cloudify 教程 ubuntu 编辑:程序博客网 时间:2024/05/22 05:13

@pezy
9.47

#include <string>#include <iostream>using std::string;using std::cout;using std::endl;int main(){    string numbers{ "123456789" };    string alphabet{ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" };    string str{ "ab2c3d7R4E6" };    cout << "numeric characters: ";    for (int pos = 0; (pos = str.find_first_of(numbers, pos)) != string::npos; ++pos)        cout << str[pos] << " ";    cout << "\nalphabetic characters: ";    for (int pos = 0; (pos = str.find_first_of(alphabet, pos)) != string::npos; ++pos)        cout << str[pos] << " ";    cout << endl;    return 0;}

9.48

string::npos

9.49

#include <string>#include <fstream>#include <iostream>using std::string; using std::cout; using std::endl; using std::ifstream;int main(){    ifstream ifs("../data/letter.txt");    if (!ifs) return -1;    string longest;    auto update_with = [&longest](string const& curr)    {        if (string::npos == curr.find_first_not_of("aceimnorsuvwxz"))            longest = longest.size() < curr.size() ? curr : longest;    };    for (string curr; ifs >> curr; update_with(curr));    cout << longest << endl;    return 0;}
0 0
原创粉丝点击