A
arnuld
Earlier, I have posted a program like this, a month ago IIRC. I have
created it again, without looking at the old program. Can I have your
opinions on this:
1) I wanted my program to be efficient, so I used reference to vector.
2) anything else you think worth mentioning
/* A program that asks the user for input and when user hits EOF will sort the words
* alphabetically and prints them.
*
*/
#include <iostream>
#include <vector>
#include <string>
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 )
{
std::string aword;
while( std::cin >> aword )
{
svec.push_back(aword);
}
}
void print_input( std::vector<std::string>& svec )
{
for( std::vector<std::string>::const_iter iter = svec.begin();
iter != svec.end(); ++iter )
{
std::cout << *iter << "\n";
}
}
==================== OUTPUT ========================
[arnuld@dune cpp]$ g++ -ansi -pedantic -Wall -Wextra sort-input.cpp
[arnuld@dune cpp]$ ./a.out
comp
lang
c++
is where cpp people live
--------------------------------
c++
comp
cpp
is
lang
live
people
where
[arnuld@dune cpp]$
created it again, without looking at the old program. Can I have your
opinions on this:
1) I wanted my program to be efficient, so I used reference to vector.
2) anything else you think worth mentioning
/* A program that asks the user for input and when user hits EOF will sort the words
* alphabetically and prints them.
*
*/
#include <iostream>
#include <vector>
#include <string>
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 )
{
std::string aword;
while( std::cin >> aword )
{
svec.push_back(aword);
}
}
void print_input( std::vector<std::string>& svec )
{
for( std::vector<std::string>::const_iter iter = svec.begin();
iter != svec.end(); ++iter )
{
std::cout << *iter << "\n";
}
}
==================== OUTPUT ========================
[arnuld@dune cpp]$ g++ -ansi -pedantic -Wall -Wextra sort-input.cpp
[arnuld@dune cpp]$ ./a.out
comp
lang
c++
is where cpp people live
--------------------------------
c++
comp
cpp
is
lang
live
people
where
[arnuld@dune cpp]$