C++如何把用空格隔开的字符串存入不同的字符串中

2025-03-10 18:30:50
推荐回答(2个)
回答1:

用stringstream可以用来分割空格、tab、回车换行隔开的字符串:

#include 
#include 
#include 

using namespace std;

int main() {
    string str = "hello world sperated by   spaces\tand\nhuiche";

    vector arr;
    istringstream ss(str);
    string word;
    while(ss>>word) {
        arr.push_back(word);
    }

    for(size_t i=0; i        cout << arr[i] << endl;
    }
    
    return 0;
}

回答2:

什么环境?能用CString吗?如果能,就很简便