Connecting Tech Pros Worldwide Help | Site Map

Populating Vector from string Help

yogi_bear_79
Guest
 
Posts: n/a
#1: Mar 17 '08
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.

(i.e. 179,534,672,198 would be stored with block [0] = 198, block [1]
= 672, block [2] = 534, and block [3] = 179)

vector<intvOne, vTwo;
string one, two;
size_t r;

cout << "\n\n\t Please enter the first integer:\t";
cin>>one;

r = one.length();

for (int x = 0; x < r; x++){
vOne.push_back(int(one[x] - '0'));
}
Kai-Uwe Bux
Guest
 
Posts: n/a
#2: Mar 17 '08

re: Populating Vector from string Help


yogi_bear_79 wrote:
Quote:
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.
>
(i.e. 179,534,672,198 would be stored with block [0] = 198, block [1]
= 672, block [2] = 534, and block [3] = 179)
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?

[snip]


Best

Kai-Uwe Bux
yogi_bear_79
Guest
 
Posts: n/a
#3: Mar 17 '08

re: Populating Vector from string Help


On Mar 16, 9:49*pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Quote:
yogi_bear_79 wrote:
Quote:
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:
(i.e. 179,534,672,198 would be stored with block [0] = 198, block [1]
= 672, block [2] = 534, and block [3] = 179)
>
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?
>
[snip]
>
Best
>
Kai-Uwe Bux
I would want it to be [012] {345]
Martin York
Guest
 
Posts: n/a
#4: Mar 17 '08

re: Populating Vector from string Help


On Mar 16, 6:56 pm, yogi_bear_79 <yogi_bear...@yahoo.comwrote:
Quote:
On Mar 16, 9:49 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>
>
>
Quote:
yogi_bear_79 wrote:
Quote:
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:
(i.e. 179,534,672,198 would be stored with block [0] = 198, block [1]
= 672, block [2] = 534, and block [3] = 179)
>
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:
[snip]
>
Quote:
Best
>
Quote:
Kai-Uwe Bux
>
I would want it to be [012] {345]
Accosding to your first post you mean:
[345]
[012]

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.
yogi_bear_79
Guest
 
Posts: n/a
#5: Mar 17 '08

re: Populating Vector from string Help


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:
On Mar 16, 9:49 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>
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:
(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:
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:
[snip]
>
Quote:
Quote:
Best
>
Quote:
Quote:
Kai-Uwe Bux
>
Quote:
I would want it to be [012] {345]
>
Accosding to your first post you mean:
[345]
[012]
>
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 -
>
- Show quoted text -
just a number, no other characters will be present
James Kanze
Guest
 
Posts: n/a
#6: Mar 18 '08

re: Populating Vector from string Help


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:
[snip]
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
Closed Thread