A
arnuld
A program that asks user to input words and then prints them in
alphabetical order. I have used vectors to accomplish task. That left me
wondering with 2 questions:
1) whether list will be a good idea. I am basically concerned about CPU
efficiency.
2) Is the program is a C++ program or C program written in C++.
/* A program that will ask user for input and then will print them
* in an alphabetical order
*
* VERSION 1.0
*
*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
void ask_input( std::vector<std::string>& );
void print_vector( const std::vector<std::string>& );
int main()
{
std::vector<std::string> svec;
std::vector<std::string>& r_svec = svec;
ask_input( r_svec );
// sort the input
std::sort( r_svec.begin(), r_svec.end() );
std::cout << "--------------------------------"
<< std::endl;
print_vector( r_svec );
return 0;
}
void ask_input( std::vector<std::string>& svec )
{
std::string str;
while( std::cin >> str )
{
svec.push_back( str );
}
}
void print_vector( const std::vector<std::string>& svec )
{
std::vector<std::string>::const_iterator iter = svec.begin();
for( ; iter != svec.end(); ++iter )
{
std::cout << *iter << std::endl;
}
}
alphabetical order. I have used vectors to accomplish task. That left me
wondering with 2 questions:
1) whether list will be a good idea. I am basically concerned about CPU
efficiency.
2) Is the program is a C++ program or C program written in C++.
/* A program that will ask user for input and then will print them
* in an alphabetical order
*
* VERSION 1.0
*
*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
void ask_input( std::vector<std::string>& );
void print_vector( const std::vector<std::string>& );
int main()
{
std::vector<std::string> svec;
std::vector<std::string>& r_svec = svec;
ask_input( r_svec );
// sort the input
std::sort( r_svec.begin(), r_svec.end() );
std::cout << "--------------------------------"
<< std::endl;
print_vector( r_svec );
return 0;
}
void ask_input( std::vector<std::string>& svec )
{
std::string str;
while( std::cin >> str )
{
svec.push_back( str );
}
}
void print_vector( const std::vector<std::string>& svec )
{
std::vector<std::string>::const_iterator iter = svec.begin();
for( ; iter != svec.end(); ++iter )
{
std::cout << *iter << std::endl;
}
}