袋熊的树洞

日拱一卒,功不唐捐

0%

C++ Snippets Part 1

Introduction

C++ Snippets

A string tokenizer

字符串分隔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <string>
#include <vector>
#include <iostream>

using namespace std;

void tokenize(const string &str, vector<string> &tokens, const string &delimiters = " ")
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);

while (string::npos != pos || string::npos != lastPos) {
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}

int main()
{
vector<string> tokens;
string str("Split me up! Word1 Word2 Word3.");

tokenize(str, tokens);

cout << "String: " << str << endl;
cout << "Tokens: " << endl;
for (auto it = tokens.begin(); it != tokens.end(); it++)
cout << *it << endl;
return 0;
}