473,748 Members | 4,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sort input

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 ::stringvec_of_ strings;

get_input(vec_o f_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>::cons t_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]$

--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is UnBlocked now :)
Nov 4 '08 #1
9 2796
#include <iostream>
#include <vector>
#include <string>
you will need

#include <algorithm>

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_it erator<string>( cin), istream_iterato r<string>(),
back_inserter< vector<string(s vec));
}
>
void print_input( std::vector<std ::string>& svec )
{
* for( std::vector<std ::string>::cons t_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_iterato r<string>(cout , "\n"));
}

Regards,
Csaba
Nov 4 '08 #2
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_it erator<string>( cin), istream_iterato r<string>(),
back_inserter< vector<string(s vec));
}
...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 ::stringvec_of_ strings;

get_input(vec_o f_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_iterato r<std::string>( std::cin), istream_iterato r<std::string>( ),
back_inserter<s td::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_iterato r<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::str ing, std::allocator< std::string&)':
CLCPP_sort-input.cpp:30: error: `istream_iterat or' 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::s tring, std::allocator< std::string&)':
CLCPP_sort-input.cpp:41: error: `ostream_iterat or' 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_iterat or'
[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 :)
Nov 5 '08 #3
On Wed, 05 Nov 2008 09:17:26 +0500, arnuld wrote:
I get compile time errors:
Okay I got this. It was in <iteratorheader .

#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 ::stringvec_of_ strings;

get_input(vec_o f_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_it erator<std::str ing>(std::cin), std::istream_it erator<std::str ing>(),
std::back_inser ter<std::vector <std::string(sv ec) );

// 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::ostream_it erator<std::str ing>(std::cout, "\n") );
}

one thing still eludes me: "Why does using copy or std::copy makes no
difference ?"
--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is UnBlocked now :)
Nov 5 '08 #4
arnuld wrote:
[...]
void print_input( std::vector<std ::string>& svec )
{
std::cout << "---------------------------------------------\n";
copy( svec.begin(), svec.end(), std::ostream_it erator<std::str ing>(std::cout, "\n") );
}

one thing still eludes me: "Why does using copy or std::copy makes no
difference ?"
Because of ADL:
http://en.wikipedia.org/wiki/Argumen...nt_name_lookup

--
Thomas
Nov 5 '08 #5
On Wed, 05 Nov 2008 05:32:54 +0100, Thomas J. Gritzan wrote:
>arnuld wrote:
one thing still eludes me: "Why does using copy or std::copy makes no
difference ?"
Because of ADL:
http://en.wikipedia.org/wiki/Argumen...nt_name_lookup

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

--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is UnBlocked now :)
Nov 5 '08 #6
In article <pa************ *************** *@invalid.addre ss>,
su*****@invalid .address says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Nov 5 '08 #7
Jerry Coffin wrote:
In article <pa************ *************** *@invalid.addre ss>,
su*****@invalid .address says...

[ ... ]
>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.

Schob
Nov 6 '08 #8
In article <ge**********@h oshi.visyn.net> , sp******@gmx.de says...
Jerry Coffin wrote:
In article <pa************ *************** *@invalid.addre ss>,
su*****@invalid .address says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Nov 7 '08 #9
Jerry Coffin wrote:
[ ... ]
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.
Amen. :)

Schobi
Nov 7 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
6205
by: googleboy | last post by:
I didn't think this would be as difficult as it now seems to me. I am reading in a csv file that documents a bunch of different info on about 200 books, such as title, author, publisher, isbn, date and several other bits of info too. I can do a simple sort over the first field (title as it turns out), and that is fine as far as it gets:
1
2068
by: Derek Tinney | last post by:
Hi, I'm having difficulty building an XLST file that allows me to sort a list of log records. I put together an XSL file that allows me to output a copy of the input file and then I attempted to sort it. Eventually I want to filter it based on the "when" element (and/or others) but I cannot proceed until I get the sort to work. I have tried several approaches (specific XPATHs, data-type on the sort) none of which have worked (or have...
3
4603
by: Kevin King | last post by:
I have a question about an assignment I have. I need to count the number of comparisons in my merge sort. I know that the function is roughly nlog(n), but I am definately coming up with too many comparisons. It seems to me like I should just use a single counter in the merge function's 'if' statement, but this can't be right because an array of 50 takes about 100 comparisons this way. If anyone has any suggestions I would greatly...
12
2924
by: Eva | last post by:
Hi, I try to implement quick sort. I sort vectors by their first value. 10 2 3 4 9 3 5 6 10 4 5 6 must be 9 3 5 6 10 2 3 4 10 4 5 6 The prog works great on maybe 500 vectors, but I have an "Aborted(core
3
2662
by: rwise5 | last post by:
I have been tasked with finishing the following C program. I need to develop the sort by birthday function and the print grade function. I have been working on the print function for the last week and it seems like everything I try doesn't work. I guess I am not understanding how to deal with structures. Please if someone could please help me out I would appreciate it. Thank you. #include <stdio.h> #include <stdlib.h> #include...
2
2072
by: joesindel | last post by:
So heres the deal... I work for a company in which numbers are dropping. The owners created a game where calling potential clients racks up points for crap like basketball ticekts and such. The owners called upon me to develop a system where users could enter information to list user name, data stamp it, and then add form data such as the person they called and if it was a cold call or not. All that was pretty easy. Oh, by the by, I had...
2
3974
by: gonzo | last post by:
So I have a project where I'm supposed to have a .txt input file of no more than ten first names, last names and birth years, and than in a menu I'm to give the user some options as to how the strings can be sorted. My problem is kind of hard to understand, so please bear with me. I've figured out how to sort the last names, first names and birth years individually, but how do I make it so that all 3 things match up correctly so that the first...
0
1490
by: kgiraldin | last post by:
Hello, I have some code commented out in order to make this work. However, I would prefer to use the commented code so my users can SORT on the column heading because I know they are going to want to do that. I appreciate any help that you can give me. Thanks, Kris ----------------------------------------------------- <html> <body>
5
5971
by: Nirmala123 | last post by:
hi... I want to sort the table using combobox values. I give the code here. address.html: <html> <head> <title>Add a new entry</title> </head>
3
5192
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.