0%

cpp string

优雅切割

  • 默认空格
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #include <string>
    #include <sstream>
    #include <iostream>

    using namespace std;

    int main() {
    istringstream iss("capiTalIze tHe titLe");
    string s;
    while (iss >> s) {
    std::cout << "size = "<< s.size() << " " << s << std::endl;
    }
    return 0;
    }
  • 指定字符
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #include <string>
    #include <sstream>
    #include <iostream>

    using namespace std;

    int main() {
    istringstream iss("capiTalIze, tHe, titLe");
    string s;
    while (getline(iss, s, ',')) {
    std::cout << "size = "<< s.size() << " " << s << std::endl;
    }
    return 0;
    }
  • 优雅format
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include <string>
    #include <sstream>
    #include <iostream>

    using namespace std;

    int main() {
    istringstream iss("1 2 3");
    int i, j, k;
    iss >> i >> j >> k;
    cout << "i=" << i << ", j=" << j << ", k=" << k << endl;
    return 0;
    }