Re: any improvements for this programme
On Sat, 21 Jul 2007 08:23:42 -0600, Jerry Coffin wrote:
I'd generally do things a bit differently. Right now, you have a (mildly
messy) if statement in the inner loop. Unless it's crucial that there
NOT be an extra space after the final string, I'd just add a space after
the end of each string, which would simplify the code considerably.
yes, you are right but someone mentioned it on the newsgroup, IIRC.
#include <iostream>
#include <iterator>
#include <algorithm>
int main() {
std::copy(std::istream_iterator<std::string>(std::cin),
std::istream_iterator<std::string>(),
std::ostream_iterator<std::string>(std::cout, " "));
return 0;
}
this code runs but it prints everything as sson as i press "Enter". i
think the problem wants us to print the input only when EOF is
encountered.
int main() {
std::ostringstream temp;
std::copy(std::istream_iterator<std::string>(std::cin),
std::istream_iterator<std::string>(),
std::ostream_iterator<std::string>(temp, " "));
std::string data = std::string(temp.str(),
0,
temp.str().length()-1);
std::cout << data;
return 0;
}
this gives error at line: "std::ostringstream temp;"
{arnuld@arch cpp }% g++ -ansi -pedantic -Wall -Wextra RL_3.8.cpp
RL_3.8.cpp: In function ???int main()???:
RL_3.8.cpp:26: error: aggregate ???std::ostringstream temp??? has incomplete type and cannot be defined
RL_3.8.cpp:32: error: ???length??? was not declared in this scope
RL_3.8.cpp:32: error: expected primary-expression before ???(??? token
RL_3.8.cpp:32: error: expected unqualified-id before ???(??? token
{arnuld@arch cpp }%
-- http://arnuld.blogspot.com