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

This is a discussion on Question in using boose::tokernizer [How to tell how many tokens in a string] within the C++ Programming forums, part of the General Programming Boards category; Here is the code. How can I tell how many tokens in tok? Is there a "size()" like STL containers? ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    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,893
    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, 02:52 PM
  3. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 06:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21