Populating Vector from string Help

Y

yogi_bear_79

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<int> vOne, 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'));
}
 
K

Kai-Uwe Bux

yogi_bear_79 said:
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
 
Y

yogi_bear_79

yogi_bear_79 said:
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

I would want it to be [012] {345]
 
M

Martin York

yogi_bear_79 said:
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?

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.
 
Y

yogi_bear_79

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.
(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]

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
 
J

James Kanze

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.
(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]
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 -
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top