sort input

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]$
 
C

ctrucza

#include said:
#include <vector>
#include <string>

you will need

void get_input( std::vector<std::string>& svec )
{
  std::string aword;

  while( std::cin >> aword )
    {
      svec.push_back(aword);
    }

}

You could write the more STL-ish

void get_input( std::vector<std::string>& svec )
{
copy(istream_iterator<string>(cin), istream_iterator<string>(),
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";
    }

}

and

void print_input( std::vector<std::string>& svec )
{
copy(svec.begin(), svec.end(), ostream_iterator<string>(cout, "\n"));
}

Regards,
Csaba
 
A

arnuld

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]$
 
A

arnuld

I get compile time errors:

Okay I got this. It was in <iterator> header.



#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>

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::copy( std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>(),
std::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(), std::eek:stream_iterator<std::string>(std::cout, "\n") );
}



one thing still eludes me: "Why does using copy or std::copy makes no
difference ?"
 
J

Jerry Coffin

[ ... ]
It means I am giving the compiler a burden to search for namespace. Hence
using std::copy will be a good idea.

Not really -- lots of things would break in a hurry if ADL didn't work.
 
J

Jerry Coffin

Jerry said:
[ ... ]
It means I am giving the compiler a burden to search for namespace. Hence
using std::copy will be a good idea.

Not really -- lots of things would break in a hurry if ADL didn't work.

He didn't propose to break ADL, he just said that explicitly
specifying the namespace is a good idea. That's a POV which
I support. for one ADL can do surprising things. Also, as was
just demonstrated, the code is easier to read if the namespace
is spelled out explicitly.

My point apparently wasn't clear. I have no argument with his conclusion
at all -- only with the implication that this was a good idea _because_
("Hence") it was placing a burden on the compiler. In reality, the
compiler has to be able to handle it for things to work anyway, so
putting it to use in this situation doesn't cause it any real extra
work.

My own experience is that as often as not, rewriting code on the
assumption that one way of writing it will be easier to compile than
another is a mistake. IMO, code should be written bo be as readable as
possible for other people, and only rewritten to cover for a compiler
bug when truly necessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

sorting the input 16
C++ Primer ex 3.14 8
using sstream to read a vector<string> 8
counting input words 1
counting words in input 16
Can not create a Vector of Strings 10
Reverse a String 19
C++ Primer ex 9.14 11

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top