Boost的StrTok

Boost裡面有個strtok的功能,以下是Boost官網裡的範例,寫下來以後可觀摩觀摩:

#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>
using namespace std;
using namespace boost;

int main()
{
    {
        string s = "12252001";
        int offsets[] = {2, 2, 4};
        offset_separator f(offsets, offsets + 3);
        tokenizer<offset_separator> tok(s, f);
        for (tokenizer<offset_separator>::iterator beg = tok.begin(); beg != tok.end(); ++beg)
        {
            cout << *beg << "\n";
        }
    }

    {
        string s = "Field 1,\"putting quotes around fields, allows commas\",Field 3";
        tokenizer<escaped_list_separator<char> > tok(s);
        for (tokenizer<escaped_list_separator<char> >::iterator beg = tok.begin(); beg != tok.end(); ++beg)
        {
            cout << *beg << "\n";
        }
    }

    {
        string s = "This is,  a test";
        tokenizer<> tok(s);
        for (tokenizer<>::iterator beg = tok.begin(); beg != tok.end(); ++beg)
        {
            cout << *beg << "\n";
        }
    }
}

0 意見:

張貼留言