Connecting Tech Pros Worldwide Help | Site Map

Populating Vector from string Help

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 17th, 2008, 01:45 AM
yogi_bear_79
Guest
 
Posts: n/a
Default Populating Vector from string Help

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'));
}

  #2  
Old March 17th, 2008, 01:55 AM
Kai-Uwe Bux
Guest
 
Posts: n/a
Default 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
  #3  
Old March 17th, 2008, 02:05 AM
yogi_bear_79
Guest
 
Posts: n/a
Default 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]
  #4  
Old March 17th, 2008, 04:35 AM
Martin York
Guest
 
Posts: n/a
Default 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.
  #5  
Old March 17th, 2008, 11:55 AM
yogi_bear_79
Guest
 
Posts: n/a
Default 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
  #6  
Old March 18th, 2008, 08:25 AM
James Kanze
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.