On Mar 17, 12:48 pm, yogi_bear_79 <yogi_bear...@yahoo.comwrote:
Quote:
On Mar 17, 12:28 am, Martin York <Martin.YorkAma...@gmail.comwrote:
Quote:
On Mar 16, 6:56 pm, yogi_bear_79 <yogi_bear...@yahoo.comwrote:
Quote:
Quote:
Quote:
On Mar 16, 9:49 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Quote:
Quote:
Quote:
yogi_bear_79 wrote:
Below is a snippet of code that works for me.
Currenlty it populates my vector with one digit per
element. I would like to modify this code to populate
each element of the vector with 3 digits from the
string.
Quote:
Quote:
Quote:
(i.e. 179,534,672,198 would be stored with block [0] =
198, block [1] = 672, block [2] = 534, and block [3] =
179)
Quote:
Quote:
Quote:
This example is poorly chosen since it hides an
important detail. How do you want to store "12345"? Do
you want 12.345 or 123.45?
Quote:
Quote:
Quote:
I would want it to be [012] {345]
Quote:
Quote:
Accosding to your first post you mean:
[345]
[012]
Quote:
Quote:
But will the input include ',' or were they just to help
explain? ie. Is the input going to be a number or a real
string.- Hide quoted text -
Quote:
just a number, no other characters will be present
It's a weird requirement, but:
std::string::const_iterator end = myString.end() ;
std::string::const_iterator begin = myString.begin() ;
while ( end - begin >= 3 ) {
std::istringstream s( std::string( begin, begin + 3 ) ) ;
int i ;
s >i ;
// Error handling goes here...
myVect.push_back( i ) ;
begin += 3 ;
}
In general, when you need to break a string (or any container
supporting random access iterators) up into smaller blocks, you
can create an instance using two iterators, which delimit the
smaller block. And any time you have to convert from or to
strings, istringstream or ostringstream do the trick.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34