arnuld
11/5/2008 4:17:00 AM
> On Tue, 04 Nov 2008 05:29:40 -0800, ctrucza wrote:
> You could write the more STL-ish
>
> void get_input( std::vector<std::string>& svec )
> {
> copy(istream_iterator<string>(cin), istream_iterator<string>(),
> back_inserter< vector<string> >(svec));
> }
> ...SNIP....
I get compile time errors:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
void get_input( std::vector<std::string>& );
void print_input( std::vector<std::string>& );
int main()
{
std::vector<std::string> vec_of_strings;
get_input(vec_of_strings);
sort( vec_of_strings.begin(), vec_of_strings.end() );
print_input(vec_of_strings);
return 0;
}
void get_input( std::vector<std::string>& svec )
{
copy( istream_iterator<std::string>(std::cin), istream_iterator<std::string>(),
back_inserter<std::vector<std::string> >(svec) );
// Notice the last element, it is "> >(svec)" and not ">>(svec)"
// Just Remember the space.
}
void print_input( std::vector<std::string>& svec )
{
std::cout << "---------------------------------------------\n";
copy( svec.begin(), svec.end(), ostream_iterator<std::string>(std::cout, "\n") );
}
===================== OUTPUT =============================
[arnuld@dune cpp]$ g++ -ansi -pedantic -Wall -Wextra CLCPP_sort-input.cpp
CLCPP_sort-input.cpp: In function `void get_input(std::vector<std::string, std::allocator<std::string> >&)':
CLCPP_sort-input.cpp:30: error: `istream_iterator' was not declared in this scope
CLCPP_sort-input.cpp:30: error: expected primary-expression before '>' token
CLCPP_sort-input.cpp:30: error: expected primary-expression before '>' token
CLCPP_sort-input.cpp:30: error: expected primary-expression before ')' token
CLCPP_sort-input.cpp:31: error: `back_inserter' was not declared in this scope
CLCPP_sort-input.cpp:31: error: expected primary-expression before '>' token
CLCPP_sort-input.cpp:31: error: `copy' was not declared in this scope
CLCPP_sort-input.cpp:31: warning: unused variable 'back_inserter'
CLCPP_sort-input.cpp:31: warning: unused variable 'copy'
CLCPP_sort-input.cpp: In function `void print_input(std::vector<std::string, std::allocator<std::string> >&)':
CLCPP_sort-input.cpp:41: error: `ostream_iterator' was not declared in this scope
CLCPP_sort-input.cpp:41: error: expected primary-expression before '>' token
CLCPP_sort-input.cpp:41: warning: left-hand operand of comma has no effect
CLCPP_sort-input.cpp:41: warning: unused variable 'ostream_iterator'
[arnuld@dune cpp]$
[arnuld@dune cpp]$ gcc --version
gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-9)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[arnuld@dune cpp]$
--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is UnBlocked now :)