Thread: Question in using boose::tokernizer [How to tell how many tokens in a string]

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Question in using boose::tokernizer [How to tell how many tokens in a string]

    Here is the code.
    How can I tell how many tokens in tok? Is there a "size()" like STL containers?
    Of course I can use an iterator in a 'for' loop to scan and count, but is there an Off-The-Shelf function?
    Code:
    #include <boost/tokenizer.hpp>
    
        string iine("123#456#789");
        typedef boost::tokenizer< boost::char_separator<char> > token;             
        boost::char_separator<char> sep("#");                                      
        token tok(line, sep);                                                      
    
        cout<<tok.size()<<endl // Is there a function like this?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by meili100 View Post
    Here is the code.
    How can I tell how many tokens in tok? Is there a "size()" like STL containers?
    Of course I can use an iterator in a 'for' loop to scan and count, but is there an Off-The-Shelf function?
    I don't think so. The tokenizer only consumes input when you request the next token. This method of operation is fundamentally incompatible with being able to get the token count up-front. Just tokenize, and count.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There's std::distance, which does the counting for you.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  2. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  3. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM