Boost logo

Boost Users :

Subject: Re: [Boost-users] Extracting items from a string
From: José Tomás Tocino García (theom3ga_at_[hidden])
Date: 2010-06-29 12:12:05


Hello.

> I have a string of the following format:
>
> 11, 0, 1461, 400
>
> I need to be able to extract any element of the string at any point, so for
> example, the 1, would return 11, or the second world return 0. What is the
> best way of extracting any individual element, without using any temporary
> variables.
>
> IF I use boost.tokenizer, can I access any element individually, without
> using iterators? Or is there a better way of dealing with this sort of
> string.

Looks like you want to have a vector with the tokenized elements. I'm
afraid the only way of getting the results of a tokenizing operation
is via iterators. However, you can easily assign the elements to a
vector:

#include<iostream>
#include<boost/tokenizer.hpp>
#include<boost/foreach.hpp>
#include<string>
#include <vector>
int main(){
   using namespace std;
   using namespace boost;
   string s = "This is, a test";
   tokenizer<> tok(s);
   vector <string> myTokens (tok.begin(), tok.end());
   BOOST_FOREACH(string & s, myTokens){
     cout << s << endl;
   }
}

Hope it helps.

-- 
José Tomás Tocino García

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net