A
arnuld
PURPOSE: This program asks the user to input words and then it
prints all of them in the same order but each word is reversed. e.g.
INPUT -> C++ Bjarne
OUTPUT -> ++C enrajB
*/
PROBLEM: I am not able to reverse the individual strings :-(
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
std::vector<std::string> svec;
std::copy( std::istream_iterator<std::string>( std::cin ),
std::istream_iterator<std::string>(),
std::back_inserter( svec ) );
/* prints the vector to standard output */
std::cout << "\n------------------------------------\n";
std::copy( svec.begin(), svec.end(),
std:stream_iterator<std::string>( std::cout, "\n" ) );
return 0;
}
========= OUTPUT ==============
~/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra reverse-input.cpp
~/programming/c++ $ ./a.out
C++ Bjarne
------------------------------------
C++
Bjarne
~/programming/c++ $
I tried to use "reverse_copy" in place of 1st std::copy:
std::reverse_copy( std::istream_iterator<std::string>( std::cin ),
std::istream_iterator<std::string>(),
std::back_inserter(svec ) );
but it raises compile-time error:
~/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra reverse-input.cpp
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.2.1/../../../../include/c++/4.2.1/bits/stl_algo.h:
In function ‘_OutputIterator std::reverse_copy(_BidirectionalIterator,
_BidirectionalIterator,
:_OutputIterator) [with _BidirectionalIterator =
std::istream_iterator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, char, std::char_traits<char>, long int>,
_OutputIterator =
std::back_insert_iterator<std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > > >]’:
reverse-input.cpp:22: instantiated from here
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.2.1/../../../../include/c++/4.2.1/bits/stl_algo.h:1722:
error: no match for ‘operato\ r--’ in ‘--__last’ ~/programming/c++
$
-- arnuld
http://lispmachine.wordpress.com
prints all of them in the same order but each word is reversed. e.g.
INPUT -> C++ Bjarne
OUTPUT -> ++C enrajB
*/
PROBLEM: I am not able to reverse the individual strings :-(
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
std::vector<std::string> svec;
std::copy( std::istream_iterator<std::string>( std::cin ),
std::istream_iterator<std::string>(),
std::back_inserter( svec ) );
/* prints the vector to standard output */
std::cout << "\n------------------------------------\n";
std::copy( svec.begin(), svec.end(),
std:stream_iterator<std::string>( std::cout, "\n" ) );
return 0;
}
========= OUTPUT ==============
~/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra reverse-input.cpp
~/programming/c++ $ ./a.out
C++ Bjarne
------------------------------------
C++
Bjarne
~/programming/c++ $
I tried to use "reverse_copy" in place of 1st std::copy:
std::reverse_copy( std::istream_iterator<std::string>( std::cin ),
std::istream_iterator<std::string>(),
std::back_inserter(svec ) );
but it raises compile-time error:
~/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra reverse-input.cpp
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.2.1/../../../../include/c++/4.2.1/bits/stl_algo.h:
In function ‘_OutputIterator std::reverse_copy(_BidirectionalIterator,
_BidirectionalIterator,
:_OutputIterator) [with _BidirectionalIterator =
std::istream_iterator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, char, std::char_traits<char>, long int>,
_OutputIterator =
std::back_insert_iterator<std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > > >]’:
reverse-input.cpp:22: instantiated from here
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.2.1/../../../../include/c++/4.2.1/bits/stl_algo.h:1722:
error: no match for ‘operato\ r--’ in ‘--__last’ ~/programming/c++
$
-- arnuld
http://lispmachine.wordpress.com