2
In a std::string
, the allocation is done so; in Chunks 128-character note. It takes every character from input and puts it by std::string::push_back
. How can I do it?
2
In a std::string
, the allocation is done so; in Chunks 128-character note. It takes every character from input and puts it by std::string::push_back
. How can I do it?
2
I believe you must be looking for istream_iterator
.
Consider a simple example:
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
istream_iterator<char> iit(cin);
do
{
cout << *iit;
iit++;
}
while (*iit != 'z');
return 0;
}
If you enter a sentence in which the last character is the letter z
, for example FooBarz
, cout
will only show FooBar
disregarding z
.
Another example, now using vectors, corrected:
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector <std::string> v;
std::istream_iterator<std::string> iit(std::cin);
v.push_back(*iit);
for ( std::vector<std::string>::iterator i = v.begin(); i != v.end(); i++ )
std::cout << *i << std::endl;
return 0;
}
Browser other questions tagged c++ allocation
You are not signed in. Login or sign up in order to post.
And how I would do that without the z, but rather check when Cin was done?
– user2692
@Lucashenrique You can use Std::istream_iterator the same way as any other iterator. To scroll through all of the input characters, you will need the start iterator, as shown in the answer, and the end iterator, which is simply a Std::istream_iterator built without passing, in this case, Std::Cin. Having the two iterators just compare them to each iteration and when they are different means that you have read all.
– Tiago Gomes
@DBX8 I already knew this technique; however, Std::Cin goes into a loop, and I don’t know how to break it. How should I get the input done just once?
– user2692
It must also be from your awareness that example 2 does not give output. (Mingw 4.8.1)
– user2692
Still not working.
– user2692
As a matter of fact, I can only get into the computer now. And it’s working. But think: if I give a proper example for creating a string, how would I use a string???
– user2692
I don’t understand your last question, @Lucashenrique...
– Massa
@Sunstreaker thinks: if I am implementing a string, how would I use a string?
– user2692