#include <iostream> #include <vector> size_t split(std::string &src, std::vector<std::string> *tokens, std::string sep) { size_t last= 0; size_t index = src.find(sep, last); size_t length = src.size(); while(index != std::string::npos) { tokens->push_back(src.substr(last, index-last)); last = index + 1; index = src.find(sep, last); } if(length - last > 0) { tokens->push_back(src.substr(last, length-last)); } return tokens->size(); } int main(int argc, char* argv[]) { std::string src = "QWEQWE"; std::string sep = "W"; std::vector<std::string> tokens; std::cout << split(src, &tokens, sep) << std::endl; for(std::vector<std::string>::iterator iter = tokens.begin(); iter != tokens.end(); iter++) { std::cout << *iter->c_str() << std::endl; } return 0; }
时间: 2024-10-12 15:47:33